1 /* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11 * 2005 program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * tmpfs vnode interface.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/dirent.h>
42 #include <sys/extattr.h>
43 #include <sys/fcntl.h>
44 #include <sys/file.h>
45 #include <sys/filio.h>
46 #include <sys/limits.h>
47 #include <sys/lockf.h>
48 #include <sys/lock.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/rwlock.h>
54 #include <sys/sched.h>
55 #include <sys/smr.h>
56 #include <sys/stat.h>
57 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
60 #include <security/audit/audit.h>
61 #include <security/mac/mac_framework.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_page.h>
67 #include <vm/vm_pager.h>
68 #include <vm/swap_pager.h>
69
70 #include <fs/tmpfs/tmpfs_vnops.h>
71 #include <fs/tmpfs/tmpfs.h>
72
73 SYSCTL_DECL(_vfs_tmpfs);
74 VFS_SMR_DECLARE;
75
76 static volatile int tmpfs_rename_restarts;
77 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
78 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
79 "Times rename had to restart due to lock contention");
80
81 MALLOC_DEFINE(M_TMPFSEA, "tmpfs extattr", "tmpfs extattr structure");
82
83 static int
tmpfs_vn_get_ino_alloc(struct mount * mp,void * arg,int lkflags,struct vnode ** rvp)84 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
85 struct vnode **rvp)
86 {
87
88 return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
89 }
90
91 static int
tmpfs_lookup1(struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp)92 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
93 {
94 struct tmpfs_dirent *de;
95 struct tmpfs_node *dnode, *pnode;
96 struct tmpfs_mount *tm;
97 int error;
98
99 /* Caller assumes responsibility for ensuring access (VEXEC). */
100 dnode = VP_TO_TMPFS_DIR(dvp);
101 *vpp = NULLVP;
102
103 /* We cannot be requesting the parent directory of the root node. */
104 MPASS(IMPLIES(dnode->tn_type == VDIR &&
105 dnode->tn_dir.tn_parent == dnode,
106 !(cnp->cn_flags & ISDOTDOT)));
107
108 TMPFS_ASSERT_LOCKED(dnode);
109 if (dnode->tn_dir.tn_parent == NULL) {
110 error = ENOENT;
111 goto out;
112 }
113 if (cnp->cn_flags & ISDOTDOT) {
114 tm = VFS_TO_TMPFS(dvp->v_mount);
115 pnode = dnode->tn_dir.tn_parent;
116 tmpfs_ref_node(pnode);
117 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
118 pnode, cnp->cn_lkflags, vpp);
119 tmpfs_free_node(tm, pnode);
120 if (error != 0)
121 goto out;
122 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
123 VREF(dvp);
124 *vpp = dvp;
125 error = 0;
126 } else {
127 de = tmpfs_dir_lookup(dnode, NULL, cnp);
128 if (de != NULL && de->td_node == NULL)
129 cnp->cn_flags |= ISWHITEOUT;
130 if (de == NULL || de->td_node == NULL) {
131 /*
132 * The entry was not found in the directory.
133 * This is OK if we are creating or renaming an
134 * entry and are working on the last component of
135 * the path name.
136 */
137 if ((cnp->cn_flags & ISLASTCN) &&
138 (cnp->cn_nameiop == CREATE || \
139 cnp->cn_nameiop == RENAME ||
140 (cnp->cn_nameiop == DELETE &&
141 cnp->cn_flags & DOWHITEOUT &&
142 cnp->cn_flags & ISWHITEOUT))) {
143 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
144 curthread);
145 if (error != 0)
146 goto out;
147
148 error = EJUSTRETURN;
149 } else
150 error = ENOENT;
151 } else {
152 struct tmpfs_node *tnode;
153
154 /*
155 * The entry was found, so get its associated
156 * tmpfs_node.
157 */
158 tnode = de->td_node;
159
160 /*
161 * If we are not at the last path component and
162 * found a non-directory or non-link entry (which
163 * may itself be pointing to a directory), raise
164 * an error.
165 */
166 if ((tnode->tn_type != VDIR &&
167 tnode->tn_type != VLNK) &&
168 !(cnp->cn_flags & ISLASTCN)) {
169 error = ENOTDIR;
170 goto out;
171 }
172
173 /*
174 * If we are deleting or renaming the entry, keep
175 * track of its tmpfs_dirent so that it can be
176 * easily deleted later.
177 */
178 if ((cnp->cn_flags & ISLASTCN) &&
179 (cnp->cn_nameiop == DELETE ||
180 cnp->cn_nameiop == RENAME)) {
181 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
182 curthread);
183 if (error != 0)
184 goto out;
185
186 /* Allocate a new vnode on the matching entry. */
187 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
188 cnp->cn_lkflags, vpp);
189 if (error != 0)
190 goto out;
191
192 if ((dnode->tn_mode & S_ISTXT) &&
193 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
194 curthread) && VOP_ACCESS(*vpp, VADMIN,
195 cnp->cn_cred, curthread)) {
196 error = EPERM;
197 vput(*vpp);
198 *vpp = NULL;
199 goto out;
200 }
201 } else {
202 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
203 cnp->cn_lkflags, vpp);
204 if (error != 0)
205 goto out;
206 }
207 }
208 }
209
210 /*
211 * Store the result of this lookup in the cache. Avoid this if the
212 * request was for creation, as it does not improve timings on
213 * emprical tests.
214 */
215 if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
216 cache_enter(dvp, *vpp, cnp);
217
218 out:
219 #ifdef INVARIANTS
220 /*
221 * If there were no errors, *vpp cannot be null and it must be
222 * locked.
223 */
224 if (error == 0) {
225 MPASS(*vpp != NULLVP);
226 ASSERT_VOP_LOCKED(*vpp, __func__);
227 } else {
228 MPASS(*vpp == NULL);
229 }
230 #endif
231
232 return (error);
233 }
234
235 static int
tmpfs_cached_lookup(struct vop_cachedlookup_args * v)236 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
237 {
238
239 return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
240 }
241
242 static int
tmpfs_lookup(struct vop_lookup_args * v)243 tmpfs_lookup(struct vop_lookup_args *v)
244 {
245 struct vnode *dvp = v->a_dvp;
246 struct vnode **vpp = v->a_vpp;
247 struct componentname *cnp = v->a_cnp;
248 int error;
249
250 /* Check accessibility of requested node as a first step. */
251 error = vn_dir_check_exec(dvp, cnp);
252 if (error != 0)
253 return (error);
254
255 return (tmpfs_lookup1(dvp, vpp, cnp));
256 }
257
258 static int
tmpfs_create(struct vop_create_args * v)259 tmpfs_create(struct vop_create_args *v)
260 {
261 struct vnode *dvp = v->a_dvp;
262 struct vnode **vpp = v->a_vpp;
263 struct componentname *cnp = v->a_cnp;
264 struct vattr *vap = v->a_vap;
265 int error;
266
267 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
268
269 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
270 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
271 cache_enter(dvp, *vpp, cnp);
272 return (error);
273 }
274
275 static int
tmpfs_mknod(struct vop_mknod_args * v)276 tmpfs_mknod(struct vop_mknod_args *v)
277 {
278 struct vnode *dvp = v->a_dvp;
279 struct vnode **vpp = v->a_vpp;
280 struct componentname *cnp = v->a_cnp;
281 struct vattr *vap = v->a_vap;
282
283 if (vap->va_type != VBLK && vap->va_type != VCHR &&
284 vap->va_type != VFIFO)
285 return (EINVAL);
286
287 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
288 }
289
290 struct fileops tmpfs_fnops;
291
292 static int
tmpfs_open(struct vop_open_args * v)293 tmpfs_open(struct vop_open_args *v)
294 {
295 struct vnode *vp;
296 struct tmpfs_node *node;
297 struct file *fp;
298 int error, mode;
299
300 vp = v->a_vp;
301 mode = v->a_mode;
302 node = VP_TO_TMPFS_NODE(vp);
303
304 /*
305 * The file is still active but all its names have been removed
306 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
307 * it is about to die.
308 */
309 if (node->tn_links < 1)
310 return (ENOENT);
311
312 /* If the file is marked append-only, deny write requests. */
313 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
314 error = EPERM;
315 else {
316 error = 0;
317 /* For regular files, the call below is nop. */
318 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
319 OBJ_DEAD) == 0, ("dead object"));
320 vnode_create_vobject(vp, node->tn_size, v->a_td);
321 }
322
323 fp = v->a_fp;
324 MPASS(fp == NULL || fp->f_data == NULL);
325 if (error == 0 && fp != NULL && vp->v_type == VREG) {
326 tmpfs_ref_node(node);
327 finit_vnode(fp, mode, node, &tmpfs_fnops);
328 }
329
330 return (error);
331 }
332
333 static int
tmpfs_close(struct vop_close_args * v)334 tmpfs_close(struct vop_close_args *v)
335 {
336 struct vnode *vp = v->a_vp;
337
338 /* Update node times. */
339 tmpfs_update(vp);
340
341 return (0);
342 }
343
344 int
tmpfs_fo_close(struct file * fp,struct thread * td)345 tmpfs_fo_close(struct file *fp, struct thread *td)
346 {
347 struct tmpfs_node *node;
348
349 node = fp->f_data;
350 if (node != NULL) {
351 MPASS(node->tn_type == VREG);
352 tmpfs_free_node(node->tn_reg.tn_tmp, node);
353 }
354 return (vnops.fo_close(fp, td));
355 }
356
357 /*
358 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
359 * the comment above cache_fplookup for details.
360 */
361 int
tmpfs_fplookup_vexec(struct vop_fplookup_vexec_args * v)362 tmpfs_fplookup_vexec(struct vop_fplookup_vexec_args *v)
363 {
364 struct vnode *vp;
365 struct tmpfs_node *node;
366 struct ucred *cred;
367 mode_t all_x, mode;
368
369 vp = v->a_vp;
370 node = VP_TO_TMPFS_NODE_SMR(vp);
371 if (__predict_false(node == NULL))
372 return (EAGAIN);
373
374 all_x = S_IXUSR | S_IXGRP | S_IXOTH;
375 mode = atomic_load_short(&node->tn_mode);
376 if (__predict_true((mode & all_x) == all_x))
377 return (0);
378
379 cred = v->a_cred;
380 return (vaccess_vexec_smr(mode, node->tn_uid, node->tn_gid, cred));
381 }
382
383 static int
tmpfs_access_locked(struct vnode * vp,struct tmpfs_node * node,accmode_t accmode,struct ucred * cred)384 tmpfs_access_locked(struct vnode *vp, struct tmpfs_node *node,
385 accmode_t accmode, struct ucred *cred)
386 {
387 #ifdef DEBUG_VFS_LOCKS
388 if (!mtx_owned(TMPFS_NODE_MTX(node))) {
389 ASSERT_VOP_LOCKED(vp,
390 "tmpfs_access_locked needs locked vnode or node");
391 }
392 #endif
393
394 if ((accmode & VWRITE) != 0 && (node->tn_flags & IMMUTABLE) != 0)
395 return (EPERM);
396 return (vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid,
397 accmode, cred));
398 }
399
400 int
tmpfs_access(struct vop_access_args * v)401 tmpfs_access(struct vop_access_args *v)
402 {
403 struct vnode *vp = v->a_vp;
404 struct ucred *cred = v->a_cred;
405 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
406 mode_t all_x = S_IXUSR | S_IXGRP | S_IXOTH;
407 accmode_t accmode = v->a_accmode;
408
409 /*
410 * Common case path lookup.
411 */
412 if (__predict_true(accmode == VEXEC &&
413 (node->tn_mode & all_x) == all_x))
414 return (0);
415
416 switch (vp->v_type) {
417 case VDIR:
418 /* FALLTHROUGH */
419 case VLNK:
420 /* FALLTHROUGH */
421 case VREG:
422 if ((accmode & VWRITE) != 0 &&
423 (vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
424 return (EROFS);
425 break;
426
427 case VBLK:
428 /* FALLTHROUGH */
429 case VCHR:
430 /* FALLTHROUGH */
431 case VSOCK:
432 /* FALLTHROUGH */
433 case VFIFO:
434 break;
435
436 default:
437 return (EINVAL);
438 }
439
440 return (tmpfs_access_locked(vp, node, accmode, cred));
441 }
442
443 int
tmpfs_stat(struct vop_stat_args * v)444 tmpfs_stat(struct vop_stat_args *v)
445 {
446 struct vnode *vp = v->a_vp;
447 struct stat *sb = v->a_sb;
448 struct tmpfs_node *node;
449 int error;
450
451 node = VP_TO_TMPFS_NODE(vp);
452
453 tmpfs_update_getattr(vp);
454
455 error = vop_stat_helper_pre(v);
456 if (__predict_false(error))
457 return (error);
458
459 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
460 sb->st_ino = node->tn_id;
461 sb->st_mode = node->tn_mode | VTTOIF(vp->v_type);
462 sb->st_nlink = node->tn_links;
463 sb->st_uid = node->tn_uid;
464 sb->st_gid = node->tn_gid;
465 sb->st_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
466 node->tn_rdev : NODEV;
467 sb->st_size = node->tn_size;
468 sb->st_atim.tv_sec = node->tn_atime.tv_sec;
469 sb->st_atim.tv_nsec = node->tn_atime.tv_nsec;
470 sb->st_mtim.tv_sec = node->tn_mtime.tv_sec;
471 sb->st_mtim.tv_nsec = node->tn_mtime.tv_nsec;
472 sb->st_ctim.tv_sec = node->tn_ctime.tv_sec;
473 sb->st_ctim.tv_nsec = node->tn_ctime.tv_nsec;
474 sb->st_birthtim.tv_sec = node->tn_birthtime.tv_sec;
475 sb->st_birthtim.tv_nsec = node->tn_birthtime.tv_nsec;
476 sb->st_blksize = PAGE_SIZE;
477 sb->st_flags = node->tn_flags;
478 sb->st_gen = node->tn_gen;
479 sb->st_filerev = 0;
480 if (vp->v_type == VREG) {
481 #ifdef __ILP32__
482 vm_object_t obj = node->tn_reg.tn_aobj;
483
484 /* Handle torn read */
485 VM_OBJECT_RLOCK(obj);
486 #endif
487 sb->st_blocks = ptoa(node->tn_reg.tn_pages);
488 #ifdef __ILP32__
489 VM_OBJECT_RUNLOCK(obj);
490 #endif
491 } else {
492 sb->st_blocks = node->tn_size;
493 }
494 sb->st_blocks /= S_BLKSIZE;
495 return (vop_stat_helper_post(v, error));
496 }
497
498 int
tmpfs_getattr(struct vop_getattr_args * v)499 tmpfs_getattr(struct vop_getattr_args *v)
500 {
501 struct vnode *vp = v->a_vp;
502 struct vattr *vap = v->a_vap;
503 struct tmpfs_node *node;
504
505 node = VP_TO_TMPFS_NODE(vp);
506
507 tmpfs_update_getattr(vp);
508
509 vap->va_type = vp->v_type;
510 vap->va_mode = node->tn_mode;
511 vap->va_nlink = node->tn_links;
512 vap->va_uid = node->tn_uid;
513 vap->va_gid = node->tn_gid;
514 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
515 vap->va_fileid = node->tn_id;
516 vap->va_size = node->tn_size;
517 vap->va_blocksize = PAGE_SIZE;
518 vap->va_atime = node->tn_atime;
519 vap->va_mtime = node->tn_mtime;
520 vap->va_ctime = node->tn_ctime;
521 vap->va_birthtime = node->tn_birthtime;
522 vap->va_gen = node->tn_gen;
523 vap->va_flags = node->tn_flags;
524 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
525 node->tn_rdev : NODEV;
526 if (vp->v_type == VREG) {
527 #ifdef __ILP32__
528 vm_object_t obj = node->tn_reg.tn_aobj;
529
530 VM_OBJECT_RLOCK(obj);
531 #endif
532 vap->va_bytes = ptoa(node->tn_reg.tn_pages);
533 #ifdef __ILP32__
534 VM_OBJECT_RUNLOCK(obj);
535 #endif
536 } else {
537 vap->va_bytes = node->tn_size;
538 }
539 vap->va_filerev = 0;
540
541 return (0);
542 }
543
544 int
tmpfs_setattr(struct vop_setattr_args * v)545 tmpfs_setattr(struct vop_setattr_args *v)
546 {
547 struct vnode *vp = v->a_vp;
548 struct vattr *vap = v->a_vap;
549 struct ucred *cred = v->a_cred;
550 struct thread *td = curthread;
551
552 int error;
553
554 ASSERT_VOP_IN_SEQC(vp);
555
556 error = 0;
557
558 /* Abort if any unsettable attribute is given. */
559 if (vap->va_type != VNON ||
560 vap->va_nlink != VNOVAL ||
561 vap->va_fsid != VNOVAL ||
562 vap->va_fileid != VNOVAL ||
563 vap->va_blocksize != VNOVAL ||
564 vap->va_gen != VNOVAL ||
565 vap->va_rdev != VNOVAL ||
566 vap->va_bytes != VNOVAL)
567 error = EINVAL;
568
569 if (error == 0 && (vap->va_flags != VNOVAL))
570 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
571
572 if (error == 0 && (vap->va_size != VNOVAL))
573 error = tmpfs_chsize(vp, vap->va_size, cred, td);
574
575 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
576 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
577
578 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
579 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
580
581 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
582 vap->va_atime.tv_nsec != VNOVAL) ||
583 (vap->va_mtime.tv_sec != VNOVAL &&
584 vap->va_mtime.tv_nsec != VNOVAL) ||
585 (vap->va_birthtime.tv_sec != VNOVAL &&
586 vap->va_birthtime.tv_nsec != VNOVAL)))
587 error = tmpfs_chtimes(vp, vap, cred, td);
588
589 /*
590 * Update the node times. We give preference to the error codes
591 * generated by this function rather than the ones that may arise
592 * from tmpfs_update.
593 */
594 tmpfs_update(vp);
595
596 return (error);
597 }
598
599 static int
tmpfs_read(struct vop_read_args * v)600 tmpfs_read(struct vop_read_args *v)
601 {
602 struct vnode *vp;
603 struct uio *uio;
604 struct tmpfs_node *node;
605
606 vp = v->a_vp;
607 if (vp->v_type != VREG)
608 return (EISDIR);
609 uio = v->a_uio;
610 if (uio->uio_offset < 0)
611 return (EINVAL);
612 node = VP_TO_TMPFS_NODE(vp);
613 tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
614 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
615 }
616
617 static int
tmpfs_read_pgcache(struct vop_read_pgcache_args * v)618 tmpfs_read_pgcache(struct vop_read_pgcache_args *v)
619 {
620 struct vnode *vp;
621 struct tmpfs_node *node;
622 vm_object_t object;
623 off_t size;
624 int error;
625
626 vp = v->a_vp;
627 VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) != 0, vp);
628
629 if (v->a_uio->uio_offset < 0)
630 return (EINVAL);
631
632 error = EJUSTRETURN;
633 vfs_smr_enter();
634
635 node = VP_TO_TMPFS_NODE_SMR(vp);
636 if (node == NULL)
637 goto out_smr;
638 MPASS(node->tn_type == VREG);
639 MPASS(node->tn_refcount >= 1);
640 object = node->tn_reg.tn_aobj;
641 if (object == NULL)
642 goto out_smr;
643
644 MPASS(object->type == tmpfs_pager_type);
645 MPASS((object->flags & (OBJ_ANON | OBJ_DEAD | OBJ_SWAP)) ==
646 OBJ_SWAP);
647 if (!VN_IS_DOOMED(vp)) {
648 /* size cannot become shorter due to rangelock. */
649 size = node->tn_size;
650 tmpfs_set_accessed(node->tn_reg.tn_tmp, node);
651 vfs_smr_exit();
652 error = uiomove_object(object, size, v->a_uio);
653 return (error);
654 }
655 out_smr:
656 vfs_smr_exit();
657 return (error);
658 }
659
660 static int
tmpfs_write(struct vop_write_args * v)661 tmpfs_write(struct vop_write_args *v)
662 {
663 struct vnode *vp;
664 struct uio *uio;
665 struct tmpfs_node *node;
666 off_t oldsize;
667 ssize_t r;
668 int error, ioflag;
669 mode_t newmode;
670
671 vp = v->a_vp;
672 uio = v->a_uio;
673 ioflag = v->a_ioflag;
674 error = 0;
675 node = VP_TO_TMPFS_NODE(vp);
676 oldsize = node->tn_size;
677
678 if (uio->uio_offset < 0 || vp->v_type != VREG)
679 return (EINVAL);
680 if (uio->uio_resid == 0)
681 return (0);
682 if (ioflag & IO_APPEND)
683 uio->uio_offset = node->tn_size;
684 error = vn_rlimit_fsizex(vp, uio, VFS_TO_TMPFS(vp->v_mount)->
685 tm_maxfilesize, &r, uio->uio_td);
686 if (error != 0) {
687 vn_rlimit_fsizex_res(uio, r);
688 return (error);
689 }
690
691 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
692 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
693 FALSE);
694 if (error != 0)
695 goto out;
696 }
697
698 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
699 node->tn_status |= TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED;
700 node->tn_accessed = true;
701 if (node->tn_mode & (S_ISUID | S_ISGID)) {
702 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID)) {
703 newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
704 vn_seqc_write_begin(vp);
705 atomic_store_short(&node->tn_mode, newmode);
706 vn_seqc_write_end(vp);
707 }
708 }
709 if (error != 0)
710 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
711
712 out:
713 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
714 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
715
716 vn_rlimit_fsizex_res(uio, r);
717 return (error);
718 }
719
720 static int
tmpfs_deallocate(struct vop_deallocate_args * v)721 tmpfs_deallocate(struct vop_deallocate_args *v)
722 {
723 return (tmpfs_reg_punch_hole(v->a_vp, v->a_offset, v->a_len));
724 }
725
726 static int
tmpfs_fsync(struct vop_fsync_args * v)727 tmpfs_fsync(struct vop_fsync_args *v)
728 {
729 struct vnode *vp = v->a_vp;
730
731 tmpfs_check_mtime(vp);
732 tmpfs_update(vp);
733
734 return (0);
735 }
736
737 static int
tmpfs_remove(struct vop_remove_args * v)738 tmpfs_remove(struct vop_remove_args *v)
739 {
740 struct vnode *dvp = v->a_dvp;
741 struct vnode *vp = v->a_vp;
742
743 int error;
744 struct tmpfs_dirent *de;
745 struct tmpfs_mount *tmp;
746 struct tmpfs_node *dnode;
747 struct tmpfs_node *node;
748
749 if (vp->v_type == VDIR) {
750 error = EISDIR;
751 goto out;
752 }
753
754 dnode = VP_TO_TMPFS_DIR(dvp);
755 node = VP_TO_TMPFS_NODE(vp);
756 tmp = VFS_TO_TMPFS(vp->v_mount);
757 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
758 MPASS(de != NULL);
759
760 /* Files marked as immutable or append-only cannot be deleted. */
761 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
762 (dnode->tn_flags & APPEND)) {
763 error = EPERM;
764 goto out;
765 }
766
767 /* Remove the entry from the directory; as it is a file, we do not
768 * have to change the number of hard links of the directory. */
769 tmpfs_dir_detach(dvp, de);
770 if (v->a_cnp->cn_flags & DOWHITEOUT)
771 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
772
773 /* Free the directory entry we just deleted. Note that the node
774 * referred by it will not be removed until the vnode is really
775 * reclaimed. */
776 tmpfs_free_dirent(tmp, de);
777
778 node->tn_status |= TMPFS_NODE_CHANGED;
779 node->tn_accessed = true;
780 error = 0;
781
782 out:
783 return (error);
784 }
785
786 static int
tmpfs_link(struct vop_link_args * v)787 tmpfs_link(struct vop_link_args *v)
788 {
789 struct vnode *dvp = v->a_tdvp;
790 struct vnode *vp = v->a_vp;
791 struct componentname *cnp = v->a_cnp;
792
793 int error;
794 struct tmpfs_dirent *de;
795 struct tmpfs_node *node;
796
797 MPASS(dvp != vp); /* XXX When can this be false? */
798 node = VP_TO_TMPFS_NODE(vp);
799
800 /* Ensure that we do not overflow the maximum number of links imposed
801 * by the system. */
802 MPASS(node->tn_links <= TMPFS_LINK_MAX);
803 if (node->tn_links == TMPFS_LINK_MAX) {
804 error = EMLINK;
805 goto out;
806 }
807
808 /* We cannot create links of files marked immutable or append-only. */
809 if (node->tn_flags & (IMMUTABLE | APPEND)) {
810 error = EPERM;
811 goto out;
812 }
813
814 /* Allocate a new directory entry to represent the node. */
815 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
816 cnp->cn_nameptr, cnp->cn_namelen, &de);
817 if (error != 0)
818 goto out;
819
820 /* Insert the new directory entry into the appropriate directory. */
821 if (cnp->cn_flags & ISWHITEOUT)
822 tmpfs_dir_whiteout_remove(dvp, cnp);
823 tmpfs_dir_attach(dvp, de);
824
825 /* vp link count has changed, so update node times. */
826 node->tn_status |= TMPFS_NODE_CHANGED;
827 tmpfs_update(vp);
828
829 error = 0;
830
831 out:
832 return (error);
833 }
834
835 /*
836 * We acquire all but fdvp locks using non-blocking acquisitions. If we
837 * fail to acquire any lock in the path we will drop all held locks,
838 * acquire the new lock in a blocking fashion, and then release it and
839 * restart the rename. This acquire/release step ensures that we do not
840 * spin on a lock waiting for release. On error release all vnode locks
841 * and decrement references the way tmpfs_rename() would do.
842 */
843 static int
tmpfs_rename_relock(struct vnode * fdvp,struct vnode ** fvpp,struct vnode * tdvp,struct vnode ** tvpp,struct componentname * fcnp,struct componentname * tcnp)844 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
845 struct vnode *tdvp, struct vnode **tvpp,
846 struct componentname *fcnp, struct componentname *tcnp)
847 {
848 struct vnode *nvp;
849 struct mount *mp;
850 struct tmpfs_dirent *de;
851 int error, restarts = 0;
852
853 VOP_UNLOCK(tdvp);
854 if (*tvpp != NULL && *tvpp != tdvp)
855 VOP_UNLOCK(*tvpp);
856 mp = fdvp->v_mount;
857
858 relock:
859 restarts += 1;
860 error = vn_lock(fdvp, LK_EXCLUSIVE);
861 if (error)
862 goto releout;
863 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
864 VOP_UNLOCK(fdvp);
865 error = vn_lock(tdvp, LK_EXCLUSIVE);
866 if (error)
867 goto releout;
868 VOP_UNLOCK(tdvp);
869 goto relock;
870 }
871 /*
872 * Re-resolve fvp to be certain it still exists and fetch the
873 * correct vnode.
874 */
875 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
876 if (de == NULL) {
877 VOP_UNLOCK(fdvp);
878 VOP_UNLOCK(tdvp);
879 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
880 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
881 error = EINVAL;
882 else
883 error = ENOENT;
884 goto releout;
885 }
886 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
887 if (error != 0) {
888 VOP_UNLOCK(fdvp);
889 VOP_UNLOCK(tdvp);
890 if (error != EBUSY)
891 goto releout;
892 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
893 if (error != 0)
894 goto releout;
895 VOP_UNLOCK(nvp);
896 /*
897 * Concurrent rename race.
898 */
899 if (nvp == tdvp) {
900 vrele(nvp);
901 error = EINVAL;
902 goto releout;
903 }
904 vrele(*fvpp);
905 *fvpp = nvp;
906 goto relock;
907 }
908 vrele(*fvpp);
909 *fvpp = nvp;
910 VOP_UNLOCK(*fvpp);
911 /*
912 * Re-resolve tvp and acquire the vnode lock if present.
913 */
914 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
915 /*
916 * If tvp disappeared we just carry on.
917 */
918 if (de == NULL && *tvpp != NULL) {
919 vrele(*tvpp);
920 *tvpp = NULL;
921 }
922 /*
923 * Get the tvp ino if the lookup succeeded. We may have to restart
924 * if the non-blocking acquire fails.
925 */
926 if (de != NULL) {
927 nvp = NULL;
928 error = tmpfs_alloc_vp(mp, de->td_node,
929 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
930 if (*tvpp != NULL)
931 vrele(*tvpp);
932 *tvpp = nvp;
933 if (error != 0) {
934 VOP_UNLOCK(fdvp);
935 VOP_UNLOCK(tdvp);
936 if (error != EBUSY)
937 goto releout;
938 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
939 &nvp);
940 if (error != 0)
941 goto releout;
942 VOP_UNLOCK(nvp);
943 /*
944 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
945 */
946 if (nvp == fdvp) {
947 error = ENOTEMPTY;
948 goto releout;
949 }
950 goto relock;
951 }
952 }
953 tmpfs_rename_restarts += restarts;
954
955 return (0);
956
957 releout:
958 vrele(fdvp);
959 vrele(*fvpp);
960 vrele(tdvp);
961 if (*tvpp != NULL)
962 vrele(*tvpp);
963 tmpfs_rename_restarts += restarts;
964
965 return (error);
966 }
967
968 static int
tmpfs_rename(struct vop_rename_args * v)969 tmpfs_rename(struct vop_rename_args *v)
970 {
971 struct vnode *fdvp = v->a_fdvp;
972 struct vnode *fvp = v->a_fvp;
973 struct componentname *fcnp = v->a_fcnp;
974 struct vnode *tdvp = v->a_tdvp;
975 struct vnode *tvp = v->a_tvp;
976 struct componentname *tcnp = v->a_tcnp;
977 char *newname;
978 struct tmpfs_dirent *de;
979 struct tmpfs_mount *tmp;
980 struct tmpfs_node *fdnode;
981 struct tmpfs_node *fnode;
982 struct tmpfs_node *tnode;
983 struct tmpfs_node *tdnode;
984 int error;
985 bool want_seqc_end;
986
987 want_seqc_end = false;
988
989 /*
990 * Disallow cross-device renames.
991 * XXX Why isn't this done by the caller?
992 */
993 if (fvp->v_mount != tdvp->v_mount ||
994 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
995 error = EXDEV;
996 goto out;
997 }
998
999 /* If source and target are the same file, there is nothing to do. */
1000 if (fvp == tvp) {
1001 error = 0;
1002 goto out;
1003 }
1004
1005 /*
1006 * If we need to move the directory between entries, lock the
1007 * source so that we can safely operate on it.
1008 */
1009 if (fdvp != tdvp && fdvp != tvp) {
1010 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1011 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1012 fcnp, tcnp);
1013 if (error != 0)
1014 return (error);
1015 ASSERT_VOP_ELOCKED(fdvp,
1016 "tmpfs_rename: fdvp not locked");
1017 ASSERT_VOP_ELOCKED(tdvp,
1018 "tmpfs_rename: tdvp not locked");
1019 if (tvp != NULL)
1020 ASSERT_VOP_ELOCKED(tvp,
1021 "tmpfs_rename: tvp not locked");
1022 if (fvp == tvp) {
1023 error = 0;
1024 goto out_locked;
1025 }
1026 }
1027 }
1028
1029 /*
1030 * Avoid manipulating '.' and '..' entries.
1031 */
1032 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1033 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')) {
1034 error = EINVAL;
1035 goto out_locked;
1036 }
1037
1038 if (tvp != NULL)
1039 vn_seqc_write_begin(tvp);
1040 vn_seqc_write_begin(tdvp);
1041 vn_seqc_write_begin(fvp);
1042 vn_seqc_write_begin(fdvp);
1043 want_seqc_end = true;
1044
1045 tmp = VFS_TO_TMPFS(tdvp->v_mount);
1046 tdnode = VP_TO_TMPFS_DIR(tdvp);
1047 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1048 fdnode = VP_TO_TMPFS_DIR(fdvp);
1049 fnode = VP_TO_TMPFS_NODE(fvp);
1050 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1051
1052 /*
1053 * Entry can disappear before we lock fdvp.
1054 */
1055 if (de == NULL) {
1056 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1057 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1058 error = EINVAL;
1059 else
1060 error = ENOENT;
1061 goto out_locked;
1062 }
1063 MPASS(de->td_node == fnode);
1064
1065 /*
1066 * If re-naming a directory to another preexisting directory
1067 * ensure that the target directory is empty so that its
1068 * removal causes no side effects.
1069 * Kern_rename guarantees the destination to be a directory
1070 * if the source is one.
1071 */
1072 if (tvp != NULL) {
1073 MPASS(tnode != NULL);
1074
1075 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1076 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1077 error = EPERM;
1078 goto out_locked;
1079 }
1080
1081 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1082 if (tnode->tn_size != 0 &&
1083 ((tcnp->cn_flags & IGNOREWHITEOUT) == 0 ||
1084 tnode->tn_size > tnode->tn_dir.tn_wht_size)) {
1085 error = ENOTEMPTY;
1086 goto out_locked;
1087 }
1088 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1089 error = ENOTDIR;
1090 goto out_locked;
1091 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1092 error = EISDIR;
1093 goto out_locked;
1094 } else {
1095 MPASS(fnode->tn_type != VDIR &&
1096 tnode->tn_type != VDIR);
1097 }
1098 }
1099
1100 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1101 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1102 error = EPERM;
1103 goto out_locked;
1104 }
1105
1106 /*
1107 * Ensure that we have enough memory to hold the new name, if it
1108 * has to be changed.
1109 */
1110 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1111 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1112 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1113 } else
1114 newname = NULL;
1115
1116 /*
1117 * If the node is being moved to another directory, we have to do
1118 * the move.
1119 */
1120 if (fdnode != tdnode) {
1121 /*
1122 * In case we are moving a directory, we have to adjust its
1123 * parent to point to the new parent.
1124 */
1125 if (de->td_node->tn_type == VDIR) {
1126 struct tmpfs_node *n;
1127
1128 TMPFS_NODE_LOCK(fnode);
1129 error = tmpfs_access_locked(fvp, fnode, VWRITE,
1130 tcnp->cn_cred);
1131 TMPFS_NODE_UNLOCK(fnode);
1132 if (error) {
1133 if (newname != NULL)
1134 free(newname, M_TMPFSNAME);
1135 goto out_locked;
1136 }
1137
1138 /*
1139 * Ensure the target directory is not a child of the
1140 * directory being moved. Otherwise, we'd end up
1141 * with stale nodes.
1142 */
1143 n = tdnode;
1144 /*
1145 * TMPFS_LOCK guaranties that no nodes are freed while
1146 * traversing the list. Nodes can only be marked as
1147 * removed: tn_parent == NULL.
1148 */
1149 TMPFS_LOCK(tmp);
1150 TMPFS_NODE_LOCK(n);
1151 while (n != n->tn_dir.tn_parent) {
1152 struct tmpfs_node *parent;
1153
1154 if (n == fnode) {
1155 TMPFS_NODE_UNLOCK(n);
1156 TMPFS_UNLOCK(tmp);
1157 error = EINVAL;
1158 if (newname != NULL)
1159 free(newname, M_TMPFSNAME);
1160 goto out_locked;
1161 }
1162 parent = n->tn_dir.tn_parent;
1163 TMPFS_NODE_UNLOCK(n);
1164 if (parent == NULL) {
1165 n = NULL;
1166 break;
1167 }
1168 TMPFS_NODE_LOCK(parent);
1169 if (parent->tn_dir.tn_parent == NULL) {
1170 TMPFS_NODE_UNLOCK(parent);
1171 n = NULL;
1172 break;
1173 }
1174 n = parent;
1175 }
1176 TMPFS_UNLOCK(tmp);
1177 if (n == NULL) {
1178 error = EINVAL;
1179 if (newname != NULL)
1180 free(newname, M_TMPFSNAME);
1181 goto out_locked;
1182 }
1183 TMPFS_NODE_UNLOCK(n);
1184
1185 /* Adjust the parent pointer. */
1186 TMPFS_VALIDATE_DIR(fnode);
1187 TMPFS_NODE_LOCK(de->td_node);
1188 de->td_node->tn_dir.tn_parent = tdnode;
1189 TMPFS_NODE_UNLOCK(de->td_node);
1190
1191 /*
1192 * As a result of changing the target of the '..'
1193 * entry, the link count of the source and target
1194 * directories has to be adjusted.
1195 */
1196 TMPFS_NODE_LOCK(tdnode);
1197 TMPFS_ASSERT_LOCKED(tdnode);
1198 tdnode->tn_links++;
1199 TMPFS_NODE_UNLOCK(tdnode);
1200
1201 TMPFS_NODE_LOCK(fdnode);
1202 TMPFS_ASSERT_LOCKED(fdnode);
1203 fdnode->tn_links--;
1204 TMPFS_NODE_UNLOCK(fdnode);
1205 }
1206 }
1207
1208 /*
1209 * Do the move: just remove the entry from the source directory
1210 * and insert it into the target one.
1211 */
1212 tmpfs_dir_detach(fdvp, de);
1213
1214 if (fcnp->cn_flags & DOWHITEOUT)
1215 tmpfs_dir_whiteout_add(fdvp, fcnp);
1216 if (tcnp->cn_flags & ISWHITEOUT)
1217 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1218
1219 /*
1220 * If the name has changed, we need to make it effective by changing
1221 * it in the directory entry.
1222 */
1223 if (newname != NULL) {
1224 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1225
1226 free(de->ud.td_name, M_TMPFSNAME);
1227 de->ud.td_name = newname;
1228 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1229
1230 fnode->tn_status |= TMPFS_NODE_CHANGED;
1231 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1232 }
1233
1234 /*
1235 * If we are overwriting an entry, we have to remove the old one
1236 * from the target directory.
1237 */
1238 if (tvp != NULL) {
1239 struct tmpfs_dirent *tde;
1240
1241 /* Remove the old entry from the target directory. */
1242 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1243 tmpfs_dir_detach(tdvp, tde);
1244
1245 /*
1246 * If we are overwriting a directory, per the ENOTEMPTY check
1247 * above it must either be empty or contain only whiteout
1248 * entries. In the latter case (which can only happen if
1249 * IGNOREWHITEOUT was passed in tcnp->cn_flags), clear the
1250 * whiteout entries to avoid leaking memory.
1251 */
1252 if (tnode->tn_type == VDIR && tnode->tn_size > 0)
1253 tmpfs_dir_clear_whiteouts(tvp);
1254
1255 /* Update node's ctime because of possible hardlinks. */
1256 tnode->tn_status |= TMPFS_NODE_CHANGED;
1257 tmpfs_update(tvp);
1258
1259 /*
1260 * Free the directory entry we just deleted. Note that the
1261 * node referred by it will not be removed until the vnode is
1262 * really reclaimed.
1263 */
1264 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1265 }
1266
1267 tmpfs_dir_attach(tdvp, de);
1268
1269 if (tmpfs_use_nc(fvp)) {
1270 cache_vop_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
1271 }
1272
1273 error = 0;
1274
1275 out_locked:
1276 if (fdvp != tdvp && fdvp != tvp)
1277 VOP_UNLOCK(fdvp);
1278
1279 out:
1280 if (want_seqc_end) {
1281 if (tvp != NULL)
1282 vn_seqc_write_end(tvp);
1283 vn_seqc_write_end(tdvp);
1284 vn_seqc_write_end(fvp);
1285 vn_seqc_write_end(fdvp);
1286 }
1287
1288 /*
1289 * Release target nodes.
1290 * XXX: I don't understand when tdvp can be the same as tvp, but
1291 * other code takes care of this...
1292 */
1293 if (tdvp == tvp)
1294 vrele(tdvp);
1295 else
1296 vput(tdvp);
1297 if (tvp != NULL)
1298 vput(tvp);
1299
1300 /* Release source nodes. */
1301 vrele(fdvp);
1302 vrele(fvp);
1303
1304 return (error);
1305 }
1306
1307 static int
tmpfs_mkdir(struct vop_mkdir_args * v)1308 tmpfs_mkdir(struct vop_mkdir_args *v)
1309 {
1310 struct vnode *dvp = v->a_dvp;
1311 struct vnode **vpp = v->a_vpp;
1312 struct componentname *cnp = v->a_cnp;
1313 struct vattr *vap = v->a_vap;
1314
1315 MPASS(vap->va_type == VDIR);
1316
1317 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
1318 }
1319
1320 static int
tmpfs_rmdir(struct vop_rmdir_args * v)1321 tmpfs_rmdir(struct vop_rmdir_args *v)
1322 {
1323 struct vnode *dvp = v->a_dvp;
1324 struct vnode *vp = v->a_vp;
1325 struct componentname *cnp = v->a_cnp;
1326
1327 int error;
1328 struct tmpfs_dirent *de;
1329 struct tmpfs_mount *tmp;
1330 struct tmpfs_node *dnode;
1331 struct tmpfs_node *node;
1332
1333 tmp = VFS_TO_TMPFS(dvp->v_mount);
1334 dnode = VP_TO_TMPFS_DIR(dvp);
1335 node = VP_TO_TMPFS_DIR(vp);
1336
1337 /*
1338 * Directories with more than two non-whiteout entries ('.' and '..')
1339 * cannot be removed.
1340 */
1341 if (node->tn_size != 0 &&
1342 ((cnp->cn_flags & IGNOREWHITEOUT) == 0 ||
1343 node->tn_size > node->tn_dir.tn_wht_size)) {
1344 error = ENOTEMPTY;
1345 goto out;
1346 }
1347
1348 /* Check flags to see if we are allowed to remove the directory. */
1349 if ((dnode->tn_flags & APPEND)
1350 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1351 error = EPERM;
1352 goto out;
1353 }
1354
1355 /* This invariant holds only if we are not trying to remove "..".
1356 * We checked for that above so this is safe now. */
1357 MPASS(node->tn_dir.tn_parent == dnode);
1358
1359 /* Get the directory entry associated with node (vp). This was
1360 * filled by tmpfs_lookup while looking up the entry. */
1361 de = tmpfs_dir_lookup(dnode, node, cnp);
1362 MPASS(TMPFS_DIRENT_MATCHES(de,
1363 cnp->cn_nameptr,
1364 cnp->cn_namelen));
1365
1366 /* Detach the directory entry from the directory (dnode). */
1367 tmpfs_dir_detach(dvp, de);
1368
1369 /*
1370 * If we are removing a directory, per the ENOTEMPTY check above it
1371 * must either be empty or contain only whiteout entries. In the
1372 * latter case (which can only happen if IGNOREWHITEOUT was passed
1373 * in cnp->cn_flags), clear the whiteout entries to avoid leaking
1374 * memory.
1375 */
1376 if (node->tn_size > 0)
1377 tmpfs_dir_clear_whiteouts(vp);
1378
1379 if (cnp->cn_flags & DOWHITEOUT)
1380 tmpfs_dir_whiteout_add(dvp, cnp);
1381
1382 /* No vnode should be allocated for this entry from this point */
1383 TMPFS_NODE_LOCK(node);
1384 node->tn_links--;
1385 node->tn_dir.tn_parent = NULL;
1386 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1387 node->tn_accessed = true;
1388
1389 TMPFS_NODE_UNLOCK(node);
1390
1391 TMPFS_NODE_LOCK(dnode);
1392 dnode->tn_links--;
1393 dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1394 dnode->tn_accessed = true;
1395 TMPFS_NODE_UNLOCK(dnode);
1396
1397 if (tmpfs_use_nc(dvp)) {
1398 cache_vop_rmdir(dvp, vp);
1399 }
1400
1401 /* Free the directory entry we just deleted. Note that the node
1402 * referred by it will not be removed until the vnode is really
1403 * reclaimed. */
1404 tmpfs_free_dirent(tmp, de);
1405
1406 /* Release the deleted vnode (will destroy the node, notify
1407 * interested parties and clean it from the cache). */
1408
1409 dnode->tn_status |= TMPFS_NODE_CHANGED;
1410 tmpfs_update(dvp);
1411
1412 error = 0;
1413
1414 out:
1415 return (error);
1416 }
1417
1418 static int
tmpfs_symlink(struct vop_symlink_args * v)1419 tmpfs_symlink(struct vop_symlink_args *v)
1420 {
1421 struct vnode *dvp = v->a_dvp;
1422 struct vnode **vpp = v->a_vpp;
1423 struct componentname *cnp = v->a_cnp;
1424 struct vattr *vap = v->a_vap;
1425 const char *target = v->a_target;
1426
1427 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1428 MPASS(vap->va_type == VLNK);
1429 #else
1430 vap->va_type = VLNK;
1431 #endif
1432
1433 return (tmpfs_alloc_file(dvp, vpp, vap, cnp, target));
1434 }
1435
1436 static int
tmpfs_readdir(struct vop_readdir_args * va)1437 tmpfs_readdir(struct vop_readdir_args *va)
1438 {
1439 struct vnode *vp;
1440 struct uio *uio;
1441 struct tmpfs_mount *tm;
1442 struct tmpfs_node *node;
1443 uint64_t **cookies;
1444 int *eofflag, *ncookies;
1445 ssize_t startresid;
1446 int error, maxcookies;
1447
1448 vp = va->a_vp;
1449 uio = va->a_uio;
1450 eofflag = va->a_eofflag;
1451 cookies = va->a_cookies;
1452 ncookies = va->a_ncookies;
1453
1454 /* This operation only makes sense on directory nodes. */
1455 if (vp->v_type != VDIR)
1456 return (ENOTDIR);
1457
1458 maxcookies = 0;
1459 node = VP_TO_TMPFS_DIR(vp);
1460 tm = VFS_TO_TMPFS(vp->v_mount);
1461
1462 startresid = uio->uio_resid;
1463
1464 /* Allocate cookies for NFS and compat modules. */
1465 if (cookies != NULL && ncookies != NULL) {
1466 maxcookies = howmany(node->tn_size,
1467 sizeof(struct tmpfs_dirent)) + 2;
1468 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1469 M_WAITOK);
1470 *ncookies = 0;
1471 }
1472
1473 if (cookies == NULL)
1474 error = tmpfs_dir_getdents(tm, node, uio, 0, NULL, NULL);
1475 else
1476 error = tmpfs_dir_getdents(tm, node, uio, maxcookies, *cookies,
1477 ncookies);
1478
1479 /* Buffer was filled without hitting EOF. */
1480 if (error == EJUSTRETURN)
1481 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1482
1483 if (error != 0 && cookies != NULL && ncookies != NULL) {
1484 free(*cookies, M_TEMP);
1485 *cookies = NULL;
1486 *ncookies = 0;
1487 }
1488
1489 if (eofflag != NULL)
1490 *eofflag =
1491 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1492
1493 return (error);
1494 }
1495
1496 static int
tmpfs_readlink(struct vop_readlink_args * v)1497 tmpfs_readlink(struct vop_readlink_args *v)
1498 {
1499 struct vnode *vp = v->a_vp;
1500 struct uio *uio = v->a_uio;
1501
1502 int error;
1503 struct tmpfs_node *node;
1504
1505 MPASS(uio->uio_offset == 0);
1506 MPASS(vp->v_type == VLNK);
1507
1508 node = VP_TO_TMPFS_NODE(vp);
1509
1510 error = uiomove(node->tn_link_target, MIN(node->tn_size, uio->uio_resid),
1511 uio);
1512 tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
1513
1514 return (error);
1515 }
1516
1517 /*
1518 * VOP_FPLOOKUP_SYMLINK routines are subject to special circumstances, see
1519 * the comment above cache_fplookup for details.
1520 *
1521 * Check tmpfs_alloc_node for tmpfs-specific synchronisation notes.
1522 */
1523 static int
tmpfs_fplookup_symlink(struct vop_fplookup_symlink_args * v)1524 tmpfs_fplookup_symlink(struct vop_fplookup_symlink_args *v)
1525 {
1526 struct vnode *vp;
1527 struct tmpfs_node *node;
1528 char *symlink;
1529
1530 vp = v->a_vp;
1531 node = VP_TO_TMPFS_NODE_SMR(vp);
1532 if (__predict_false(node == NULL))
1533 return (EAGAIN);
1534 if (!atomic_load_char(&node->tn_link_smr))
1535 return (EAGAIN);
1536 symlink = atomic_load_ptr(&node->tn_link_target);
1537 if (symlink == NULL)
1538 return (EAGAIN);
1539
1540 return (cache_symlink_resolve(v->a_fpl, symlink, node->tn_size));
1541 }
1542
1543 static int
tmpfs_inactive(struct vop_inactive_args * v)1544 tmpfs_inactive(struct vop_inactive_args *v)
1545 {
1546 struct vnode *vp;
1547 struct tmpfs_node *node;
1548
1549 vp = v->a_vp;
1550 node = VP_TO_TMPFS_NODE(vp);
1551 if (node->tn_links == 0)
1552 vrecycle(vp);
1553 else
1554 tmpfs_check_mtime(vp);
1555 return (0);
1556 }
1557
1558 static int
tmpfs_need_inactive(struct vop_need_inactive_args * ap)1559 tmpfs_need_inactive(struct vop_need_inactive_args *ap)
1560 {
1561 struct vnode *vp;
1562 struct tmpfs_node *node;
1563 struct vm_object *obj;
1564
1565 vp = ap->a_vp;
1566 node = VP_TO_TMPFS_NODE(vp);
1567 if (node->tn_links == 0)
1568 goto need;
1569 if (vp->v_type == VREG) {
1570 obj = vp->v_object;
1571 if (obj->generation != obj->cleangeneration)
1572 goto need;
1573 }
1574 return (0);
1575 need:
1576 return (1);
1577 }
1578
1579 int
tmpfs_reclaim(struct vop_reclaim_args * v)1580 tmpfs_reclaim(struct vop_reclaim_args *v)
1581 {
1582 struct vnode *vp;
1583 struct tmpfs_mount *tmp;
1584 struct tmpfs_node *node;
1585 bool unlock;
1586
1587 vp = v->a_vp;
1588 node = VP_TO_TMPFS_NODE(vp);
1589 tmp = VFS_TO_TMPFS(vp->v_mount);
1590
1591 if (vp->v_type == VREG)
1592 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1593 vp->v_object = NULL;
1594
1595 TMPFS_LOCK(tmp);
1596 TMPFS_NODE_LOCK(node);
1597 tmpfs_free_vp(vp);
1598
1599 /*
1600 * If the node referenced by this vnode was deleted by the user,
1601 * we must free its associated data structures (now that the vnode
1602 * is being reclaimed).
1603 */
1604 unlock = true;
1605 if (node->tn_links == 0 &&
1606 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1607 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1608 unlock = !tmpfs_free_node_locked(tmp, node, true);
1609 }
1610
1611 if (unlock) {
1612 TMPFS_NODE_UNLOCK(node);
1613 TMPFS_UNLOCK(tmp);
1614 }
1615
1616 MPASS(vp->v_data == NULL);
1617 return (0);
1618 }
1619
1620 int
tmpfs_print(struct vop_print_args * v)1621 tmpfs_print(struct vop_print_args *v)
1622 {
1623 struct vnode *vp = v->a_vp;
1624
1625 struct tmpfs_node *node;
1626
1627 node = VP_TO_TMPFS_NODE(vp);
1628
1629 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %jd\n",
1630 node, node->tn_flags, (uintmax_t)node->tn_links);
1631 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1632 node->tn_mode, node->tn_uid, node->tn_gid,
1633 (intmax_t)node->tn_size, node->tn_status);
1634
1635 if (vp->v_type == VFIFO)
1636 fifo_printinfo(vp);
1637
1638 printf("\n");
1639
1640 return (0);
1641 }
1642
1643 int
tmpfs_pathconf(struct vop_pathconf_args * v)1644 tmpfs_pathconf(struct vop_pathconf_args *v)
1645 {
1646 struct vnode *vp = v->a_vp;
1647 int name = v->a_name;
1648 long *retval = v->a_retval;
1649
1650 int error;
1651
1652 error = 0;
1653
1654 switch (name) {
1655 case _PC_LINK_MAX:
1656 *retval = TMPFS_LINK_MAX;
1657 break;
1658
1659 case _PC_SYMLINK_MAX:
1660 *retval = MAXPATHLEN;
1661 break;
1662
1663 case _PC_NAME_MAX:
1664 *retval = NAME_MAX;
1665 break;
1666
1667 case _PC_PIPE_BUF:
1668 if (vp->v_type == VDIR || vp->v_type == VFIFO)
1669 *retval = PIPE_BUF;
1670 else
1671 error = EINVAL;
1672 break;
1673
1674 case _PC_CHOWN_RESTRICTED:
1675 *retval = 1;
1676 break;
1677
1678 case _PC_NO_TRUNC:
1679 *retval = 1;
1680 break;
1681
1682 case _PC_SYNC_IO:
1683 *retval = 1;
1684 break;
1685
1686 case _PC_FILESIZEBITS:
1687 *retval = 64;
1688 break;
1689
1690 case _PC_MIN_HOLE_SIZE:
1691 *retval = PAGE_SIZE;
1692 break;
1693
1694 case _PC_HAS_HIDDENSYSTEM:
1695 *retval = 1;
1696 break;
1697
1698 default:
1699 error = vop_stdpathconf(v);
1700 }
1701
1702 return (error);
1703 }
1704
1705 static int
tmpfs_vptofh(struct vop_vptofh_args * ap)1706 tmpfs_vptofh(struct vop_vptofh_args *ap)
1707 /*
1708 vop_vptofh {
1709 IN struct vnode *a_vp;
1710 IN struct fid *a_fhp;
1711 };
1712 */
1713 {
1714 struct tmpfs_fid_data *const tfd = (struct tmpfs_fid_data *)ap->a_fhp;
1715 struct tmpfs_node *node;
1716 _Static_assert(sizeof(struct tmpfs_fid_data) <= sizeof(struct fid),
1717 "struct tmpfs_fid_data cannot be larger than struct fid");
1718
1719 node = VP_TO_TMPFS_NODE(ap->a_vp);
1720 tfd->tfd_len = sizeof(*tfd);
1721 tfd->tfd_gen = node->tn_gen;
1722 tfd->tfd_id = node->tn_id;
1723
1724 return (0);
1725 }
1726
1727 static int
tmpfs_whiteout(struct vop_whiteout_args * ap)1728 tmpfs_whiteout(struct vop_whiteout_args *ap)
1729 {
1730 struct vnode *dvp = ap->a_dvp;
1731 struct componentname *cnp = ap->a_cnp;
1732 struct tmpfs_dirent *de;
1733
1734 switch (ap->a_flags) {
1735 case LOOKUP:
1736 return (0);
1737 case CREATE:
1738 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1739 if (de != NULL)
1740 return (de->td_node == NULL ? 0 : EEXIST);
1741 return (tmpfs_dir_whiteout_add(dvp, cnp));
1742 case DELETE:
1743 tmpfs_dir_whiteout_remove(dvp, cnp);
1744 return (0);
1745 default:
1746 panic("tmpfs_whiteout: unknown op");
1747 }
1748 }
1749
1750 static int
tmpfs_vptocnp_dir(struct tmpfs_node * tn,struct tmpfs_node * tnp,struct tmpfs_dirent ** pde)1751 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1752 struct tmpfs_dirent **pde)
1753 {
1754 struct tmpfs_dir_cursor dc;
1755 struct tmpfs_dirent *de;
1756
1757 for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1758 de = tmpfs_dir_next(tnp, &dc)) {
1759 if (de->td_node == tn) {
1760 *pde = de;
1761 return (0);
1762 }
1763 }
1764 return (ENOENT);
1765 }
1766
1767 static int
tmpfs_vptocnp_fill(struct vnode * vp,struct tmpfs_node * tn,struct tmpfs_node * tnp,char * buf,size_t * buflen,struct vnode ** dvp)1768 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1769 struct tmpfs_node *tnp, char *buf, size_t *buflen, struct vnode **dvp)
1770 {
1771 struct tmpfs_dirent *de;
1772 int error, i;
1773
1774 error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1775 dvp);
1776 if (error != 0)
1777 return (error);
1778 error = tmpfs_vptocnp_dir(tn, tnp, &de);
1779 if (error == 0) {
1780 i = *buflen;
1781 i -= de->td_namelen;
1782 if (i < 0) {
1783 error = ENOMEM;
1784 } else {
1785 bcopy(de->ud.td_name, buf + i, de->td_namelen);
1786 *buflen = i;
1787 }
1788 }
1789 if (error == 0) {
1790 if (vp != *dvp)
1791 VOP_UNLOCK(*dvp);
1792 } else {
1793 if (vp != *dvp)
1794 vput(*dvp);
1795 else
1796 vrele(vp);
1797 }
1798 return (error);
1799 }
1800
1801 static int
tmpfs_vptocnp(struct vop_vptocnp_args * ap)1802 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1803 {
1804 struct vnode *vp, **dvp;
1805 struct tmpfs_node *tn, *tnp, *tnp1;
1806 struct tmpfs_dirent *de;
1807 struct tmpfs_mount *tm;
1808 char *buf;
1809 size_t *buflen;
1810 int error;
1811
1812 vp = ap->a_vp;
1813 dvp = ap->a_vpp;
1814 buf = ap->a_buf;
1815 buflen = ap->a_buflen;
1816
1817 tm = VFS_TO_TMPFS(vp->v_mount);
1818 tn = VP_TO_TMPFS_NODE(vp);
1819 if (tn->tn_type == VDIR) {
1820 tnp = tn->tn_dir.tn_parent;
1821 if (tnp == NULL)
1822 return (ENOENT);
1823 tmpfs_ref_node(tnp);
1824 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1825 buflen, dvp);
1826 tmpfs_free_node(tm, tnp);
1827 return (error);
1828 }
1829 restart:
1830 TMPFS_LOCK(tm);
1831 restart_locked:
1832 LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1833 if (tnp->tn_type != VDIR)
1834 continue;
1835 TMPFS_NODE_LOCK(tnp);
1836 tmpfs_ref_node(tnp);
1837
1838 /*
1839 * tn_vnode cannot be instantiated while we hold the
1840 * node lock, so the directory cannot be changed while
1841 * we iterate over it. Do this to avoid instantiating
1842 * vnode for directories which cannot point to our
1843 * node.
1844 */
1845 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1846 &de) : 0;
1847
1848 if (error == 0) {
1849 TMPFS_NODE_UNLOCK(tnp);
1850 TMPFS_UNLOCK(tm);
1851 error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1852 dvp);
1853 if (error == 0) {
1854 tmpfs_free_node(tm, tnp);
1855 return (0);
1856 }
1857 if (VN_IS_DOOMED(vp)) {
1858 tmpfs_free_node(tm, tnp);
1859 return (ENOENT);
1860 }
1861 TMPFS_LOCK(tm);
1862 TMPFS_NODE_LOCK(tnp);
1863 }
1864 if (tmpfs_free_node_locked(tm, tnp, false)) {
1865 goto restart;
1866 } else {
1867 KASSERT(tnp->tn_refcount > 0,
1868 ("node %p refcount zero", tnp));
1869 if (tnp->tn_attached) {
1870 tnp1 = LIST_NEXT(tnp, tn_entries);
1871 TMPFS_NODE_UNLOCK(tnp);
1872 } else {
1873 TMPFS_NODE_UNLOCK(tnp);
1874 goto restart_locked;
1875 }
1876 }
1877 }
1878 TMPFS_UNLOCK(tm);
1879 return (ENOENT);
1880 }
1881
1882 void
tmpfs_extattr_free(struct tmpfs_extattr * ea)1883 tmpfs_extattr_free(struct tmpfs_extattr *ea)
1884 {
1885 free(ea->ea_name, M_TMPFSEA);
1886 free(ea->ea_value, M_TMPFSEA);
1887 free(ea, M_TMPFSEA);
1888 }
1889
1890 static bool
tmpfs_extattr_update_mem(struct tmpfs_mount * tmp,ssize_t size)1891 tmpfs_extattr_update_mem(struct tmpfs_mount *tmp, ssize_t size)
1892 {
1893 TMPFS_LOCK(tmp);
1894 if (size > 0 &&
1895 !tmpfs_pages_check_avail(tmp, howmany(size, PAGE_SIZE))) {
1896 TMPFS_UNLOCK(tmp);
1897 return (false);
1898 }
1899 if (tmp->tm_ea_memory_inuse + size > tmp->tm_ea_memory_max) {
1900 TMPFS_UNLOCK(tmp);
1901 return (false);
1902 }
1903 tmp->tm_ea_memory_inuse += size;
1904 TMPFS_UNLOCK(tmp);
1905 return (true);
1906 }
1907
1908 static int
tmpfs_deleteextattr(struct vop_deleteextattr_args * ap)1909 tmpfs_deleteextattr(struct vop_deleteextattr_args *ap)
1910 {
1911 struct vnode *vp = ap->a_vp;
1912 struct tmpfs_mount *tmp;
1913 struct tmpfs_node *node;
1914 struct tmpfs_extattr *ea;
1915 size_t namelen;
1916 ssize_t diff;
1917 int error;
1918
1919 node = VP_TO_TMPFS_NODE(vp);
1920 tmp = VFS_TO_TMPFS(vp->v_mount);
1921 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1922 return (EOPNOTSUPP);
1923 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1924 ap->a_cred, ap->a_td, VWRITE);
1925 if (error != 0)
1926 return (error);
1927 if (ap->a_name == NULL || ap->a_name[0] == '\0')
1928 return (EINVAL);
1929 namelen = strlen(ap->a_name);
1930 if (namelen > EXTATTR_MAXNAMELEN)
1931 return (EINVAL);
1932
1933 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
1934 if (ea->ea_namespace == ap->a_attrnamespace &&
1935 namelen == ea->ea_namelen &&
1936 memcmp(ap->a_name, ea->ea_name, namelen) == 0)
1937 break;
1938 }
1939
1940 if (ea == NULL)
1941 return (ENOATTR);
1942 LIST_REMOVE(ea, ea_extattrs);
1943 diff = -(sizeof(struct tmpfs_extattr) + namelen + ea->ea_size);
1944 tmpfs_extattr_update_mem(tmp, diff);
1945 tmpfs_extattr_free(ea);
1946 return (0);
1947 }
1948
1949 static int
tmpfs_getextattr(struct vop_getextattr_args * ap)1950 tmpfs_getextattr(struct vop_getextattr_args *ap)
1951 {
1952 struct vnode *vp = ap->a_vp;
1953 struct tmpfs_node *node;
1954 struct tmpfs_extattr *ea;
1955 size_t namelen;
1956 int error;
1957
1958 node = VP_TO_TMPFS_NODE(vp);
1959 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1960 return (EOPNOTSUPP);
1961 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1962 ap->a_cred, ap->a_td, VREAD);
1963 if (error != 0)
1964 return (error);
1965 if (ap->a_name == NULL || ap->a_name[0] == '\0')
1966 return (EINVAL);
1967 namelen = strlen(ap->a_name);
1968 if (namelen > EXTATTR_MAXNAMELEN)
1969 return (EINVAL);
1970
1971 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
1972 if (ea->ea_namespace == ap->a_attrnamespace &&
1973 namelen == ea->ea_namelen &&
1974 memcmp(ap->a_name, ea->ea_name, namelen) == 0)
1975 break;
1976 }
1977
1978 if (ea == NULL)
1979 return (ENOATTR);
1980 if (ap->a_size != NULL)
1981 *ap->a_size = ea->ea_size;
1982 if (ap->a_uio != NULL && ea->ea_size != 0)
1983 error = uiomove(ea->ea_value, ea->ea_size, ap->a_uio);
1984 return (error);
1985 }
1986
1987 static int
tmpfs_listextattr(struct vop_listextattr_args * ap)1988 tmpfs_listextattr(struct vop_listextattr_args *ap)
1989 {
1990 struct vnode *vp = ap->a_vp;
1991 struct tmpfs_node *node;
1992 struct tmpfs_extattr *ea;
1993 int error;
1994
1995 node = VP_TO_TMPFS_NODE(vp);
1996 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1997 return (EOPNOTSUPP);
1998 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1999 ap->a_cred, ap->a_td, VREAD);
2000 if (error != 0)
2001 return (error);
2002 if (ap->a_size != NULL)
2003 *ap->a_size = 0;
2004
2005 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
2006 if (ea->ea_namespace != ap->a_attrnamespace)
2007 continue;
2008 if (ap->a_size != NULL)
2009 *ap->a_size += ea->ea_namelen + 1;
2010 if (ap->a_uio != NULL) {
2011 error = uiomove(&ea->ea_namelen, 1, ap->a_uio);
2012 if (error != 0)
2013 break;
2014 error = uiomove(ea->ea_name, ea->ea_namelen, ap->a_uio);
2015 if (error != 0)
2016 break;
2017 }
2018 }
2019
2020 return (error);
2021 }
2022
2023 static int
tmpfs_setextattr(struct vop_setextattr_args * ap)2024 tmpfs_setextattr(struct vop_setextattr_args *ap)
2025 {
2026 struct vnode *vp = ap->a_vp;
2027 struct tmpfs_mount *tmp;
2028 struct tmpfs_node *node;
2029 struct tmpfs_extattr *ea;
2030 struct tmpfs_extattr *new_ea;
2031 size_t attr_size;
2032 size_t namelen;
2033 ssize_t diff;
2034 int error;
2035
2036 node = VP_TO_TMPFS_NODE(vp);
2037 tmp = VFS_TO_TMPFS(vp->v_mount);
2038 attr_size = ap->a_uio->uio_resid;
2039 diff = 0;
2040 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
2041 return (EOPNOTSUPP);
2042 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
2043 ap->a_cred, ap->a_td, VWRITE);
2044 if (error != 0)
2045 return (error);
2046 if (ap->a_name == NULL || ap->a_name[0] == '\0')
2047 return (EINVAL);
2048 namelen = strlen(ap->a_name);
2049 if (namelen > EXTATTR_MAXNAMELEN)
2050 return (EINVAL);
2051
2052 LIST_FOREACH(ea, &node->tn_extattrs, ea_extattrs) {
2053 if (ea->ea_namespace == ap->a_attrnamespace &&
2054 namelen == ea->ea_namelen &&
2055 memcmp(ap->a_name, ea->ea_name, namelen) == 0) {
2056 diff -= sizeof(struct tmpfs_extattr) + ea->ea_namelen +
2057 ea->ea_size;
2058 break;
2059 }
2060 }
2061
2062 diff += sizeof(struct tmpfs_extattr) + namelen + attr_size;
2063 if (!tmpfs_extattr_update_mem(tmp, diff))
2064 return (ENOSPC);
2065 new_ea = malloc(sizeof(struct tmpfs_extattr), M_TMPFSEA, M_WAITOK);
2066 new_ea->ea_namespace = ap->a_attrnamespace;
2067 new_ea->ea_name = malloc(namelen, M_TMPFSEA, M_WAITOK);
2068 new_ea->ea_namelen = namelen;
2069 memcpy(new_ea->ea_name, ap->a_name, namelen);
2070 if (attr_size != 0) {
2071 new_ea->ea_value = malloc(attr_size, M_TMPFSEA, M_WAITOK);
2072 new_ea->ea_size = attr_size;
2073 error = uiomove(new_ea->ea_value, attr_size, ap->a_uio);
2074 } else {
2075 new_ea->ea_value = NULL;
2076 new_ea->ea_size = 0;
2077 }
2078 if (error != 0) {
2079 tmpfs_extattr_update_mem(tmp, -diff);
2080 tmpfs_extattr_free(new_ea);
2081 return (error);
2082 }
2083 if (ea != NULL) {
2084 LIST_REMOVE(ea, ea_extattrs);
2085 tmpfs_extattr_free(ea);
2086 }
2087 LIST_INSERT_HEAD(&node->tn_extattrs, new_ea, ea_extattrs);
2088 return (0);
2089 }
2090
2091 static off_t
tmpfs_seek_data_locked(vm_object_t obj,off_t noff)2092 tmpfs_seek_data_locked(vm_object_t obj, off_t noff)
2093 {
2094 vm_pindex_t p;
2095
2096 p = swap_pager_seek_data(obj, OFF_TO_IDX(noff));
2097 return (p == OFF_TO_IDX(noff) ? noff : IDX_TO_OFF(p));
2098 }
2099
2100 static int
tmpfs_seek_clamp(struct tmpfs_node * tn,off_t * noff,bool seekdata)2101 tmpfs_seek_clamp(struct tmpfs_node *tn, off_t *noff, bool seekdata)
2102 {
2103 if (*noff < tn->tn_size)
2104 return (0);
2105 if (seekdata)
2106 return (ENXIO);
2107 *noff = tn->tn_size;
2108 return (0);
2109 }
2110
2111 static off_t
tmpfs_seek_hole_locked(vm_object_t obj,off_t noff)2112 tmpfs_seek_hole_locked(vm_object_t obj, off_t noff)
2113 {
2114
2115 return (IDX_TO_OFF(swap_pager_seek_hole(obj, OFF_TO_IDX(noff))));
2116 }
2117
2118 static int
tmpfs_seek_datahole(struct vnode * vp,off_t * off,bool seekdata)2119 tmpfs_seek_datahole(struct vnode *vp, off_t *off, bool seekdata)
2120 {
2121 struct tmpfs_node *tn;
2122 vm_object_t obj;
2123 off_t noff;
2124 int error;
2125
2126 if (vp->v_type != VREG)
2127 return (ENOTTY);
2128 tn = VP_TO_TMPFS_NODE(vp);
2129 noff = *off;
2130 if (noff < 0)
2131 return (ENXIO);
2132 error = tmpfs_seek_clamp(tn, &noff, seekdata);
2133 if (error != 0)
2134 return (error);
2135 obj = tn->tn_reg.tn_aobj;
2136
2137 VM_OBJECT_RLOCK(obj);
2138 noff = seekdata ? tmpfs_seek_data_locked(obj, noff) :
2139 tmpfs_seek_hole_locked(obj, noff);
2140 VM_OBJECT_RUNLOCK(obj);
2141
2142 error = tmpfs_seek_clamp(tn, &noff, seekdata);
2143 if (error == 0)
2144 *off = noff;
2145 return (error);
2146 }
2147
2148 static int
tmpfs_ioctl(struct vop_ioctl_args * ap)2149 tmpfs_ioctl(struct vop_ioctl_args *ap)
2150 {
2151 struct vnode *vp = ap->a_vp;
2152 int error = 0;
2153
2154 switch (ap->a_command) {
2155 case FIOSEEKDATA:
2156 case FIOSEEKHOLE:
2157 error = vn_lock(vp, LK_SHARED);
2158 if (error != 0) {
2159 error = EBADF;
2160 break;
2161 }
2162 error = tmpfs_seek_datahole(vp, (off_t *)ap->a_data,
2163 ap->a_command == FIOSEEKDATA);
2164 VOP_UNLOCK(vp);
2165 break;
2166 default:
2167 error = ENOTTY;
2168 break;
2169 }
2170 return (error);
2171 }
2172
2173 /*
2174 * Vnode operations vector used for files stored in a tmpfs file system.
2175 */
2176 struct vop_vector tmpfs_vnodeop_entries = {
2177 .vop_default = &default_vnodeops,
2178 .vop_lookup = vfs_cache_lookup,
2179 .vop_cachedlookup = tmpfs_cached_lookup,
2180 .vop_create = tmpfs_create,
2181 .vop_mknod = tmpfs_mknod,
2182 .vop_open = tmpfs_open,
2183 .vop_close = tmpfs_close,
2184 .vop_fplookup_vexec = tmpfs_fplookup_vexec,
2185 .vop_fplookup_symlink = tmpfs_fplookup_symlink,
2186 .vop_access = tmpfs_access,
2187 .vop_stat = tmpfs_stat,
2188 .vop_getattr = tmpfs_getattr,
2189 .vop_setattr = tmpfs_setattr,
2190 .vop_read = tmpfs_read,
2191 .vop_read_pgcache = tmpfs_read_pgcache,
2192 .vop_write = tmpfs_write,
2193 .vop_deallocate = tmpfs_deallocate,
2194 .vop_fsync = tmpfs_fsync,
2195 .vop_remove = tmpfs_remove,
2196 .vop_link = tmpfs_link,
2197 .vop_rename = tmpfs_rename,
2198 .vop_mkdir = tmpfs_mkdir,
2199 .vop_rmdir = tmpfs_rmdir,
2200 .vop_symlink = tmpfs_symlink,
2201 .vop_readdir = tmpfs_readdir,
2202 .vop_readlink = tmpfs_readlink,
2203 .vop_inactive = tmpfs_inactive,
2204 .vop_need_inactive = tmpfs_need_inactive,
2205 .vop_reclaim = tmpfs_reclaim,
2206 .vop_print = tmpfs_print,
2207 .vop_pathconf = tmpfs_pathconf,
2208 .vop_vptofh = tmpfs_vptofh,
2209 .vop_whiteout = tmpfs_whiteout,
2210 .vop_bmap = VOP_EOPNOTSUPP,
2211 .vop_vptocnp = tmpfs_vptocnp,
2212 .vop_lock1 = vop_lock,
2213 .vop_unlock = vop_unlock,
2214 .vop_islocked = vop_islocked,
2215 .vop_deleteextattr = tmpfs_deleteextattr,
2216 .vop_getextattr = tmpfs_getextattr,
2217 .vop_listextattr = tmpfs_listextattr,
2218 .vop_setextattr = tmpfs_setextattr,
2219 .vop_add_writecount = vop_stdadd_writecount_nomsync,
2220 .vop_ioctl = tmpfs_ioctl,
2221 };
2222 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);
2223
2224 /*
2225 * Same vector for mounts which do not use namecache.
2226 */
2227 struct vop_vector tmpfs_vnodeop_nonc_entries = {
2228 .vop_default = &tmpfs_vnodeop_entries,
2229 .vop_lookup = tmpfs_lookup,
2230 };
2231 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_nonc_entries);
2232