tmpfs_vnops.c revision 1.92.2.3 1 /* $NetBSD: tmpfs_vnops.c,v 1.92.2.3 2012/04/17 00:08:20 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.3 2012/04/17 00:08:20 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 error;
137
138 KASSERT(VOP_ISLOCKED(dvp));
139
140 dnode = VP_TO_TMPFS_DIR(dvp);
141 *vpp = NULL;
142
143 /* Check accessibility of directory. */
144 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
145 if (error) {
146 goto out;
147 }
148
149 /*
150 * If requesting the last path component on a read-only file system
151 * with a write operation, deny it.
152 */
153 if (lastcn && (dvp->v_mount->mnt_flag & MNT_RDONLY) != 0 &&
154 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
155 error = EROFS;
156 goto out;
157 }
158
159 /*
160 * Avoid doing a linear scan of the directory if the requested
161 * directory/name couple is already in the cache.
162 */
163 error = cache_lookup(dvp, vpp, cnp);
164 if (error >= 0) {
165 /* Both cache-hit or an error case. */
166 goto out;
167 }
168
169 if (cnp->cn_flags & ISDOTDOT) {
170 tmpfs_node_t *pnode;
171
172 /*
173 * Lookup of ".." case.
174 */
175 if (lastcn && cnp->cn_nameiop == RENAME) {
176 error = EINVAL;
177 goto out;
178 }
179 KASSERT(dnode->tn_type == VDIR);
180 pnode = dnode->tn_spec.tn_dir.tn_parent;
181 if (pnode == NULL) {
182 error = ENOENT;
183 goto out;
184 }
185
186 /*
187 * Lock the parent tn_vlock before releasing the vnode lock,
188 * and thus prevents parent from disappearing.
189 */
190 mutex_enter(&pnode->tn_vlock);
191 VOP_UNLOCK(dvp);
192
193 /*
194 * Get a vnode of the '..' entry and re-acquire the lock.
195 * Release the tn_vlock.
196 */
197 error = tmpfs_vnode_get(dvp->v_mount, pnode, vpp);
198 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
199 goto out;
200
201 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
202 /*
203 * Lookup of "." case.
204 */
205 if (lastcn && cnp->cn_nameiop == RENAME) {
206 error = EISDIR;
207 goto out;
208 }
209 vref(dvp);
210 *vpp = dvp;
211 error = 0;
212 goto done;
213 }
214
215 /*
216 * Other lookup cases: perform directory scan.
217 */
218 de = tmpfs_dir_lookup(dnode, cnp);
219 if (de == NULL || de->td_node == TMPFS_NODE_WHITEOUT) {
220 /*
221 * The entry was not found in the directory. This is valid
222 * if we are creating or renaming an entry and are working
223 * on the last component of the path name.
224 */
225 if (lastcn && (cnp->cn_nameiop == CREATE ||
226 cnp->cn_nameiop == RENAME)) {
227 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
228 if (error) {
229 goto out;
230 }
231 error = EJUSTRETURN;
232 } else {
233 error = ENOENT;
234 }
235 if (de) {
236 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
237 cnp->cn_flags |= ISWHITEOUT;
238 }
239 goto done;
240 }
241
242 tnode = de->td_node;
243
244 /*
245 * If it is not the last path component and found a non-directory
246 * or non-link entry (which may itself be pointing to a directory),
247 * raise an error.
248 */
249 if (!lastcn && tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
250 error = ENOTDIR;
251 goto out;
252 }
253
254 /* Check the permissions. */
255 if (lastcn && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
256 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
257 if (error)
258 goto out;
259
260 if ((dnode->tn_mode & S_ISTXT) != 0) {
261 error = kauth_authorize_vnode(cnp->cn_cred,
262 KAUTH_VNODE_DELETE, tnode->tn_vnode,
263 dnode->tn_vnode, genfs_can_sticky(cnp->cn_cred,
264 dnode->tn_uid, tnode->tn_uid));
265 if (error) {
266 error = EPERM;
267 goto out;
268 }
269 }
270 }
271
272 /* Get a vnode for the matching entry. */
273 mutex_enter(&tnode->tn_vlock);
274 error = tmpfs_vnode_get(dvp->v_mount, tnode, vpp);
275 done:
276 /*
277 * Cache the result, unless request was for creation (as it does
278 * not improve the performance).
279 */
280 if ((cnp->cn_flags & MAKEENTRY) != 0 && cnp->cn_nameiop != CREATE) {
281 cache_enter(dvp, *vpp, cnp);
282 }
283 out:
284 KASSERT((*vpp && VOP_ISLOCKED(*vpp)) || error);
285 KASSERT(VOP_ISLOCKED(dvp));
286
287 return error;
288 }
289
290 int
291 tmpfs_create(void *v)
292 {
293 struct vop_create_args /* {
294 struct vnode *a_dvp;
295 struct vnode **a_vpp;
296 struct componentname *a_cnp;
297 struct vattr *a_vap;
298 } */ *ap = v;
299 vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
300 struct componentname *cnp = ap->a_cnp;
301 struct vattr *vap = ap->a_vap;
302
303 KASSERT(VOP_ISLOCKED(dvp));
304 KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
305 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
306 }
307
308 int
309 tmpfs_mknod(void *v)
310 {
311 struct vop_mknod_args /* {
312 struct vnode *a_dvp;
313 struct vnode **a_vpp;
314 struct componentname *a_cnp;
315 struct vattr *a_vap;
316 } */ *ap = v;
317 vnode_t *dvp = ap->a_dvp, **vpp = ap->a_vpp;
318 struct componentname *cnp = ap->a_cnp;
319 struct vattr *vap = ap->a_vap;
320 enum vtype vt = vap->va_type;
321
322 if (vt != VBLK && vt != VCHR && vt != VFIFO) {
323 vput(dvp);
324 return EINVAL;
325 }
326 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
327 }
328
329 int
330 tmpfs_open(void *v)
331 {
332 struct vop_open_args /* {
333 struct vnode *a_vp;
334 int a_mode;
335 kauth_cred_t a_cred;
336 } */ *ap = v;
337 vnode_t *vp = ap->a_vp;
338 mode_t mode = ap->a_mode;
339 tmpfs_node_t *node;
340
341 KASSERT(VOP_ISLOCKED(vp));
342
343 node = VP_TO_TMPFS_NODE(vp);
344 if (node->tn_links < 1) {
345 /*
346 * The file is still active, but all its names have been
347 * removed (e.g. by a "rmdir $(pwd)"). It cannot be opened
348 * any more, as it is about to be destroyed.
349 */
350 return ENOENT;
351 }
352
353 /* If the file is marked append-only, deny write requests. */
354 if ((node->tn_flags & APPEND) != 0 &&
355 (mode & (FWRITE | O_APPEND)) == FWRITE) {
356 return EPERM;
357 }
358 return 0;
359 }
360
361 int
362 tmpfs_close(void *v)
363 {
364 struct vop_close_args /* {
365 struct vnode *a_vp;
366 int a_fflag;
367 kauth_cred_t a_cred;
368 } */ *ap = v;
369 vnode_t *vp = ap->a_vp;
370
371 KASSERT(VOP_ISLOCKED(vp));
372
373 tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
374 return 0;
375 }
376
377 int
378 tmpfs_access(void *v)
379 {
380 struct vop_access_args /* {
381 struct vnode *a_vp;
382 int a_mode;
383 kauth_cred_t a_cred;
384 } */ *ap = v;
385 vnode_t *vp = ap->a_vp;
386 mode_t mode = ap->a_mode;
387 kauth_cred_t cred = ap->a_cred;
388 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
389 const bool writing = (mode & VWRITE) != 0;
390
391 KASSERT(VOP_ISLOCKED(vp));
392
393 /* Possible? */
394 switch (vp->v_type) {
395 case VDIR:
396 case VLNK:
397 case VREG:
398 if (writing && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
399 return EROFS;
400 }
401 break;
402 case VBLK:
403 case VCHR:
404 case VSOCK:
405 case VFIFO:
406 break;
407 default:
408 return EINVAL;
409 }
410 if (writing && (node->tn_flags & IMMUTABLE) != 0) {
411 return EPERM;
412 }
413
414 return kauth_authorize_vnode(cred, kauth_access_action(mode,
415 vp->v_type, node->tn_mode), vp, NULL, genfs_can_access(vp->v_type,
416 node->tn_mode, node->tn_uid, node->tn_gid, mode, cred));
417 }
418
419 int
420 tmpfs_getattr(void *v)
421 {
422 struct vop_getattr_args /* {
423 struct vnode *a_vp;
424 struct vattr *a_vap;
425 kauth_cred_t a_cred;
426 } */ *ap = v;
427 vnode_t *vp = ap->a_vp;
428 struct vattr *vap = ap->a_vap;
429 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
430
431 vattr_null(vap);
432
433 tmpfs_update(vp, NULL, NULL, NULL, 0);
434
435 vap->va_type = vp->v_type;
436 vap->va_mode = node->tn_mode;
437 vap->va_nlink = node->tn_links;
438 vap->va_uid = node->tn_uid;
439 vap->va_gid = node->tn_gid;
440 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
441 vap->va_fileid = node->tn_id;
442 vap->va_size = node->tn_size;
443 vap->va_blocksize = PAGE_SIZE;
444 vap->va_atime = node->tn_atime;
445 vap->va_mtime = node->tn_mtime;
446 vap->va_ctime = node->tn_ctime;
447 vap->va_birthtime = node->tn_birthtime;
448 vap->va_gen = TMPFS_NODE_GEN(node);
449 vap->va_flags = node->tn_flags;
450 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
451 node->tn_spec.tn_dev.tn_rdev : VNOVAL;
452 vap->va_bytes = round_page(node->tn_size);
453 vap->va_filerev = VNOVAL;
454 vap->va_vaflags = 0;
455 vap->va_spare = VNOVAL; /* XXX */
456
457 return 0;
458 }
459
460 #define GOODTIME(tv) ((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
461 /* XXX Should this operation be atomic? I think it should, but code in
462 * XXX other places (e.g., ufs) doesn't seem to be... */
463 int
464 tmpfs_setattr(void *v)
465 {
466 struct vop_setattr_args /* {
467 struct vnode *a_vp;
468 struct vattr *a_vap;
469 kauth_cred_t a_cred;
470 } */ *ap = v;
471 vnode_t *vp = ap->a_vp;
472 struct vattr *vap = ap->a_vap;
473 kauth_cred_t cred = ap->a_cred;
474 lwp_t *l = curlwp;
475 int error = 0;
476
477 KASSERT(VOP_ISLOCKED(vp));
478
479 /* Abort if any unsettable attribute is given. */
480 if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
481 vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
482 vap->va_blocksize != VNOVAL || GOODTIME(&vap->va_ctime) ||
483 vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
484 vap->va_bytes != VNOVAL) {
485 return EINVAL;
486 }
487 if (error == 0 && (vap->va_flags != VNOVAL))
488 error = tmpfs_chflags(vp, vap->va_flags, cred, l);
489
490 if (error == 0 && (vap->va_size != VNOVAL))
491 error = tmpfs_chsize(vp, vap->va_size, cred, l);
492
493 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
494 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
495
496 if (error == 0 && (vap->va_mode != VNOVAL))
497 error = tmpfs_chmod(vp, vap->va_mode, cred, l);
498
499 if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
500 || GOODTIME(&vap->va_birthtime))) {
501 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
502 &vap->va_birthtime, vap->va_vaflags, cred, l);
503 if (error == 0)
504 return 0;
505 }
506 tmpfs_update(vp, NULL, NULL, NULL, 0);
507 return error;
508 }
509
510 int
511 tmpfs_read(void *v)
512 {
513 struct vop_read_args /* {
514 struct vnode *a_vp;
515 struct uio *a_uio;
516 int a_ioflag;
517 kauth_cred_t a_cred;
518 } */ *ap = v;
519 vnode_t *vp = ap->a_vp;
520 struct uio *uio = ap->a_uio;
521 const int ioflag = ap->a_ioflag;
522 tmpfs_node_t *node;
523 struct uvm_object *uobj;
524 const int advice = IO_ADV_DECODE(ioflag);
525 int error;
526
527 KASSERT(VOP_ISLOCKED(vp));
528
529 if (vp->v_type != VREG) {
530 return EISDIR;
531 }
532 if (uio->uio_offset < 0) {
533 return EINVAL;
534 }
535
536 node = VP_TO_TMPFS_NODE(vp);
537 node->tn_status |= TMPFS_NODE_ACCESSED;
538 uobj = node->tn_spec.tn_reg.tn_aobj;
539 error = 0;
540
541 if (uio->uio_offset + uio->uio_resid <= node->tn_size) {
542 uvm_loanobj(&vp->v_uobj, uio, advice);
543 }
544 while (error == 0 && uio->uio_resid > 0) {
545 vsize_t len;
546
547 if (node->tn_size <= uio->uio_offset) {
548 break;
549 }
550 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
551 if (len == 0) {
552 break;
553 }
554 error = ubc_uiomove(uobj, uio, len, advice,
555 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
556 }
557 return error;
558 }
559
560 int
561 tmpfs_write(void *v)
562 {
563 struct vop_write_args /* {
564 struct vnode *a_vp;
565 struct uio *a_uio;
566 int a_ioflag;
567 kauth_cred_t a_cred;
568 } */ *ap = v;
569 vnode_t *vp = ap->a_vp;
570 struct uio *uio = ap->a_uio;
571 const int ioflag = ap->a_ioflag;
572 tmpfs_node_t *node;
573 struct uvm_object *uobj;
574 off_t oldsize;
575 bool extended;
576 int error;
577
578 KASSERT(VOP_ISLOCKED(vp));
579
580 node = VP_TO_TMPFS_NODE(vp);
581 oldsize = node->tn_size;
582
583 if (uio->uio_offset < 0 || vp->v_type != VREG) {
584 error = EINVAL;
585 goto out;
586 }
587 if (uio->uio_resid == 0) {
588 error = 0;
589 goto out;
590 }
591 if (ioflag & IO_APPEND) {
592 uio->uio_offset = node->tn_size;
593 }
594
595 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
596 if (extended) {
597 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
598 if (error)
599 goto out;
600 }
601
602 uobj = node->tn_spec.tn_reg.tn_aobj;
603 error = 0;
604 while (error == 0 && uio->uio_resid > 0) {
605 vsize_t len;
606
607 len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
608 if (len == 0) {
609 break;
610 }
611 error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
612 UBC_WRITE | UBC_UNMAP_FLAG(vp));
613 }
614 if (error) {
615 (void)tmpfs_reg_resize(vp, oldsize);
616 }
617
618 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
619 (extended ? TMPFS_NODE_CHANGED : 0);
620 VN_KNOTE(vp, NOTE_WRITE);
621 out:
622 if (error) {
623 KASSERT(oldsize == node->tn_size);
624 } else {
625 KASSERT(uio->uio_resid == 0);
626 }
627 return error;
628 }
629
630 int
631 tmpfs_fsync(void *v)
632 {
633 struct vop_fsync_args /* {
634 struct vnode *a_vp;
635 kauth_cred_t a_cred;
636 int a_flags;
637 off_t a_offlo;
638 off_t a_offhi;
639 struct lwp *a_l;
640 } */ *ap = v;
641 vnode_t *vp = ap->a_vp;
642
643 /* Nothing to do. Just update. */
644 KASSERT(VOP_ISLOCKED(vp));
645 tmpfs_update(vp, NULL, NULL, NULL, 0);
646 return 0;
647 }
648
649 /*
650 * tmpfs_remove: unlink a file.
651 *
652 * => Both directory (dvp) and file (vp) are locked.
653 * => We unlock and drop the reference on both.
654 */
655 int
656 tmpfs_remove(void *v)
657 {
658 struct vop_remove_args /* {
659 struct vnode *a_dvp;
660 struct vnode *a_vp;
661 struct componentname *a_cnp;
662 } */ *ap = v;
663 vnode_t *dvp = ap->a_dvp, *vp = ap->a_vp;
664 tmpfs_node_t *node;
665 tmpfs_dirent_t *de;
666 int error;
667
668 KASSERT(VOP_ISLOCKED(dvp));
669 KASSERT(VOP_ISLOCKED(vp));
670
671 if (vp->v_type == VDIR) {
672 error = EPERM;
673 goto out;
674 }
675 node = VP_TO_TMPFS_NODE(vp);
676
677 /* Files marked as immutable or append-only cannot be deleted. */
678 if (node->tn_flags & (IMMUTABLE | APPEND)) {
679 error = EPERM;
680 goto out;
681 }
682
683 /* Lookup the directory entry (check the cached hint first). */
684 de = tmpfs_dir_cached(node);
685 if (de == NULL) {
686 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
687 struct componentname *cnp = ap->a_cnp;
688 de = tmpfs_dir_lookup(dnode, cnp);
689 }
690 KASSERT(de && de->td_node == node);
691
692 /*
693 * Remove the entry from the directory (drops the link count) and
694 * destroy it or replace it with a whiteout.
695 * Note: the inode referred by it will not be destroyed
696 * until the vnode is reclaimed/recycled.
697 */
698 tmpfs_dir_detach(dvp, de);
699 if (ap->a_cnp->cn_flags & DOWHITEOUT)
700 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
701 else
702 tmpfs_free_dirent(VFS_TO_TMPFS(vp->v_mount), de);
703 error = 0;
704 out:
705 /* Drop the references and unlock the vnodes. */
706 vput(vp);
707 if (dvp == vp) {
708 vrele(dvp);
709 } else {
710 vput(dvp);
711 }
712 return error;
713 }
714
715 /*
716 * tmpfs_link: create a hard link.
717 */
718 int
719 tmpfs_link(void *v)
720 {
721 struct vop_link_args /* {
722 struct vnode *a_dvp;
723 struct vnode *a_vp;
724 struct componentname *a_cnp;
725 } */ *ap = v;
726 vnode_t *dvp = ap->a_dvp;
727 vnode_t *vp = ap->a_vp;
728 struct componentname *cnp = ap->a_cnp;
729 tmpfs_node_t *dnode, *node;
730 tmpfs_dirent_t *de;
731 int error;
732
733 KASSERT(dvp != vp);
734 KASSERT(VOP_ISLOCKED(dvp));
735 KASSERT(vp->v_type != VDIR);
736 KASSERT(dvp->v_mount == vp->v_mount);
737
738 dnode = VP_TO_TMPFS_DIR(dvp);
739 node = VP_TO_TMPFS_NODE(vp);
740
741 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
742
743 /* Check for maximum number of links limit. */
744 if (node->tn_links == LINK_MAX) {
745 error = EMLINK;
746 goto out;
747 }
748 KASSERT(node->tn_links < LINK_MAX);
749
750 /* We cannot create links of files marked immutable or append-only. */
751 if (node->tn_flags & (IMMUTABLE | APPEND)) {
752 error = EPERM;
753 goto out;
754 }
755
756 /* Allocate a new directory entry to represent the inode. */
757 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount),
758 cnp->cn_nameptr, cnp->cn_namelen, &de);
759 if (error) {
760 goto out;
761 }
762
763 /*
764 * Insert the entry into the directory.
765 * It will increase the inode link count.
766 */
767 tmpfs_dir_attach(dvp, de, node);
768
769 /* Update the timestamps and trigger the event. */
770 if (node->tn_vnode) {
771 VN_KNOTE(node->tn_vnode, NOTE_LINK);
772 }
773 node->tn_status |= TMPFS_NODE_CHANGED;
774 tmpfs_update(vp, NULL, NULL, NULL, 0);
775 error = 0;
776 out:
777 VOP_UNLOCK(vp);
778 vput(dvp);
779 return error;
780 }
781
782 /*
783 * tmpfs_rename: rename routine, the hairiest system call, with the
784 * insane API.
785 *
786 * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
787 * and tvp (to-leaf), if exists (NULL if not).
788 *
789 * => Caller holds a reference on fdvp and fvp, they are unlocked.
790 * Note: fdvp and fvp can refer to the same object (i.e. when it is root).
791 *
792 * => Both tdvp and tvp are referenced and locked. It is our responsibility
793 * to release the references and unlock them (or destroy).
794 */
795
796 /*
797 * First, some forward declarations of subroutines.
798 */
799
800 static int tmpfs_sane_rename(struct vnode *, struct componentname *,
801 struct vnode *, struct componentname *, kauth_cred_t, bool);
802 static int tmpfs_rename_enter(struct mount *, struct tmpfs_mount *,
803 kauth_cred_t,
804 struct vnode *, struct tmpfs_node *, struct componentname *,
805 struct tmpfs_dirent **, struct vnode **,
806 struct vnode *, struct tmpfs_node *, struct componentname *,
807 struct tmpfs_dirent **, struct vnode **);
808 static int tmpfs_rename_enter_common(struct mount *, struct tmpfs_mount *,
809 kauth_cred_t,
810 struct vnode *, struct tmpfs_node *,
811 struct componentname *, struct tmpfs_dirent **, struct vnode **,
812 struct componentname *, struct tmpfs_dirent **, struct vnode **);
813 static int tmpfs_rename_enter_separate(struct mount *, struct tmpfs_mount *,
814 kauth_cred_t,
815 struct vnode *, struct tmpfs_node *, struct componentname *,
816 struct tmpfs_dirent **, struct vnode **,
817 struct vnode *, struct tmpfs_node *, struct componentname *,
818 struct tmpfs_dirent **, struct vnode **);
819 static void tmpfs_rename_exit(struct tmpfs_mount *,
820 struct vnode *, struct vnode *, struct vnode *, struct vnode *);
821 static int tmpfs_rename_lock_directory(struct vnode *, struct tmpfs_node *);
822 static int tmpfs_rename_genealogy(struct tmpfs_node *, struct tmpfs_node *,
823 struct tmpfs_node **);
824 static int tmpfs_rename_lock(struct mount *, kauth_cred_t, int,
825 struct vnode *, struct tmpfs_node *, struct componentname *, bool,
826 struct tmpfs_dirent **, struct vnode **,
827 struct vnode *, struct tmpfs_node *, struct componentname *, bool,
828 struct tmpfs_dirent **, struct vnode **);
829 static void tmpfs_rename_attachdetach(struct tmpfs_mount *,
830 struct vnode *, struct tmpfs_dirent *, struct vnode *,
831 struct vnode *, struct tmpfs_dirent *, struct vnode *);
832 static int tmpfs_do_remove(struct tmpfs_mount *, struct vnode *,
833 struct tmpfs_node *, struct tmpfs_dirent *, struct vnode *, kauth_cred_t);
834 static int tmpfs_rename_check_possible(struct tmpfs_node *,
835 struct tmpfs_node *, struct tmpfs_node *, struct tmpfs_node *);
836 static int tmpfs_rename_check_permitted(kauth_cred_t,
837 struct tmpfs_node *, struct tmpfs_node *,
838 struct tmpfs_node *, struct tmpfs_node *);
839 static int tmpfs_remove_check_possible(struct tmpfs_node *,
840 struct tmpfs_node *);
841 static int tmpfs_remove_check_permitted(kauth_cred_t,
842 struct tmpfs_node *, struct tmpfs_node *);
843 static int tmpfs_check_sticky(kauth_cred_t,
844 struct tmpfs_node *, struct tmpfs_node *);
845
846 int
847 tmpfs_rename(void *v)
848 {
849 struct vop_rename_args /* {
850 struct vnode *a_fdvp;
851 struct vnode *a_fvp;
852 struct componentname *a_fcnp;
853 struct vnode *a_tdvp;
854 struct vnode *a_tvp;
855 struct componentname *a_tcnp;
856 } */ *ap = v;
857 struct vnode *fdvp = ap->a_fdvp;
858 struct vnode *fvp = ap->a_fvp;
859 struct componentname *fcnp = ap->a_fcnp;
860 struct vnode *tdvp = ap->a_tdvp;
861 struct vnode *tvp = ap->a_tvp;
862 struct componentname *tcnp = ap->a_tcnp;
863 kauth_cred_t cred;
864 int error;
865
866 KASSERT(fdvp != NULL);
867 KASSERT(fvp != NULL);
868 KASSERT(fcnp != NULL);
869 KASSERT(fcnp->cn_nameptr != NULL);
870 KASSERT(tdvp != NULL);
871 KASSERT(tcnp != NULL);
872 KASSERT(fcnp->cn_nameptr != NULL);
873 /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
874 /* KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
875 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
876 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
877 KASSERT(fdvp->v_type == VDIR);
878 KASSERT(tdvp->v_type == VDIR);
879
880 cred = fcnp->cn_cred;
881 KASSERT(tcnp->cn_cred == cred);
882
883 /*
884 * Sanitize our world from the VFS insanity. Unlock the target
885 * directory and node, which are locked. Release the children,
886 * which are referenced. Check for rename("x", "y/."), which
887 * it is our responsibility to reject, not the caller's. (But
888 * the caller does reject rename("x/.", "y"). Go figure.)
889 */
890
891 VOP_UNLOCK(tdvp);
892 if ((tvp != NULL) && (tvp != tdvp))
893 VOP_UNLOCK(tvp);
894
895 vrele(fvp);
896 if (tvp != NULL)
897 vrele(tvp);
898
899 if (tvp == tdvp) {
900 error = EINVAL;
901 goto out;
902 }
903
904 error = tmpfs_sane_rename(fdvp, fcnp, tdvp, tcnp, cred, false);
905
906 out: /*
907 * All done, whether with success or failure. Release the
908 * directory nodes now, as the caller expects from the VFS
909 * protocol.
910 */
911 vrele(fdvp);
912 vrele(tdvp);
913
914 return error;
915 }
916
917 /*
918 * tmpfs_sane_rename: rename routine, the hairiest system call, with
919 * the sane API.
920 *
921 * Arguments:
922 *
923 * . fdvp (from directory vnode),
924 * . fcnp (from component name),
925 * . tdvp (to directory vnode), and
926 * . tcnp (to component name).
927 *
928 * fdvp and tdvp must be referenced and unlocked.
929 */
930 static int
931 tmpfs_sane_rename(struct vnode *fdvp, struct componentname *fcnp,
932 struct vnode *tdvp, struct componentname *tcnp, kauth_cred_t cred,
933 bool posixly_correct)
934 {
935 struct mount *mount;
936 struct tmpfs_mount *tmpfs;
937 struct tmpfs_node *fdnode, *tdnode;
938 struct tmpfs_dirent *fde, *tde;
939 struct vnode *fvp, *tvp;
940 char *newname;
941 int error;
942
943 KASSERT(fdvp != NULL);
944 KASSERT(fcnp != NULL);
945 KASSERT(tdvp != NULL);
946 KASSERT(tcnp != NULL);
947 /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
948 /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
949 KASSERT(fdvp->v_type == VDIR);
950 KASSERT(tdvp->v_type == VDIR);
951 KASSERT(fdvp->v_mount == tdvp->v_mount);
952 KASSERT((fcnp->cn_flags & ISDOTDOT) == 0);
953 KASSERT((tcnp->cn_flags & ISDOTDOT) == 0);
954 KASSERT((fcnp->cn_namelen != 1) || (fcnp->cn_nameptr[0] != '.'));
955 KASSERT((tcnp->cn_namelen != 1) || (tcnp->cn_nameptr[0] != '.'));
956 KASSERT((fcnp->cn_namelen != 2) || (fcnp->cn_nameptr[0] != '.') ||
957 (fcnp->cn_nameptr[1] != '.'));
958 KASSERT((tcnp->cn_namelen != 2) || (tcnp->cn_nameptr[0] != '.') ||
959 (tcnp->cn_nameptr[1] != '.'));
960
961 /*
962 * Pull out the tmpfs data structures.
963 */
964 fdnode = VP_TO_TMPFS_NODE(fdvp);
965 tdnode = VP_TO_TMPFS_NODE(tdvp);
966 KASSERT(fdnode != NULL);
967 KASSERT(tdnode != NULL);
968 KASSERT(fdnode->tn_vnode == fdvp);
969 KASSERT(tdnode->tn_vnode == tdvp);
970 KASSERT(fdnode->tn_type == VDIR);
971 KASSERT(tdnode->tn_type == VDIR);
972
973 mount = fdvp->v_mount;
974 KASSERT(mount != NULL);
975 KASSERT(mount == tdvp->v_mount);
976 /* XXX How can we be sure this stays true? (Not that you're
977 * likely to mount a tmpfs read-only...) */
978 KASSERT((mount->mnt_flag & MNT_RDONLY) == 0);
979 tmpfs = VFS_TO_TMPFS(mount);
980 KASSERT(tmpfs != NULL);
981
982 /*
983 * Decide whether we need a new name, and allocate memory for
984 * it if so. Do this before locking anything or taking
985 * destructive actions so that we can back out safely and sleep
986 * safely. XXX Is sleeping an issue here? Can this just be
987 * moved into tmpfs_rename_attachdetach?
988 */
989 if (tmpfs_strname_neqlen(fcnp, tcnp)) {
990 newname = tmpfs_strname_alloc(tmpfs, tcnp->cn_namelen);
991 if (newname == NULL) {
992 error = ENOSPC;
993 goto out_unlocked;
994 }
995 } else {
996 newname = NULL;
997 }
998
999 /*
1000 * Lock and look up everything. GCC is not very clever.
1001 */
1002 fde = tde = NULL;
1003 fvp = tvp = NULL;
1004 error = tmpfs_rename_enter(mount, tmpfs, cred,
1005 fdvp, fdnode, fcnp, &fde, &fvp,
1006 tdvp, tdnode, tcnp, &tde, &tvp);
1007 if (error)
1008 goto out_unlocked;
1009
1010 /*
1011 * Check that everything is locked and looks right.
1012 */
1013 KASSERT(fde != NULL);
1014 KASSERT(fvp != NULL);
1015 KASSERT(fde->td_node != NULL);
1016 KASSERT(fde->td_node->tn_vnode == fvp);
1017 KASSERT(fde->td_node->tn_type == fvp->v_type);
1018 KASSERT((tde == NULL) == (tvp == NULL));
1019 KASSERT((tde == NULL) || (tde->td_node != NULL));
1020 KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
1021 KASSERT((tde == NULL) || (tde->td_node->tn_type == tvp->v_type));
1022 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1023 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1024 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1025 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1026
1027 /*
1028 * If the source and destination are the same object, we need
1029 * only at most delete the source entry.
1030 */
1031 if (fvp == tvp) {
1032 KASSERT(tvp != NULL);
1033 if (fde->td_node->tn_type == VDIR) {
1034 /* XXX How can this possibly happen? */
1035 error = EINVAL;
1036 goto out_locked;
1037 }
1038 if (!posixly_correct && (fde != tde)) {
1039 /* XXX Doesn't work because of locking.
1040 * error = VOP_REMOVE(fdvp, fvp);
1041 */
1042 error = tmpfs_do_remove(tmpfs, fdvp, fdnode, fde, fvp,
1043 cred);
1044 if (error)
1045 goto out_locked;
1046 }
1047 goto success;
1048 }
1049 KASSERT(fde != tde);
1050 KASSERT(fvp != tvp);
1051
1052 /*
1053 * If the target exists, refuse to rename a directory over a
1054 * non-directory or vice versa, or to clobber a non-empty
1055 * directory.
1056 */
1057 if (tvp != NULL) {
1058 KASSERT(tde != NULL);
1059 KASSERT(tde->td_node != NULL);
1060 if (fvp->v_type == VDIR && tvp->v_type == VDIR)
1061 error = ((tde->td_node->tn_size > 0)? ENOTEMPTY : 0);
1062 else if (fvp->v_type == VDIR && tvp->v_type != VDIR)
1063 error = ENOTDIR;
1064 else if (fvp->v_type != VDIR && tvp->v_type == VDIR)
1065 error = EISDIR;
1066 else
1067 error = 0;
1068 if (error)
1069 goto out_locked;
1070 KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
1071 }
1072
1073 /*
1074 * Authorize the rename.
1075 */
1076 error = tmpfs_rename_check_possible(fdnode, fde->td_node,
1077 tdnode, (tde? tde->td_node : NULL));
1078 if (error)
1079 goto out_locked;
1080 error = tmpfs_rename_check_permitted(cred, fdnode, fde->td_node,
1081 tdnode, (tde? tde->td_node : NULL));
1082 error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, fvp, fdvp,
1083 error);
1084 error = kauth_authorize_vnode(cred, KAUTH_VNODE_RENAME, tvp, tdvp,
1085 error);
1086 if (error)
1087 goto out_locked;
1088
1089 /*
1090 * Everything is hunky-dory. Shuffle the directory entries.
1091 */
1092 tmpfs_rename_attachdetach(tmpfs, fdvp, fde, fvp, tdvp, tde, tvp);
1093
1094 /*
1095 * Update the directory entry's name necessary, and flag
1096 * metadata updates. A memory allocation failure here is not
1097 * OK because we've already committed some changes that we
1098 * can't back out at this point, and we have things locked so
1099 * we can't sleep, hence the early allocation above.
1100 */
1101 if (newname != NULL) {
1102 KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
1103
1104 tmpfs_strname_free(tmpfs, fde->td_name, fde->td_namelen);
1105 fde->td_namelen = (uint16_t)tcnp->cn_namelen;
1106 (void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1107 /* Commit newname and don't free it on the way out. */
1108 fde->td_name = newname;
1109 newname = NULL;
1110
1111 fde->td_node->tn_status |= TMPFS_NODE_CHANGED;
1112 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1113 }
1114
1115 success:
1116 VN_KNOTE(fvp, NOTE_RENAME);
1117 error = 0;
1118
1119 out_locked:
1120 tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
1121
1122 out_unlocked:
1123 /* KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
1124 /* KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
1125 /* KASSERT((fvp == NULL) || (VOP_ISLOCKED(fvp) != LK_EXCLUSIVE)); */
1126 /* KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
1127
1128 if (newname != NULL)
1129 tmpfs_strname_free(tmpfs, newname, tcnp->cn_namelen);
1130
1131 return error;
1132 }
1133
1134 /*
1135 * Look up fcnp in fdnode/fdvp and store its directory entry in fde_ret
1136 * and the associated vnode in fvp_ret; fail if not found. Look up
1137 * tcnp in tdnode/tdvp and store its directory entry in tde_ret and the
1138 * associated vnode in tvp_ret; store null instead if not found. Fail
1139 * if anything has been mounted on any of the nodes involved.
1140 *
1141 * fdvp and tdvp must be referenced.
1142 *
1143 * On entry, nothing is locked.
1144 *
1145 * On success, everything is locked, and *fvp_ret, and *tvp_ret if
1146 * nonnull, are referenced. The only pairs of vnodes that may be
1147 * identical are {fdvp, tdvp} and {fvp, tvp}.
1148 *
1149 * On failure, everything remains as was.
1150 *
1151 * Locking everything including the source and target nodes is
1152 * necessary to make sure that, e.g., link count updates are OK. The
1153 * locking order is, in general, ancestor-first, matching the order you
1154 * need to use to look up a descendant anyway.
1155 */
1156 static int
1157 tmpfs_rename_enter(struct mount *mount, struct tmpfs_mount *tmpfs,
1158 kauth_cred_t cred,
1159 struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
1160 struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1161 struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
1162 struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1163 {
1164 int error;
1165
1166 KASSERT(mount != NULL);
1167 KASSERT(tmpfs != NULL);
1168 KASSERT(fdvp != NULL);
1169 KASSERT(fdnode != NULL);
1170 KASSERT(fcnp != NULL);
1171 KASSERT(fde_ret != NULL);
1172 KASSERT(fvp_ret != NULL);
1173 KASSERT(tdvp != NULL);
1174 KASSERT(tdnode != NULL);
1175 KASSERT(tcnp != NULL);
1176 KASSERT(tde_ret != NULL);
1177 KASSERT(tvp_ret != NULL);
1178 KASSERT(fdnode->tn_vnode == fdvp);
1179 KASSERT(tdnode->tn_vnode == tdvp);
1180 KASSERT(fdnode->tn_type == VDIR);
1181 KASSERT(tdnode->tn_type == VDIR);
1182
1183 if (fdvp == tdvp) {
1184 KASSERT(fdnode == tdnode);
1185 error = tmpfs_rename_enter_common(mount, tmpfs, cred, fdvp,
1186 fdnode, fcnp, fde_ret, fvp_ret, tcnp, tde_ret, tvp_ret);
1187 } else {
1188 KASSERT(fdnode != tdnode);
1189 error = tmpfs_rename_enter_separate(mount, tmpfs, cred,
1190 fdvp, fdnode, fcnp, fde_ret, fvp_ret,
1191 tdvp, tdnode, tcnp, tde_ret, tvp_ret);
1192 }
1193
1194 if (error)
1195 return error;
1196
1197 KASSERT(*fde_ret != NULL);
1198 KASSERT(*fvp_ret != NULL);
1199 KASSERT((*tde_ret == NULL) == (*tvp_ret == NULL));
1200 KASSERT((*tde_ret == NULL) || ((*tde_ret)->td_node != NULL));
1201 KASSERT((*tde_ret == NULL) ||
1202 ((*tde_ret)->td_node->tn_vnode == *tvp_ret));
1203 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1204 KASSERT(VOP_ISLOCKED(*fvp_ret) == LK_EXCLUSIVE);
1205 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1206 KASSERT((*tvp_ret == NULL) ||
1207 (VOP_ISLOCKED(*tvp_ret) == LK_EXCLUSIVE));
1208 KASSERT(*fvp_ret != fdvp);
1209 KASSERT(*fvp_ret != tdvp);
1210 KASSERT(*tvp_ret != fdvp);
1211 KASSERT(*tvp_ret != tdvp);
1212 return 0;
1213 }
1214
1215 /*
1216 * Lock and look up with a common source/target directory.
1217 */
1218 static int
1219 tmpfs_rename_enter_common(struct mount *mount, struct tmpfs_mount *tmpfs,
1220 kauth_cred_t cred,
1221 struct vnode *dvp, struct tmpfs_node *dnode,
1222 struct componentname *fcnp,
1223 struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1224 struct componentname *tcnp,
1225 struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1226 {
1227 struct tmpfs_dirent *fde, *tde;
1228 struct vnode *fvp, *tvp;
1229 int error;
1230
1231 error = tmpfs_rename_lock_directory(dvp, dnode);
1232 if (error)
1233 goto fail0;
1234
1235 /* Did we lose a race with mount? */
1236 if (dvp->v_mountedhere != NULL) {
1237 error = EBUSY;
1238 goto fail1;
1239 }
1240
1241 /* Make sure the caller may read the directory. */
1242 error = VOP_ACCESS(dvp, VEXEC, cred);
1243 if (error)
1244 goto fail1;
1245
1246 /*
1247 * The order in which we lock the source and target nodes is
1248 * irrelevant because there can only be one rename on this
1249 * directory in flight at a time, and we have it locked.
1250 */
1251
1252 fde = tmpfs_dir_lookup(dnode, fcnp);
1253 if (fde == NULL) {
1254 error = ENOENT;
1255 goto fail1;
1256 }
1257
1258 KASSERT(fde->td_node != NULL);
1259 /* We ruled out `.' earlier. */
1260 KASSERT(fde->td_node != dnode);
1261 /* We ruled out `..' earlier. */
1262 KASSERT(fde->td_node != dnode->tn_spec.tn_dir.tn_parent);
1263 mutex_enter(&fde->td_node->tn_vlock);
1264 error = tmpfs_vnode_get(mount, fde->td_node, &fvp);
1265 if (error)
1266 goto fail1;
1267 KASSERT(fvp != NULL);
1268 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1269 KASSERT(fvp != dvp);
1270 KASSERT(fvp->v_mount == mount);
1271
1272 /* Refuse to rename a mount point. */
1273 if ((fvp->v_type == VDIR) && (fvp->v_mountedhere != NULL)) {
1274 error = EBUSY;
1275 goto fail2;
1276 }
1277
1278 tde = tmpfs_dir_lookup(dnode, tcnp);
1279 if (tde == NULL) {
1280 tvp = NULL;
1281 } else {
1282 KASSERT(tde->td_node != NULL);
1283 /* We ruled out `.' earlier. */
1284 KASSERT(tde->td_node != dnode);
1285 /* We ruled out `..' earlier. */
1286 KASSERT(tde->td_node != dnode->tn_spec.tn_dir.tn_parent);
1287 if (tde->td_node != fde->td_node) {
1288 mutex_enter(&tde->td_node->tn_vlock);
1289 error = tmpfs_vnode_get(mount, tde->td_node, &tvp);
1290 if (error)
1291 goto fail2;
1292 KASSERT(tvp->v_mount == mount);
1293 /* Refuse to rename over a mount point. */
1294 if ((tvp->v_type == VDIR) &&
1295 (tvp->v_mountedhere != NULL)) {
1296 error = EBUSY;
1297 goto fail3;
1298 }
1299 } else {
1300 tvp = fvp;
1301 vref(tvp);
1302 }
1303 KASSERT(tvp != NULL);
1304 KASSERT(VOP_ISLOCKED(tvp) == LK_EXCLUSIVE);
1305 }
1306 KASSERT(tvp != dvp);
1307
1308 *fde_ret = fde;
1309 *fvp_ret = fvp;
1310 *tde_ret = tde;
1311 *tvp_ret = tvp;
1312 return 0;
1313
1314 fail3: if (tvp != NULL) {
1315 if (tvp != fvp)
1316 vput(tvp);
1317 else
1318 vrele(tvp);
1319 }
1320
1321 fail2: vput(fvp);
1322 fail1: VOP_UNLOCK(dvp);
1323 fail0: return error;
1324 }
1325
1326 /*
1327 * Lock and look up with separate source and target directories.
1328 */
1329 static int
1330 tmpfs_rename_enter_separate(struct mount *mount, struct tmpfs_mount *tmpfs,
1331 kauth_cred_t cred,
1332 struct vnode *fdvp, struct tmpfs_node *fdnode, struct componentname *fcnp,
1333 struct tmpfs_dirent **fde_ret, struct vnode **fvp_ret,
1334 struct vnode *tdvp, struct tmpfs_node *tdnode, struct componentname *tcnp,
1335 struct tmpfs_dirent **tde_ret, struct vnode **tvp_ret)
1336 {
1337 struct tmpfs_node *intermediate_node;
1338 struct tmpfs_dirent *fde, *tde;
1339 struct vnode *fvp, *tvp;
1340 int error;
1341
1342 KASSERT(fdvp != tdvp);
1343 KASSERT(fdnode != tdnode);
1344
1345 #if 0 /* XXX */
1346 mutex_enter(&tmpfs->tm_rename_lock);
1347 #endif
1348
1349 error = tmpfs_rename_genealogy(fdnode, tdnode, &intermediate_node);
1350 if (error)
1351 goto fail;
1352
1353 /*
1354 * intermediate_node == NULL means fdnode is not an ancestor of
1355 * tdnode.
1356 */
1357 if (intermediate_node == NULL)
1358 error = tmpfs_rename_lock(mount, cred, ENOTEMPTY,
1359 tdvp, tdnode, tcnp, true, &tde, &tvp,
1360 fdvp, fdnode, fcnp, false, &fde, &fvp);
1361 else
1362 error = tmpfs_rename_lock(mount, cred, EINVAL,
1363 fdvp, fdnode, fcnp, false, &fde, &fvp,
1364 tdvp, tdnode, tcnp, true, &tde, &tvp);
1365 if (error)
1366 goto fail;
1367
1368 KASSERT(fde != NULL);
1369 KASSERT(fde->td_node != NULL);
1370
1371 /*
1372 * Reject rename("foo/bar", "foo/bar/baz/quux/zot").
1373 */
1374 if (fde->td_node == intermediate_node) {
1375 tmpfs_rename_exit(tmpfs, fdvp, fvp, tdvp, tvp);
1376 return EINVAL;
1377 }
1378
1379 *fde_ret = fde;
1380 *fvp_ret = fvp;
1381 *tde_ret = tde;
1382 *tvp_ret = tvp;
1383 return 0;
1384
1385 fail:
1386 #if 0 /* XXX */
1387 mutex_exit(&tmpfs->tm_rename_lock);
1388 #endif
1389 return error;
1390 }
1391
1392 /*
1393 * Unlock everything we locked for rename.
1394 *
1395 * fdvp and tdvp must be referenced.
1396 *
1397 * On entry, everything is locked, and fvp and tvp referenced.
1398 *
1399 * On exit, everything is unlocked, and fvp and tvp are released.
1400 */
1401 static void
1402 tmpfs_rename_exit(struct tmpfs_mount *tmpfs,
1403 struct vnode *fdvp, struct vnode *fvp,
1404 struct vnode *tdvp, struct vnode *tvp)
1405 {
1406
1407 KASSERT(tmpfs != NULL);
1408 KASSERT(fdvp != NULL);
1409 KASSERT(fvp != NULL);
1410 KASSERT(fdvp != fvp);
1411 KASSERT(fdvp != tvp);
1412 KASSERT(tdvp != tvp);
1413 KASSERT(tdvp != fvp);
1414 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1415 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1416 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1417 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1418
1419 if (tvp != NULL) {
1420 if (tvp != fvp)
1421 vput(tvp);
1422 else
1423 vrele(tvp);
1424 }
1425 VOP_UNLOCK(tdvp);
1426 vput(fvp);
1427 if (fdvp != tdvp)
1428 VOP_UNLOCK(fdvp);
1429
1430 #if 0 /* XXX */
1431 if (fdvp != tdvp)
1432 mutex_exit(&tmpfs->tm_rename_lock);
1433 #endif
1434 }
1435
1436 /*
1437 * Lock a directory, but fail if it has been rmdir'd.
1438 *
1439 * vp must be referenced.
1440 */
1441 static int
1442 tmpfs_rename_lock_directory(struct vnode *vp, struct tmpfs_node *node)
1443 {
1444
1445 KASSERT(vp != NULL);
1446 KASSERT(node != NULL);
1447 KASSERT(node->tn_vnode == vp);
1448 KASSERT(node->tn_type == VDIR);
1449
1450 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1451 if (node->tn_spec.tn_dir.tn_parent == NULL) {
1452 VOP_UNLOCK(vp);
1453 return ENOENT;
1454 }
1455
1456 return 0;
1457 }
1458
1459 /*
1460 * Analyze the genealogy of the source and target nodes.
1461 *
1462 * On success, stores in *intermediate_node_ret either the child of
1463 * fdnode of which tdnode is a descendant, or null if tdnode is not a
1464 * descendant of fdnode at all.
1465 *
1466 * fdnode and tdnode must be unlocked and referenced. The file
1467 * system's rename lock must also be held, to exclude concurrent
1468 * changes to the file system's genealogy other than rmdir.
1469 *
1470 * XXX This causes an extra lock/unlock of tdnode in the case when
1471 * we're just about to lock it again before locking anything else.
1472 * However, changing that requires reorganizing the code to make it
1473 * even more horrifically obscure.
1474 */
1475 static int
1476 tmpfs_rename_genealogy(struct tmpfs_node *fdnode, struct tmpfs_node *tdnode,
1477 struct tmpfs_node **intermediate_node_ret)
1478 {
1479 struct tmpfs_node *node = tdnode, *parent;
1480 int error;
1481
1482 KASSERT(fdnode != NULL);
1483 KASSERT(tdnode != NULL);
1484 KASSERT(fdnode != tdnode);
1485 KASSERT(intermediate_node_ret != NULL);
1486
1487 KASSERT(fdnode->tn_vnode != NULL);
1488 KASSERT(tdnode->tn_vnode != NULL);
1489 KASSERT(fdnode->tn_type == VDIR);
1490 KASSERT(tdnode->tn_type == VDIR);
1491
1492 /*
1493 * We need to provisionally lock tdnode->tn_vnode to keep rmdir
1494 * from deleting it -- or any ancestor -- at an inopportune
1495 * moment.
1496 */
1497 error = tmpfs_rename_lock_directory(tdnode->tn_vnode, tdnode);
1498 if (error)
1499 return error;
1500
1501 for (;;) {
1502 parent = node->tn_spec.tn_dir.tn_parent;
1503 KASSERT(parent != NULL);
1504 KASSERT(parent->tn_type == VDIR);
1505
1506 /* Did we hit the root without finding fdnode? */
1507 if (parent == node) {
1508 *intermediate_node_ret = NULL;
1509 break;
1510 }
1511
1512 /* Did we find that fdnode is an ancestor? */
1513 if (parent == fdnode) {
1514 *intermediate_node_ret = node;
1515 break;
1516 }
1517
1518 /* Neither -- keep ascending the family tree. */
1519 node = parent;
1520 }
1521
1522 VOP_UNLOCK(tdnode->tn_vnode);
1523 return 0;
1524 }
1525
1526 /*
1527 * Lock directories a and b, which must be distinct, and look up and
1528 * lock nodes a and b. Do a first and then b. Directory b may not be
1529 * an ancestor of directory a, although directory a may be an ancestor
1530 * of directory b. Fail with overlap_error if node a is directory b.
1531 * Neither componentname may be `.' or `..'.
1532 *
1533 * a_dvp and b_dvp must be referenced.
1534 *
1535 * On entry, a_dvp and b_dvp are unlocked.
1536 *
1537 * On success,
1538 * . a_dvp and b_dvp are locked,
1539 * . *a_dirent_ret is filled with a directory entry whose node is
1540 * locked and referenced,
1541 * . *b_vp_ret is filled with the corresponding vnode,
1542 * . *b_dirent_ret is filled either with null or with a directory entry
1543 * whose node is locked and referenced,
1544 * . *b_vp is filled either with null or with the corresponding vnode,
1545 * and
1546 * . the only pair of vnodes that may be identical is a_vp and b_vp.
1547 *
1548 * On failure, a_dvp and b_dvp are left unlocked, and *a_dirent_ret,
1549 * *a_vp, *b_dirent_ret, and *b_vp are left alone.
1550 */
1551 static int
1552 tmpfs_rename_lock(struct mount *mount, kauth_cred_t cred, int overlap_error,
1553 struct vnode *a_dvp, struct tmpfs_node *a_dnode,
1554 struct componentname *a_cnp, bool a_missing_ok,
1555 struct tmpfs_dirent **a_dirent_ret, struct vnode **a_vp_ret,
1556 struct vnode *b_dvp, struct tmpfs_node *b_dnode,
1557 struct componentname *b_cnp, bool b_missing_ok,
1558 struct tmpfs_dirent **b_dirent_ret, struct vnode **b_vp_ret)
1559 {
1560 struct tmpfs_dirent *a_dirent, *b_dirent;
1561 struct vnode *a_vp, *b_vp;
1562 int error;
1563
1564 KASSERT(a_dvp != NULL);
1565 KASSERT(a_dnode != NULL);
1566 KASSERT(a_cnp != NULL);
1567 KASSERT(a_dirent_ret != NULL);
1568 KASSERT(a_vp_ret != NULL);
1569 KASSERT(b_dvp != NULL);
1570 KASSERT(b_dnode != NULL);
1571 KASSERT(b_cnp != NULL);
1572 KASSERT(b_dirent_ret != NULL);
1573 KASSERT(b_vp_ret != NULL);
1574 KASSERT(a_dvp != b_dvp);
1575 KASSERT(a_dnode != b_dnode);
1576 KASSERT(a_dnode->tn_vnode == a_dvp);
1577 KASSERT(b_dnode->tn_vnode == b_dvp);
1578 KASSERT(a_dnode->tn_type == VDIR);
1579 KASSERT(b_dnode->tn_type == VDIR);
1580 KASSERT(a_missing_ok != b_missing_ok);
1581
1582 error = tmpfs_rename_lock_directory(a_dvp, a_dnode);
1583 if (error)
1584 goto fail0;
1585
1586 /* Did we lose a race with mount? */
1587 if (a_dvp->v_mountedhere != NULL) {
1588 error = EBUSY;
1589 goto fail1;
1590 }
1591
1592 /* Make sure the caller may read the directory. */
1593 error = VOP_ACCESS(a_dvp, VEXEC, cred);
1594 if (error)
1595 goto fail1;
1596
1597 a_dirent = tmpfs_dir_lookup(a_dnode, a_cnp);
1598 if (a_dirent != NULL) {
1599 KASSERT(a_dirent->td_node != NULL);
1600 /* We ruled out `.' earlier. */
1601 KASSERT(a_dirent->td_node != a_dnode);
1602 /* We ruled out `..' earlier. */
1603 KASSERT(a_dirent->td_node !=
1604 a_dnode->tn_spec.tn_dir.tn_parent);
1605 if (a_dirent->td_node == b_dnode) {
1606 error = overlap_error;
1607 goto fail1;
1608 }
1609 mutex_enter(&a_dirent->td_node->tn_vlock);
1610 error = tmpfs_vnode_get(mount, a_dirent->td_node, &a_vp);
1611 if (error)
1612 goto fail1;
1613 KASSERT(a_vp->v_mount == mount);
1614 /* Refuse to rename (over) a mount point. */
1615 if ((a_vp->v_type == VDIR) && (a_vp->v_mountedhere != NULL)) {
1616 error = EBUSY;
1617 goto fail2;
1618 }
1619 } else if (!a_missing_ok) {
1620 error = ENOENT;
1621 goto fail1;
1622 } else {
1623 a_vp = NULL;
1624 }
1625 KASSERT(a_vp != a_dvp);
1626 KASSERT(a_vp != b_dvp);
1627
1628 error = tmpfs_rename_lock_directory(b_dvp, b_dnode);
1629 if (error)
1630 goto fail2;
1631
1632 /* Did we lose a race with mount? */
1633 if (b_dvp->v_mountedhere != NULL) {
1634 error = EBUSY;
1635 goto fail3;
1636 }
1637
1638 /* Make sure the caller may read the directory. */
1639 error = VOP_ACCESS(b_dvp, VEXEC, cred);
1640 if (error)
1641 goto fail3;
1642
1643 b_dirent = tmpfs_dir_lookup(b_dnode, b_cnp);
1644 if (b_dirent != NULL) {
1645 KASSERT(b_dirent->td_node != NULL);
1646 /* We ruled out `.' earlier. */
1647 KASSERT(b_dirent->td_node != b_dnode);
1648 /* We ruled out `..' earlier. */
1649 KASSERT(b_dirent->td_node !=
1650 b_dnode->tn_spec.tn_dir.tn_parent);
1651 /* b is not an ancestor of a. */
1652 KASSERT(b_dirent->td_node != a_dnode);
1653 /* But the source and target nodes might be the same. */
1654 if ((a_dirent == NULL) ||
1655 (a_dirent->td_node != b_dirent->td_node)) {
1656 mutex_enter(&b_dirent->td_node->tn_vlock);
1657 error = tmpfs_vnode_get(mount, b_dirent->td_node,
1658 &b_vp);
1659 if (error)
1660 goto fail3;
1661 KASSERT(b_vp->v_mount == mount);
1662 KASSERT(a_vp != b_vp);
1663 /* Refuse to rename (over) a mount point. */
1664 if ((b_vp->v_type == VDIR) &&
1665 (b_vp->v_mountedhere != NULL)) {
1666 error = EBUSY;
1667 goto fail4;
1668 }
1669 } else {
1670 b_vp = a_vp;
1671 vref(b_vp);
1672 }
1673 } else if (!b_missing_ok) {
1674 error = ENOENT;
1675 goto fail3;
1676 } else {
1677 b_vp = NULL;
1678 }
1679 KASSERT(b_vp != a_dvp);
1680 KASSERT(b_vp != b_dvp);
1681
1682 KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
1683 KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
1684 KASSERT(a_missing_ok || (a_dirent != NULL));
1685 KASSERT(a_missing_ok || (a_dirent->td_node != NULL));
1686 KASSERT(b_missing_ok || (b_dirent != NULL));
1687 KASSERT(b_missing_ok || (b_dirent->td_node != NULL));
1688 KASSERT((a_dirent == NULL) || (a_dirent->td_node != NULL));
1689 KASSERT((a_dirent == NULL) || (a_dirent->td_node->tn_vnode == a_vp));
1690 KASSERT((b_dirent == NULL) || (b_dirent->td_node != NULL));
1691 KASSERT((b_dirent == NULL) || (b_dirent->td_node->tn_vnode == b_vp));
1692 KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE));
1693 KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE));
1694
1695 *a_dirent_ret = a_dirent;
1696 *b_dirent_ret = b_dirent;
1697 *a_vp_ret = a_vp;
1698 *b_vp_ret = b_vp;
1699 return 0;
1700
1701 fail4: if (b_vp != NULL) {
1702 KASSERT(VOP_ISLOCKED(b_vp) == LK_EXCLUSIVE);
1703 if (b_vp != a_vp)
1704 vput(b_vp);
1705 else
1706 vrele(a_vp);
1707 }
1708
1709 fail3: KASSERT(VOP_ISLOCKED(b_dvp) == LK_EXCLUSIVE);
1710 VOP_UNLOCK(b_dvp);
1711
1712 fail2: if (a_vp != NULL) {
1713 KASSERT(VOP_ISLOCKED(a_vp) == LK_EXCLUSIVE);
1714 vput(a_vp);
1715 }
1716
1717 fail1: KASSERT(VOP_ISLOCKED(a_dvp) == LK_EXCLUSIVE);
1718 VOP_UNLOCK(a_dvp);
1719
1720 fail0: /* KASSERT(VOP_ISLOCKED(a_dvp) != LK_EXCLUSIVE); */
1721 /* KASSERT(VOP_ISLOCKED(b_dvp) != LK_EXCLUSIVE); */
1722 /* KASSERT((a_vp == NULL) || (VOP_ISLOCKED(a_vp) != LK_EXCLUSIVE)); */
1723 /* KASSERT((b_vp == NULL) || (VOP_ISLOCKED(b_vp) != LK_EXCLUSIVE)); */
1724 return error;
1725 }
1726
1727 /*
1728 * Shuffle the directory entries to move fvp from the directory fdvp
1729 * into the directory tdvp. fde is fvp's directory entry in fdvp. If
1730 * we are overwriting a target node, it is tvp, and tde is its
1731 * directory entry in tdvp.
1732 *
1733 * fdvp, fvp, tdvp, and tvp must all be locked and referenced.
1734 */
1735 static void
1736 tmpfs_rename_attachdetach(struct tmpfs_mount *tmpfs,
1737 struct vnode *fdvp, struct tmpfs_dirent *fde, struct vnode *fvp,
1738 struct vnode *tdvp, struct tmpfs_dirent *tde, struct vnode *tvp)
1739 {
1740
1741 KASSERT(tmpfs != NULL);
1742 KASSERT(fdvp != NULL);
1743 KASSERT(fde != NULL);
1744 KASSERT(fvp != NULL);
1745 KASSERT(tdvp != NULL);
1746 KASSERT(fde->td_node != NULL);
1747 KASSERT(fde->td_node->tn_vnode == fvp);
1748 KASSERT((tde == NULL) == (tvp == NULL));
1749 KASSERT((tde == NULL) || (tde->td_node != NULL));
1750 KASSERT((tde == NULL) || (tde->td_node->tn_vnode == tvp));
1751 KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
1752 KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
1753 KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
1754 KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1755
1756 /*
1757 * If we are moving from one directory to another, detach the
1758 * source entry and reattach it to the target directory.
1759 */
1760 if (fdvp != tdvp) {
1761 /* tmpfs_dir_detach clobbers fde->td_node, so save it. */
1762 struct tmpfs_node *fnode = fde->td_node;
1763 tmpfs_dir_detach(fdvp, fde);
1764 tmpfs_dir_attach(tdvp, fde, fnode);
1765 } else if (tvp == NULL) {
1766 /*
1767 * We are changing the directory. tmpfs_dir_attach and
1768 * tmpfs_dir_detach note the events for us, but for
1769 * this case we don't call them, so we must note the
1770 * event explicitly.
1771 */
1772 VN_KNOTE(fdvp, NOTE_WRITE);
1773 }
1774
1775 /*
1776 * If we are replacing an existing target entry, delete it.
1777 */
1778 if (tde != NULL) {
1779 KASSERT(tvp != NULL);
1780 KASSERT(tde->td_node != NULL);
1781 KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
1782 if (tde->td_node->tn_type == VDIR) {
1783 KASSERT(tde->td_node->tn_size == 0);
1784 KASSERT(tde->td_node->tn_links == 2);
1785 /* Decrement the extra link count for `.' so
1786 * the vnode will be recycled when released. */
1787 tde->td_node->tn_links--;
1788 }
1789 tmpfs_dir_detach(tdvp, tde);
1790 tmpfs_free_dirent(tmpfs, tde);
1791 }
1792 }
1793
1794 /*
1795 * Remove the entry de for the non-directory vp from the directory dvp.
1796 *
1797 * Everything must be locked and referenced.
1798 */
1799 static int
1800 tmpfs_do_remove(struct tmpfs_mount *tmpfs, struct vnode *dvp,
1801 struct tmpfs_node *dnode, struct tmpfs_dirent *de, struct vnode *vp,
1802 kauth_cred_t cred)
1803 {
1804 int error;
1805
1806 KASSERT(tmpfs != NULL);
1807 KASSERT(dvp != NULL);
1808 KASSERT(dnode != NULL);
1809 KASSERT(de != NULL);
1810 KASSERT(vp != NULL);
1811 KASSERT(dnode->tn_vnode == dvp);
1812 KASSERT(de->td_node != NULL);
1813 KASSERT(de->td_node->tn_vnode == vp);
1814 KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
1815 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1816
1817 error = tmpfs_remove_check_possible(dnode, de->td_node);
1818 if (error)
1819 return error;
1820
1821 error = tmpfs_remove_check_permitted(cred, dnode, de->td_node);
1822 error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, vp, dvp,
1823 error);
1824 if (error)
1825 return error;
1826
1827 tmpfs_dir_detach(dvp, de);
1828 tmpfs_free_dirent(tmpfs, de);
1829
1830 return 0;
1831 }
1832
1833 /*
1834 * Check whether a rename is possible independent of credentials.
1835 *
1836 * Everything must be locked and referenced.
1837 */
1838 static int
1839 tmpfs_rename_check_possible(
1840 struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
1841 struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
1842 {
1843
1844 KASSERT(fdnode != NULL);
1845 KASSERT(fnode != NULL);
1846 KASSERT(tdnode != NULL);
1847 KASSERT(fdnode != fnode);
1848 KASSERT(tdnode != tnode);
1849 KASSERT(fnode != tnode);
1850 KASSERT(fdnode->tn_vnode != NULL);
1851 KASSERT(fnode->tn_vnode != NULL);
1852 KASSERT(tdnode->tn_vnode != NULL);
1853 KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
1854 KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
1855 KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
1856 KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
1857 KASSERT((tnode == NULL) ||
1858 (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
1859
1860 /*
1861 * If fdnode is immutable, we can't write to it. If fdnode is
1862 * append-only, the only change we can make is to add entries
1863 * to it. If fnode is immutable, we can't change the links to
1864 * it. If fnode is append-only...well, this is what UFS does.
1865 */
1866 if ((fdnode->tn_flags | fnode->tn_flags) & (IMMUTABLE | APPEND))
1867 return EPERM;
1868
1869 /*
1870 * If tdnode is immutable, we can't write to it. If tdnode is
1871 * append-only, we can add entries, but we can't change
1872 * existing entries.
1873 */
1874 if (tdnode->tn_flags & (IMMUTABLE | (tnode? APPEND : 0)))
1875 return EPERM;
1876
1877 /*
1878 * If tnode is immutable, we can't replace links to it. If
1879 * tnode is append-only...well, this is what UFS does.
1880 */
1881 if (tnode != NULL) {
1882 KASSERT(tnode != NULL);
1883 if ((tnode->tn_flags & (IMMUTABLE | APPEND)) != 0)
1884 return EPERM;
1885 }
1886
1887 return 0;
1888 }
1889
1890 /*
1891 * Check whether a rename is permitted given our credentials.
1892 *
1893 * Everything must be locked and referenced.
1894 */
1895 static int
1896 tmpfs_rename_check_permitted(kauth_cred_t cred,
1897 struct tmpfs_node *fdnode, struct tmpfs_node *fnode,
1898 struct tmpfs_node *tdnode, struct tmpfs_node *tnode)
1899 {
1900 int error;
1901
1902 KASSERT(fdnode != NULL);
1903 KASSERT(fnode != NULL);
1904 KASSERT(tdnode != NULL);
1905 KASSERT(fdnode != fnode);
1906 KASSERT(tdnode != tnode);
1907 KASSERT(fnode != tnode);
1908 KASSERT(fdnode->tn_vnode != NULL);
1909 KASSERT(fnode->tn_vnode != NULL);
1910 KASSERT(tdnode->tn_vnode != NULL);
1911 KASSERT((tnode == NULL) || (tnode->tn_vnode != NULL));
1912 KASSERT(VOP_ISLOCKED(fdnode->tn_vnode) == LK_EXCLUSIVE);
1913 KASSERT(VOP_ISLOCKED(fnode->tn_vnode) == LK_EXCLUSIVE);
1914 KASSERT(VOP_ISLOCKED(tdnode->tn_vnode) == LK_EXCLUSIVE);
1915 KASSERT((tnode == NULL) ||
1916 (VOP_ISLOCKED(tnode->tn_vnode) == LK_EXCLUSIVE));
1917
1918 /*
1919 * We need to remove or change an entry in the source directory.
1920 */
1921 error = VOP_ACCESS(fdnode->tn_vnode, VWRITE, cred);
1922 if (error)
1923 return error;
1924
1925 /*
1926 * If we are changing directories, then we need to write to the
1927 * target directory to add or change an entry. Also, if fnode
1928 * is a directory, we need to write to it to change its `..'
1929 * entry.
1930 */
1931 if (fdnode != tdnode) {
1932 error = VOP_ACCESS(tdnode->tn_vnode, VWRITE, cred);
1933 if (error)
1934 return error;
1935 if (fnode->tn_type == VDIR) {
1936 error = VOP_ACCESS(fnode->tn_vnode, VWRITE, cred);
1937 if (error)
1938 return error;
1939 }
1940 }
1941
1942 error = tmpfs_check_sticky(cred, fdnode, fnode);
1943 if (error)
1944 return error;
1945
1946 error = tmpfs_check_sticky(cred, tdnode, tnode);
1947 if (error)
1948 return error;
1949
1950 return 0;
1951 }
1952
1953 /*
1954 * Check whether removing node's entry in dnode is possible independent
1955 * of credentials.
1956 *
1957 * Everything must be locked and referenced.
1958 */
1959 static int
1960 tmpfs_remove_check_possible(struct tmpfs_node *dnode, struct tmpfs_node *node)
1961 {
1962
1963 KASSERT(dnode != NULL);
1964 KASSERT(dnode->tn_vnode != NULL);
1965 KASSERT(node != NULL);
1966 KASSERT(dnode != node);
1967 KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
1968 KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
1969
1970 /*
1971 * We want to delete the entry. If dnode is immutable, we
1972 * can't write to it to delete the entry. If dnode is
1973 * append-only, the only change we can make is to add entries,
1974 * so we can't delete entries. If node is immutable, we can't
1975 * change the links to it, so we can't delete the entry. If
1976 * node is append-only...well, this is what UFS does.
1977 */
1978 if ((dnode->tn_flags | node->tn_flags) & (IMMUTABLE | APPEND))
1979 return EPERM;
1980
1981 return 0;
1982 }
1983
1984 /*
1985 * Check whether removing node's entry in dnode is permitted given our
1986 * credentials.
1987 *
1988 * Everything must be locked and referenced.
1989 */
1990 static int
1991 tmpfs_remove_check_permitted(kauth_cred_t cred,
1992 struct tmpfs_node *dnode, struct tmpfs_node *node)
1993 {
1994 int error;
1995
1996 KASSERT(dnode != NULL);
1997 KASSERT(dnode->tn_vnode != NULL);
1998 KASSERT(node != NULL);
1999 KASSERT(dnode != node);
2000 KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
2001 KASSERT(VOP_ISLOCKED(node->tn_vnode) == LK_EXCLUSIVE);
2002
2003 /*
2004 * Check whether we are permitted to write to the source
2005 * directory in order to delete an entry from it.
2006 */
2007 error = VOP_ACCESS(dnode->tn_vnode, VWRITE, cred);
2008 if (error)
2009 return error;
2010
2011 error = tmpfs_check_sticky(cred, dnode, node);
2012 if (error)
2013 return error;
2014
2015 return 0;
2016 }
2017
2018 /*
2019 * Check whether we may change an entry in a sticky directory. If the
2020 * directory is sticky, the user must own either the directory or, if
2021 * it exists, the node, in order to change the entry.
2022 *
2023 * Everything must be locked and referenced.
2024 */
2025 static int
2026 tmpfs_check_sticky(kauth_cred_t cred,
2027 struct tmpfs_node *dnode, struct tmpfs_node *node)
2028 {
2029
2030 KASSERT(dnode != NULL);
2031 KASSERT(dnode->tn_vnode != NULL);
2032 KASSERT(VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE);
2033 KASSERT((node == NULL) || (node->tn_vnode != NULL));
2034 KASSERT((node == NULL) ||
2035 (VOP_ISLOCKED(dnode->tn_vnode) == LK_EXCLUSIVE));
2036
2037 if (node == NULL)
2038 return 0;
2039
2040 if (dnode->tn_mode & S_ISTXT) {
2041 if (kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
2042 node->tn_vnode, dnode->tn_vnode, genfs_can_sticky(cred,
2043 dnode->tn_uid, node->tn_uid)) != 0)
2044 return EPERM;
2045 }
2046
2047 return 0;
2048 }
2049
2050 int
2051 tmpfs_mkdir(void *v)
2052 {
2053 struct vop_mkdir_args /* {
2054 struct vnode *a_dvp;
2055 struct vnode **a_vpp;
2056 struct componentname *a_cnp;
2057 struct vattr *a_vap;
2058 } */ *ap = v;
2059 vnode_t *dvp = ap->a_dvp;
2060 vnode_t **vpp = ap->a_vpp;
2061 struct componentname *cnp = ap->a_cnp;
2062 struct vattr *vap = ap->a_vap;
2063
2064 KASSERT(vap->va_type == VDIR);
2065 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
2066 }
2067
2068 int
2069 tmpfs_rmdir(void *v)
2070 {
2071 struct vop_rmdir_args /* {
2072 struct vnode *a_dvp;
2073 struct vnode *a_vp;
2074 struct componentname *a_cnp;
2075 } */ *ap = v;
2076 vnode_t *dvp = ap->a_dvp;
2077 vnode_t *vp = ap->a_vp;
2078 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
2079 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp);
2080 tmpfs_node_t *node = VP_TO_TMPFS_DIR(vp);
2081 tmpfs_dirent_t *de;
2082 int error = 0;
2083
2084 KASSERT(VOP_ISLOCKED(dvp));
2085 KASSERT(VOP_ISLOCKED(vp));
2086 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
2087
2088 /*
2089 * Directories with more than two non-whiteout
2090 * entries ('.' and '..') cannot be removed.
2091 */
2092 if (node->tn_size > 0) {
2093 KASSERT(error == 0);
2094 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
2095 if (de->td_node != TMPFS_NODE_WHITEOUT) {
2096 error = ENOTEMPTY;
2097 break;
2098 }
2099 }
2100 if (error)
2101 goto out;
2102 }
2103
2104 /* Lookup the directory entry (check the cached hint first). */
2105 de = tmpfs_dir_cached(node);
2106 if (de == NULL) {
2107 struct componentname *cnp = ap->a_cnp;
2108 de = tmpfs_dir_lookup(dnode, cnp);
2109 }
2110 KASSERT(de && de->td_node == node);
2111
2112 /* Check flags to see if we are allowed to remove the directory. */
2113 if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
2114 error = EPERM;
2115 goto out;
2116 }
2117
2118 /* Decrement the link count for the virtual '.' entry. */
2119 node->tn_links--;
2120 node->tn_status |= TMPFS_NODE_STATUSALL;
2121
2122 /* Detach the directory entry from the directory. */
2123 tmpfs_dir_detach(dvp, de);
2124
2125 /* Purge the cache for parent. */
2126 cache_purge(dvp);
2127
2128 /*
2129 * Destroy the directory entry or replace it with a whiteout.
2130 * Note: the inode referred by it will not be destroyed
2131 * until the vnode is reclaimed.
2132 */
2133 if (ap->a_cnp->cn_flags & DOWHITEOUT)
2134 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
2135 else
2136 tmpfs_free_dirent(tmp, de);
2137
2138 /* Destroy the whiteout entries from the node. */
2139 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
2140 KASSERT(de->td_node == TMPFS_NODE_WHITEOUT);
2141 tmpfs_dir_detach(vp, de);
2142 tmpfs_free_dirent(tmp, de);
2143 }
2144
2145 KASSERT(node->tn_links == 0);
2146 out:
2147 /* Release the nodes. */
2148 vput(dvp);
2149 vput(vp);
2150 return error;
2151 }
2152
2153 int
2154 tmpfs_symlink(void *v)
2155 {
2156 struct vop_symlink_args /* {
2157 struct vnode *a_dvp;
2158 struct vnode **a_vpp;
2159 struct componentname *a_cnp;
2160 struct vattr *a_vap;
2161 char *a_target;
2162 } */ *ap = v;
2163 vnode_t *dvp = ap->a_dvp;
2164 vnode_t **vpp = ap->a_vpp;
2165 struct componentname *cnp = ap->a_cnp;
2166 struct vattr *vap = ap->a_vap;
2167 char *target = ap->a_target;
2168
2169 KASSERT(vap->va_type == VLNK);
2170 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
2171 }
2172
2173 int
2174 tmpfs_readdir(void *v)
2175 {
2176 struct vop_readdir_args /* {
2177 struct vnode *a_vp;
2178 struct uio *a_uio;
2179 kauth_cred_t a_cred;
2180 int *a_eofflag;
2181 off_t **a_cookies;
2182 int *ncookies;
2183 } */ *ap = v;
2184 vnode_t *vp = ap->a_vp;
2185 struct uio *uio = ap->a_uio;
2186 int *eofflag = ap->a_eofflag;
2187 off_t **cookies = ap->a_cookies;
2188 int *ncookies = ap->a_ncookies;
2189 off_t startoff, cnt;
2190 tmpfs_node_t *node;
2191 int error;
2192
2193 KASSERT(VOP_ISLOCKED(vp));
2194
2195 /* This operation only makes sense on directory nodes. */
2196 if (vp->v_type != VDIR) {
2197 return ENOTDIR;
2198 }
2199 node = VP_TO_TMPFS_DIR(vp);
2200 startoff = uio->uio_offset;
2201 cnt = 0;
2202 if (node->tn_links == 0) {
2203 error = 0;
2204 goto out;
2205 }
2206
2207 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
2208 error = tmpfs_dir_getdotdent(node, uio);
2209 if (error != 0) {
2210 if (error == -1)
2211 error = 0;
2212 goto out;
2213 }
2214 cnt++;
2215 }
2216 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
2217 error = tmpfs_dir_getdotdotdent(node, uio);
2218 if (error != 0) {
2219 if (error == -1)
2220 error = 0;
2221 goto out;
2222 }
2223 cnt++;
2224 }
2225 error = tmpfs_dir_getdents(node, uio, &cnt);
2226 if (error == -1) {
2227 error = 0;
2228 }
2229 KASSERT(error >= 0);
2230 out:
2231 if (eofflag != NULL) {
2232 *eofflag = (!error && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
2233 }
2234 if (error || cookies == NULL || ncookies == NULL) {
2235 return error;
2236 }
2237
2238 /* Update NFS-related variables, if any. */
2239 off_t i, off = startoff;
2240 tmpfs_dirent_t *de = NULL;
2241
2242 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
2243 *ncookies = cnt;
2244
2245 for (i = 0; i < cnt; i++) {
2246 KASSERT(off != TMPFS_DIRCOOKIE_EOF);
2247 if (off != TMPFS_DIRCOOKIE_DOT) {
2248 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
2249 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
2250 } else if (de != NULL) {
2251 de = TAILQ_NEXT(de, td_entries);
2252 } else {
2253 de = tmpfs_dir_lookupbycookie(node, off);
2254 KASSERT(de != NULL);
2255 de = TAILQ_NEXT(de, td_entries);
2256 }
2257 if (de == NULL) {
2258 off = TMPFS_DIRCOOKIE_EOF;
2259 } else {
2260 off = tmpfs_dircookie(de);
2261 }
2262 } else {
2263 off = TMPFS_DIRCOOKIE_DOTDOT;
2264 }
2265 (*cookies)[i] = off;
2266 }
2267 KASSERT(uio->uio_offset == off);
2268 return error;
2269 }
2270
2271 int
2272 tmpfs_readlink(void *v)
2273 {
2274 struct vop_readlink_args /* {
2275 struct vnode *a_vp;
2276 struct uio *a_uio;
2277 kauth_cred_t a_cred;
2278 } */ *ap = v;
2279 vnode_t *vp = ap->a_vp;
2280 struct uio *uio = ap->a_uio;
2281 tmpfs_node_t *node;
2282 int error;
2283
2284 KASSERT(VOP_ISLOCKED(vp));
2285 KASSERT(uio->uio_offset == 0);
2286 KASSERT(vp->v_type == VLNK);
2287
2288 node = VP_TO_TMPFS_NODE(vp);
2289 error = uiomove(node->tn_spec.tn_lnk.tn_link,
2290 MIN(node->tn_size, uio->uio_resid), uio);
2291 node->tn_status |= TMPFS_NODE_ACCESSED;
2292
2293 return error;
2294 }
2295
2296 int
2297 tmpfs_inactive(void *v)
2298 {
2299 struct vop_inactive_args /* {
2300 struct vnode *a_vp;
2301 bool *a_recycle;
2302 } */ *ap = v;
2303 vnode_t *vp = ap->a_vp;
2304 tmpfs_node_t *node;
2305
2306 KASSERT(VOP_ISLOCKED(vp));
2307
2308 node = VP_TO_TMPFS_NODE(vp);
2309 *ap->a_recycle = (node->tn_links == 0);
2310 VOP_UNLOCK(vp);
2311
2312 return 0;
2313 }
2314
2315 int
2316 tmpfs_reclaim(void *v)
2317 {
2318 struct vop_reclaim_args /* {
2319 struct vnode *a_vp;
2320 } */ *ap = v;
2321 vnode_t *vp = ap->a_vp;
2322 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
2323 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
2324 bool racing;
2325
2326 /* Disassociate inode from vnode. */
2327 mutex_enter(&node->tn_vlock);
2328 node->tn_vnode = NULL;
2329 vp->v_data = NULL;
2330 /* Check if tmpfs_vnode_get() is racing with us. */
2331 racing = TMPFS_NODE_RECLAIMING(node);
2332 mutex_exit(&node->tn_vlock);
2333
2334 /*
2335 * If inode is not referenced, i.e. no links, then destroy it.
2336 * Note: if racing - inode is about to get a new vnode, leave it.
2337 */
2338 if (node->tn_links == 0 && !racing) {
2339 tmpfs_free_node(tmp, node);
2340 }
2341 return 0;
2342 }
2343
2344 int
2345 tmpfs_pathconf(void *v)
2346 {
2347 struct vop_pathconf_args /* {
2348 struct vnode *a_vp;
2349 int a_name;
2350 register_t *a_retval;
2351 } */ *ap = v;
2352 const int name = ap->a_name;
2353 register_t *retval = ap->a_retval;
2354 int error = 0;
2355
2356 switch (name) {
2357 case _PC_LINK_MAX:
2358 *retval = LINK_MAX;
2359 break;
2360 case _PC_NAME_MAX:
2361 *retval = TMPFS_MAXNAMLEN;
2362 break;
2363 case _PC_PATH_MAX:
2364 *retval = PATH_MAX;
2365 break;
2366 case _PC_PIPE_BUF:
2367 *retval = PIPE_BUF;
2368 break;
2369 case _PC_CHOWN_RESTRICTED:
2370 *retval = 1;
2371 break;
2372 case _PC_NO_TRUNC:
2373 *retval = 1;
2374 break;
2375 case _PC_SYNC_IO:
2376 *retval = 1;
2377 break;
2378 case _PC_FILESIZEBITS:
2379 *retval = sizeof(off_t) * CHAR_BIT;
2380 break;
2381 default:
2382 error = EINVAL;
2383 }
2384 return error;
2385 }
2386
2387 int
2388 tmpfs_advlock(void *v)
2389 {
2390 struct vop_advlock_args /* {
2391 struct vnode *a_vp;
2392 void * a_id;
2393 int a_op;
2394 struct flock *a_fl;
2395 int a_flags;
2396 } */ *ap = v;
2397 vnode_t *vp = ap->a_vp;
2398 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
2399
2400 return lf_advlock(v, &node->tn_lockf, node->tn_size);
2401 }
2402
2403 int
2404 tmpfs_getpages(void *v)
2405 {
2406 struct vop_getpages_args /* {
2407 struct vnode *a_vp;
2408 voff_t a_offset;
2409 struct vm_page **a_m;
2410 int *a_count;
2411 int a_centeridx;
2412 vm_prot_t a_access_type;
2413 int a_advice;
2414 int a_flags;
2415 } */ * const ap = v;
2416 vnode_t *vp = ap->a_vp;
2417 const voff_t offset = ap->a_offset;
2418 struct vm_page **pgs = ap->a_m;
2419 const int centeridx = ap->a_centeridx;
2420 const vm_prot_t access_type = ap->a_access_type;
2421 const int advice = ap->a_advice;
2422 const int flags = ap->a_flags;
2423 int error, npages = *ap->a_count;
2424 tmpfs_node_t *node;
2425 struct uvm_object *uobj;
2426
2427 KASSERT(vp->v_type == VREG);
2428 KASSERT(mutex_owned(vp->v_interlock));
2429
2430 node = VP_TO_TMPFS_NODE(vp);
2431 uobj = node->tn_spec.tn_reg.tn_aobj;
2432
2433 /*
2434 * Currently, PGO_PASTEOF is not supported.
2435 */
2436 if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
2437 if ((flags & PGO_LOCKED) == 0)
2438 mutex_exit(vp->v_interlock);
2439 return EINVAL;
2440 }
2441
2442 if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
2443 npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
2444 }
2445
2446 if ((flags & PGO_LOCKED) != 0)
2447 return EBUSY;
2448
2449 if ((flags & PGO_NOTIMESTAMP) == 0) {
2450 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
2451 node->tn_status |= TMPFS_NODE_ACCESSED;
2452
2453 if ((access_type & VM_PROT_WRITE) != 0) {
2454 node->tn_status |= TMPFS_NODE_MODIFIED;
2455 if (vp->v_mount->mnt_flag & MNT_RELATIME)
2456 node->tn_status |= TMPFS_NODE_ACCESSED;
2457 }
2458 }
2459
2460 /*
2461 * Invoke the pager.
2462 *
2463 * Clean the array of pages before. XXX: PR/32166
2464 * Note that vnode lock is shared with underlying UVM object.
2465 */
2466 if (pgs) {
2467 memset(pgs, 0, sizeof(struct vm_pages *) * npages);
2468 }
2469 KASSERT(vp->v_interlock == uobj->vmobjlock);
2470
2471 error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, centeridx,
2472 access_type, advice, flags | PGO_ALLPAGES);
2473
2474 #if defined(DEBUG)
2475 if (!error && pgs) {
2476 for (int i = 0; i < npages; i++) {
2477 KASSERT(pgs[i] != NULL);
2478 }
2479 }
2480 #endif
2481 return error;
2482 }
2483
2484 int
2485 tmpfs_putpages(void *v)
2486 {
2487 struct vop_putpages_args /* {
2488 struct vnode *a_vp;
2489 voff_t a_offlo;
2490 voff_t a_offhi;
2491 int a_flags;
2492 } */ * const ap = v;
2493 vnode_t *vp = ap->a_vp;
2494 const voff_t offlo = ap->a_offlo;
2495 const voff_t offhi = ap->a_offhi;
2496 const int flags = ap->a_flags;
2497 tmpfs_node_t *node;
2498 struct uvm_object *uobj;
2499 int error;
2500
2501 KASSERT(mutex_owned(vp->v_interlock));
2502
2503 if (vp->v_type != VREG) {
2504 mutex_exit(vp->v_interlock);
2505 return 0;
2506 }
2507
2508 node = VP_TO_TMPFS_NODE(vp);
2509 uobj = node->tn_spec.tn_reg.tn_aobj;
2510
2511 KASSERT(vp->v_interlock == uobj->vmobjlock);
2512 error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
2513
2514 /* XXX mtime */
2515
2516 return error;
2517 }
2518
2519 int
2520 tmpfs_whiteout(void *v)
2521 {
2522 struct vop_whiteout_args /* {
2523 struct vnode *a_dvp;
2524 struct componentname *a_cnp;
2525 int a_flags;
2526 } */ *ap = v;
2527 vnode_t *dvp = ap->a_dvp;
2528 struct componentname *cnp = ap->a_cnp;
2529 const int flags = ap->a_flags;
2530 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
2531 tmpfs_dirent_t *de;
2532 int error;
2533
2534 switch (flags) {
2535 case LOOKUP:
2536 break;
2537 case CREATE:
2538 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr,
2539 cnp->cn_namelen, &de);
2540 if (error)
2541 return error;
2542 tmpfs_dir_attach(dvp, de, TMPFS_NODE_WHITEOUT);
2543 break;
2544 case DELETE:
2545 cnp->cn_flags &= ~DOWHITEOUT; /* when in doubt, cargo cult */
2546 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), cnp);
2547 if (de == NULL)
2548 return ENOENT;
2549 tmpfs_dir_detach(dvp, de);
2550 tmpfs_free_dirent(tmp, de);
2551 break;
2552 }
2553 return 0;
2554 }
2555
2556 int
2557 tmpfs_print(void *v)
2558 {
2559 struct vop_print_args /* {
2560 struct vnode *a_vp;
2561 } */ *ap = v;
2562 vnode_t *vp = ap->a_vp;
2563 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
2564
2565 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n"
2566 "\tmode 0%o, owner %d, group %d, size %" PRIdMAX ", status 0x%x",
2567 node, node->tn_flags, node->tn_links, node->tn_mode, node->tn_uid,
2568 node->tn_gid, (uintmax_t)node->tn_size, node->tn_status);
2569 if (vp->v_type == VFIFO) {
2570 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
2571 }
2572 printf("\n");
2573 return 0;
2574 }
2575