vfs_syscalls.c revision 1.306.2.14 1 /* $NetBSD: vfs_syscalls.c,v 1.306.2.14 2007/08/20 21:27:44 ad Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.306.2.14 2007/08/20 21:27:44 ad Exp $");
41
42 #include "opt_compat_netbsd.h"
43 #include "opt_compat_43.h"
44 #include "opt_fileassoc.h"
45 #include "fss.h"
46 #include "veriexec.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/namei.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/file.h>
54 #include <sys/stat.h>
55 #include <sys/vnode.h>
56 #include <sys/mount.h>
57 #include <sys/proc.h>
58 #include <sys/uio.h>
59 #include <sys/malloc.h>
60 #include <sys/kmem.h>
61 #include <sys/dirent.h>
62 #include <sys/sysctl.h>
63 #include <sys/syscallargs.h>
64 #include <sys/vfs_syscalls.h>
65 #include <sys/ktrace.h>
66 #ifdef FILEASSOC
67 #include <sys/fileassoc.h>
68 #endif /* FILEASSOC */
69 #include <sys/verified_exec.h>
70 #include <sys/kauth.h>
71
72 #include <miscfs/genfs/genfs.h>
73 #include <miscfs/syncfs/syncfs.h>
74
75 #ifdef COMPAT_30
76 #include "opt_nfsserver.h"
77 #include <nfs/rpcv2.h>
78 #endif
79 #include <nfs/nfsproto.h>
80 #ifdef COMPAT_30
81 #include <nfs/nfs.h>
82 #include <nfs/nfs_var.h>
83 #endif
84
85 #if NFSS > 0
86 #include <dev/fssvar.h>
87 #endif
88
89 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
90
91 static int change_dir(struct nameidata *, struct lwp *);
92 static int change_flags(struct vnode *, u_long, struct lwp *);
93 static int change_mode(struct vnode *, int, struct lwp *l);
94 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
95 static int rename_files(const char *, const char *, struct lwp *, int);
96
97 void checkdirs(struct vnode *);
98
99 int dovfsusermount = 0;
100
101 /*
102 * Virtual File System System Calls
103 */
104
105 /*
106 * Mount a file system.
107 */
108
109 #if defined(COMPAT_09) || defined(COMPAT_43)
110 /*
111 * This table is used to maintain compatibility with 4.3BSD
112 * and NetBSD 0.9 mount syscalls. Note, the order is important!
113 *
114 * Do not modify this table. It should only contain filesystems
115 * supported by NetBSD 0.9 and 4.3BSD.
116 */
117 const char * const mountcompatnames[] = {
118 NULL, /* 0 = MOUNT_NONE */
119 MOUNT_FFS, /* 1 = MOUNT_UFS */
120 MOUNT_NFS, /* 2 */
121 MOUNT_MFS, /* 3 */
122 MOUNT_MSDOS, /* 4 */
123 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */
124 MOUNT_FDESC, /* 6 */
125 MOUNT_KERNFS, /* 7 */
126 NULL, /* 8 = MOUNT_DEVFS */
127 MOUNT_AFS, /* 9 */
128 };
129 const int nmountcompatnames = sizeof(mountcompatnames) /
130 sizeof(mountcompatnames[0]);
131 #endif /* COMPAT_09 || COMPAT_43 */
132
133 static int
134 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
135 void *data, size_t *data_len)
136 {
137 struct mount *mp;
138 int error = 0, saved_flags;
139
140 mp = vp->v_mount;
141 saved_flags = mp->mnt_flag;
142
143 /* We can operate only on VV_ROOT nodes. */
144 if ((vp->v_vflag & VV_ROOT) == 0) {
145 error = EINVAL;
146 goto out;
147 }
148
149 /*
150 * We only allow the filesystem to be reloaded if it
151 * is currently mounted read-only.
152 */
153 if (flags & MNT_RELOAD && !(mp->mnt_flag & MNT_RDONLY)) {
154 error = EOPNOTSUPP; /* Needs translation */
155 goto out;
156 }
157
158 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
159 KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
160 if (error)
161 goto out;
162
163 if (vfs_busy(mp, LK_NOWAIT, 0)) {
164 error = EPERM;
165 goto out;
166 }
167
168 mp->mnt_flag &= ~MNT_OP_FLAGS;
169 mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
170
171 /*
172 * Set the mount level flags.
173 */
174 if (flags & MNT_RDONLY)
175 mp->mnt_flag |= MNT_RDONLY;
176 else if (mp->mnt_flag & MNT_RDONLY)
177 mp->mnt_iflag |= IMNT_WANTRDWR;
178 mp->mnt_flag &=
179 ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
180 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
181 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP);
182 mp->mnt_flag |= flags &
183 (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
184 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
185 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
186 MNT_IGNORE);
187
188 error = VFS_MOUNT(mp, path, data, data_len, l);
189
190 #if defined(COMPAT_30) && defined(NFSSERVER)
191 if (error && data != NULL) {
192 int error2;
193
194 /* Update failed; let's try and see if it was an
195 * export request. */
196 error2 = nfs_update_exports_30(mp, path, data, l);
197
198 /* Only update error code if the export request was
199 * understood but some problem occurred while
200 * processing it. */
201 if (error2 != EJUSTRETURN)
202 error = error2;
203 }
204 #endif
205 if (mp->mnt_iflag & IMNT_WANTRDWR)
206 mp->mnt_flag &= ~MNT_RDONLY;
207 if (error)
208 mp->mnt_flag = saved_flags;
209 mp->mnt_flag &= ~MNT_OP_FLAGS;
210 mp->mnt_iflag &= ~IMNT_WANTRDWR;
211 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
212 if (mp->mnt_syncer == NULL)
213 error = vfs_allocate_syncvnode(mp);
214 } else {
215 if (mp->mnt_syncer != NULL)
216 vfs_deallocate_syncvnode(mp);
217 }
218 vfs_unbusy(mp);
219
220 out:
221 return (error);
222 }
223
224 static int
225 mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
226 {
227 char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
228 int error;
229
230 /* Copy file-system type from userspace. */
231 error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
232 if (error) {
233 #if defined(COMPAT_09) || defined(COMPAT_43)
234 /*
235 * Historically, filesystem types were identified by numbers.
236 * If we get an integer for the filesystem type instead of a
237 * string, we check to see if it matches one of the historic
238 * filesystem types.
239 */
240 u_long fsindex = (u_long)fstype;
241 if (fsindex >= nmountcompatnames ||
242 mountcompatnames[fsindex] == NULL)
243 return ENODEV;
244 strlcpy(fstypename, mountcompatnames[fsindex], sizeof(fstypename));
245 #else
246 return error;
247 #endif
248 }
249
250 #ifdef COMPAT_10
251 /* Accept `ufs' as an alias for `ffs'. */
252 if (strcmp(fstypename, "ufs") == 0)
253 fstypename[0] = 'f';
254 #endif
255
256 if ((*vfsops = vfs_getopsbyname(fstypename)) == NULL)
257 return ENODEV;
258 return 0;
259 }
260
261 static int
262 mount_domount(struct lwp *l, struct vnode **vpp, struct vfsops *vfsops,
263 const char *path, int flags, void *data, size_t *data_len)
264 {
265 struct mount *mp = NULL;
266 struct vnode *vp = *vpp;
267 struct vattr va;
268 int error;
269
270 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
271 KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
272 if (error)
273 return error;
274
275 /* Can't make a non-dir a mount-point (from here anyway). */
276 if (vp->v_type != VDIR)
277 return ENOTDIR;
278
279 /*
280 * If the user is not root, ensure that they own the directory
281 * onto which we are attempting to mount.
282 */
283 if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)) != 0 ||
284 (va.va_uid != kauth_cred_geteuid(l->l_cred) &&
285 (error = kauth_authorize_generic(l->l_cred,
286 KAUTH_GENERIC_ISSUSER, NULL)) != 0)) {
287 return error;
288 }
289
290 if (flags & MNT_EXPORTED)
291 return EINVAL;
292
293 if ((error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
294 return error;
295
296 /*
297 * Check if a file-system is not already mounted on this vnode.
298 */
299 if (vp->v_mountedhere != NULL)
300 return EBUSY;
301
302 mp = malloc(sizeof(*mp), M_MOUNT, M_WAITOK|M_ZERO);
303
304 mp->mnt_op = vfsops;
305
306 TAILQ_INIT(&mp->mnt_vnodelist);
307 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
308 mutex_init(&mp->mnt_mutex, MUTEX_DEFAULT, IPL_NONE);
309 (void)vfs_busy(mp, LK_NOWAIT, 0);
310
311 mp->mnt_vnodecovered = vp;
312 mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
313 mp->mnt_unmounter = NULL;
314 mount_initspecific(mp);
315
316 /*
317 * The underlying file system may refuse the mount for
318 * various reasons. Allow the user to force it to happen.
319 *
320 * Set the mount level flags.
321 */
322 mp->mnt_flag = flags &
323 (MNT_FORCE | MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
324 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
325 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
326 MNT_IGNORE | MNT_RDONLY);
327
328 error = VFS_MOUNT(mp, path, data, data_len, l);
329 mp->mnt_flag &= ~MNT_OP_FLAGS;
330
331 /*
332 * Put the new filesystem on the mount list after root.
333 */
334 cache_purge(vp);
335 if (error != 0) {
336 vp->v_mountedhere = NULL;
337 mp->mnt_op->vfs_refcount--;
338 vfs_unbusy(mp);
339 vfs_destroy(mp);
340 return error;
341 }
342
343 mp->mnt_iflag &= ~IMNT_WANTRDWR;
344 vp->v_mountedhere = mp;
345 mutex_enter(&mountlist_lock);
346 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
347 mutex_exit(&mountlist_lock);
348 VOP_UNLOCK(vp, 0);
349 checkdirs(vp);
350 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
351 error = vfs_allocate_syncvnode(mp);
352 vfs_unbusy(mp);
353 (void) VFS_STATVFS(mp, &mp->mnt_stat, l);
354 error = VFS_START(mp, 0, l);
355 if (error)
356 vrele(vp);
357 *vpp = NULL;
358 return error;
359 }
360
361 static int
362 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
363 void *data, size_t *data_len)
364 {
365 struct mount *mp;
366 int error;
367
368 /* If MNT_GETARGS is specified, it should be the only flag. */
369 if (flags & ~MNT_GETARGS)
370 return EINVAL;
371
372 mp = vp->v_mount;
373
374 /* XXX: probably some notion of "can see" here if we want isolation. */
375 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
376 KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
377 if (error)
378 return error;
379
380 if ((vp->v_vflag & VV_ROOT) == 0)
381 return EINVAL;
382
383 if (vfs_busy(mp, LK_NOWAIT, 0))
384 return EPERM;
385
386 mp->mnt_flag &= ~MNT_OP_FLAGS;
387 mp->mnt_flag |= MNT_GETARGS;
388 error = VFS_MOUNT(mp, path, data, data_len, l);
389 mp->mnt_flag &= ~MNT_OP_FLAGS;
390
391 vfs_unbusy(mp);
392 return (error);
393 }
394
395 #ifdef COMPAT_40
396 /* ARGSUSED */
397 int
398 compat_40_sys_mount(struct lwp *l, void *v, register_t *retval)
399 {
400 struct compat_40_sys_mount_args /* {
401 syscallarg(const char *) type;
402 syscallarg(const char *) path;
403 syscallarg(int) flags;
404 syscallarg(void *) data;
405 } */ *uap = v;
406 register_t dummy;
407
408 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
409 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE, 0, &dummy);
410 }
411 #endif
412
413 int
414 sys___mount50(struct lwp *l, void *v, register_t *retval)
415 {
416 struct sys___mount50_args /* {
417 syscallarg(const char *) type;
418 syscallarg(const char *) path;
419 syscallarg(int) flags;
420 syscallarg(void *) data;
421 syscallarg(size_t) data_len;
422 } */ *uap = v;
423
424 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
425 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
426 SCARG(uap, data_len), retval);
427 }
428
429 int
430 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
431 const char *path, int flags, void *data, enum uio_seg data_seg,
432 size_t data_len, register_t *retval)
433 {
434 struct vnode *vp;
435 struct nameidata nd;
436 void *data_buf = data;
437 int error;
438
439 /*
440 * Get vnode to be covered
441 */
442 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path, l);
443 if ((error = namei(&nd)) != 0)
444 return (error);
445 vp = nd.ni_vp;
446
447 /*
448 * A lookup in VFS_MOUNT might result in an attempt to
449 * lock this vnode again, so make the lock recursive.
450 */
451 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
452
453 if (vfsops == NULL) {
454 if (flags & (MNT_GETARGS | MNT_UPDATE))
455 vfsops = vp->v_mount->mnt_op;
456 else {
457 /* 'type' is userspace */
458 error = mount_get_vfsops(type, &vfsops);
459 if (error != 0)
460 goto done;
461 }
462 }
463
464 if (data != NULL && data_seg == UIO_USERSPACE) {
465 if (data_len == 0) {
466 /* No length supplied, use default for filesystem */
467 data_len = vfsops->vfs_min_mount_data;
468 if (data_len > VFS_MAX_MOUNT_DATA) {
469 /* maybe a force loaded old LKM */
470 error = EINVAL;
471 goto done;
472 }
473 #ifdef COMPAT_30
474 /* Hopefully a longer buffer won't make copyin() fail */
475 if (flags & MNT_UPDATE
476 && data_len < sizeof (struct mnt_export_args30))
477 data_len = sizeof (struct mnt_export_args30);
478 #endif
479 }
480 data_buf = malloc(data_len, M_TEMP, M_WAITOK);
481
482 /* NFS needs the buffer even for mnt_getargs .... */
483 error = copyin(data, data_buf, data_len);
484 if (error != 0)
485 goto done;
486 }
487
488 if (flags & MNT_GETARGS) {
489 if (data_len == 0) {
490 error = EINVAL;
491 goto done;
492 }
493 error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
494 if (error != 0)
495 goto done;
496 if (data_seg == UIO_USERSPACE)
497 error = copyout(data_buf, data, data_len);
498 *retval = data_len;
499 } else if (flags & MNT_UPDATE) {
500 error = mount_update(l, vp, path, flags, data_buf, &data_len);
501 } else {
502 /* Locking is handled internally in mount_domount(). */
503 error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
504 &data_len);
505 }
506
507 done:
508 if (vp)
509 vput(vp);
510 if (data_buf != data)
511 free(data_buf, M_TEMP);
512 return (error);
513 }
514
515 /*
516 * Scan all active processes to see if any of them have a current
517 * or root directory onto which the new filesystem has just been
518 * mounted. If so, replace them with the new mount point.
519 */
520 void
521 checkdirs(struct vnode *olddp)
522 {
523 struct cwdinfo *cwdi;
524 struct vnode *newdp;
525 struct proc *p;
526
527 if (olddp->v_usecount == 1)
528 return;
529 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
530 panic("mount: lost mount");
531 mutex_enter(&proclist_lock);
532 PROCLIST_FOREACH(p, &allproc) {
533 cwdi = p->p_cwdi;
534 if (!cwdi)
535 continue;
536 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
537 if (cwdi->cwdi_cdir == olddp) {
538 vrele(cwdi->cwdi_cdir);
539 VREF(newdp);
540 cwdi->cwdi_cdir = newdp;
541 }
542 if (cwdi->cwdi_rdir == olddp) {
543 vrele(cwdi->cwdi_rdir);
544 VREF(newdp);
545 cwdi->cwdi_rdir = newdp;
546 }
547 rw_exit(&cwdi->cwdi_lock);
548 }
549 mutex_exit(&proclist_lock);
550 if (rootvnode == olddp) {
551 vrele(rootvnode);
552 VREF(newdp);
553 rootvnode = newdp;
554 }
555 vput(newdp);
556 }
557
558 /*
559 * Unmount a file system.
560 *
561 * Note: unmount takes a path to the vnode mounted on as argument,
562 * not special file (as before).
563 */
564 /* ARGSUSED */
565 int
566 sys_unmount(struct lwp *l, void *v, register_t *retval)
567 {
568 struct sys_unmount_args /* {
569 syscallarg(const char *) path;
570 syscallarg(int) flags;
571 } */ *uap = v;
572 struct vnode *vp;
573 struct mount *mp;
574 int error;
575 struct nameidata nd;
576
577 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
578 SCARG(uap, path), l);
579 if ((error = namei(&nd)) != 0)
580 return (error);
581 vp = nd.ni_vp;
582 mp = vp->v_mount;
583
584 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
585 KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
586 if (error) {
587 vput(vp);
588 return (error);
589 }
590
591 /*
592 * Don't allow unmounting the root file system.
593 */
594 if (mp->mnt_flag & MNT_ROOTFS) {
595 vput(vp);
596 return (EINVAL);
597 }
598
599 /*
600 * Must be the root of the filesystem
601 */
602 if ((vp->v_vflag & VV_ROOT) == 0) {
603 vput(vp);
604 return (EINVAL);
605 }
606 vput(vp);
607
608 /*
609 * XXX Freeze syncer. Must do this before locking the
610 * mount point. See dounmount() for details.
611 */
612 mutex_enter(&syncer_mutex);
613
614 if (vfs_busy(mp, 0, 0)) {
615 mutex_exit(&syncer_mutex);
616 return (EBUSY);
617 }
618
619 return (dounmount(mp, SCARG(uap, flags), l));
620 }
621
622 /*
623 * Do the actual file system unmount. File system is assumed to have been
624 * marked busy by the caller.
625 */
626 int
627 dounmount(struct mount *mp, int flags, struct lwp *l)
628 {
629 struct vnode *coveredvp;
630 int error;
631 int async;
632 int used_syncer;
633
634 #if NVERIEXEC > 0
635 error = veriexec_unmountchk(mp);
636 if (error)
637 return (error);
638 #endif /* NVERIEXEC > 0 */
639
640 mutex_enter(&mountlist_lock);
641 vfs_unbusy(mp);
642 used_syncer = (mp->mnt_syncer != NULL);
643
644 /*
645 * XXX Syncer must be frozen when we get here. This should really
646 * be done on a per-mountpoint basis, but especially the softdep
647 * code possibly called from the syncer doesn't exactly work on a
648 * per-mountpoint basis, so the softdep code would become a maze
649 * of vfs_busy() calls.
650 *
651 * The caller of dounmount() must acquire syncer_mutex because
652 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
653 * order, and we must preserve that order to avoid deadlock.
654 *
655 * So, if the file system did not use the syncer, now is
656 * the time to release the syncer_mutex.
657 */
658 if (used_syncer == 0)
659 mutex_exit(&syncer_mutex);
660
661 mp->mnt_iflag |= IMNT_UNMOUNT;
662 mp->mnt_unmounter = l;
663 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_lock);
664
665 async = mp->mnt_flag & MNT_ASYNC;
666 mp->mnt_flag &= ~MNT_ASYNC;
667 cache_purgevfs(mp); /* remove cache entries for this file sys */
668 if (mp->mnt_syncer != NULL)
669 vfs_deallocate_syncvnode(mp);
670 error = 0;
671 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
672 #if NFSS > 0
673 error = fss_umount_hook(mp, (flags & MNT_FORCE));
674 #endif
675 if (error == 0)
676 error = VFS_SYNC(mp, MNT_WAIT, l->l_cred, l);
677 }
678 if (error == 0 || (flags & MNT_FORCE))
679 error = VFS_UNMOUNT(mp, flags, l);
680 if (error) {
681 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
682 (void) vfs_allocate_syncvnode(mp);
683 mutex_enter(&mountlist_lock);
684 mp->mnt_iflag &= ~IMNT_UNMOUNT;
685 mp->mnt_unmounter = NULL;
686 mp->mnt_flag |= async;
687 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
688 &mountlist_lock);
689 if (used_syncer)
690 mutex_exit(&syncer_mutex);
691 mutex_enter(&mp->mnt_mutex);
692 while (mp->mnt_wcnt > 0) {
693 wakeup(mp);
694 mtsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1",
695 0, &mp->mnt_mutex);
696 }
697 mutex_exit(&mp->mnt_mutex);
698 return (error);
699 }
700 mutex_enter(&mountlist_lock);
701 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
702 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP)
703 coveredvp->v_mountedhere = NULL;
704 if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
705 panic("unmount: dangling vnode");
706 mp->mnt_iflag |= IMNT_GONE;
707 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_lock);
708 if (coveredvp != NULLVP)
709 vrele(coveredvp);
710 if (used_syncer)
711 mutex_exit(&syncer_mutex);
712 mutex_enter(&mp->mnt_mutex);
713 while (mp->mnt_wcnt > 0) {
714 wakeup(mp);
715 mtsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0, &mp->mnt_mutex);
716 }
717 mutex_exit(&mp->mnt_mutex);
718 vfs_hooks_unmount(mp);
719 vfs_delref(mp->mnt_op);
720 vfs_destroy(mp);
721 return (0);
722 }
723
724 /*
725 * Sync each mounted filesystem.
726 */
727 #ifdef DEBUG
728 int syncprt = 0;
729 struct ctldebug debug0 = { "syncprt", &syncprt };
730 #endif
731
732 /* ARGSUSED */
733 int
734 sys_sync(struct lwp *l, void *v, register_t *retval)
735 {
736 struct mount *mp, *nmp;
737 int asyncflag;
738
739 if (l == NULL)
740 l = &lwp0;
741
742 mutex_enter(&mountlist_lock);
743 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
744 if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
745 nmp = mp->mnt_list.cqe_prev;
746 continue;
747 }
748 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
749 asyncflag = mp->mnt_flag & MNT_ASYNC;
750 mp->mnt_flag &= ~MNT_ASYNC;
751 VFS_SYNC(mp, MNT_NOWAIT, l->l_cred, l);
752 if (asyncflag)
753 mp->mnt_flag |= MNT_ASYNC;
754 }
755 mutex_enter(&mountlist_lock);
756 nmp = mp->mnt_list.cqe_prev;
757 vfs_unbusy(mp);
758
759 }
760 mutex_exit(&mountlist_lock);
761 #ifdef DEBUG
762 if (syncprt)
763 vfs_bufstats();
764 #endif /* DEBUG */
765 return (0);
766 }
767
768 /*
769 * Change filesystem quotas.
770 */
771 /* ARGSUSED */
772 int
773 sys_quotactl(struct lwp *l, void *v, register_t *retval)
774 {
775 struct sys_quotactl_args /* {
776 syscallarg(const char *) path;
777 syscallarg(int) cmd;
778 syscallarg(int) uid;
779 syscallarg(void *) arg;
780 } */ *uap = v;
781 struct mount *mp;
782 int error;
783 struct nameidata nd;
784
785 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
786 if ((error = namei(&nd)) != 0)
787 return (error);
788 mp = nd.ni_vp->v_mount;
789 vrele(nd.ni_vp);
790 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
791 SCARG(uap, arg), l);
792 return (error);
793 }
794
795 int
796 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
797 int root)
798 {
799 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
800 int error = 0;
801
802 /*
803 * If MNT_NOWAIT or MNT_LAZY is specified, do not
804 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
805 * overrides MNT_NOWAIT.
806 */
807 if (flags == MNT_NOWAIT || flags == MNT_LAZY ||
808 (flags != MNT_WAIT && flags != 0)) {
809 memcpy(sp, &mp->mnt_stat, sizeof(*sp));
810 goto done;
811 }
812
813 /* Get the filesystem stats now */
814 memset(sp, 0, sizeof(*sp));
815 if ((error = VFS_STATVFS(mp, sp, l)) != 0) {
816 return error;
817 }
818
819 if (cwdi->cwdi_rdir == NULL)
820 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
821 done:
822 if (cwdi->cwdi_rdir != NULL) {
823 size_t len;
824 char *bp;
825 char *path = PNBUF_GET();
826
827 bp = path + MAXPATHLEN;
828 *--bp = '\0';
829 rw_enter(&cwdi->cwdi_lock, RW_READER);
830 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
831 MAXPATHLEN / 2, 0, l);
832 rw_exit(&cwdi->cwdi_lock);
833 if (error) {
834 PNBUF_PUT(path);
835 return error;
836 }
837 len = strlen(bp);
838 /*
839 * for mount points that are below our root, we can see
840 * them, so we fix up the pathname and return them. The
841 * rest we cannot see, so we don't allow viewing the
842 * data.
843 */
844 if (strncmp(bp, sp->f_mntonname, len) == 0) {
845 strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
846 sizeof(sp->f_mntonname));
847 if (sp->f_mntonname[0] == '\0')
848 (void)strlcpy(sp->f_mntonname, "/",
849 sizeof(sp->f_mntonname));
850 } else {
851 if (root)
852 (void)strlcpy(sp->f_mntonname, "/",
853 sizeof(sp->f_mntonname));
854 else
855 error = EPERM;
856 }
857 PNBUF_PUT(path);
858 }
859 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
860 return error;
861 }
862
863 /*
864 * Get filesystem statistics by path.
865 */
866 int
867 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
868 {
869 struct mount *mp;
870 int error;
871 struct nameidata nd;
872
873 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path, l);
874 if ((error = namei(&nd)) != 0)
875 return error;
876 mp = nd.ni_vp->v_mount;
877 error = dostatvfs(mp, sb, l, flags, 1);
878 vrele(nd.ni_vp);
879 return error;
880 }
881
882 /* ARGSUSED */
883 int
884 sys_statvfs1(struct lwp *l, void *v, register_t *retval)
885 {
886 struct sys_statvfs1_args /* {
887 syscallarg(const char *) path;
888 syscallarg(struct statvfs *) buf;
889 syscallarg(int) flags;
890 } */ *uap = v;
891 struct statvfs *sb;
892 int error;
893
894 sb = STATVFSBUF_GET();
895 error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
896 if (error == 0)
897 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
898 STATVFSBUF_PUT(sb);
899 return error;
900 }
901
902 /*
903 * Get filesystem statistics by fd.
904 */
905 int
906 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
907 {
908 struct proc *p = l->l_proc;
909 struct file *fp;
910 struct mount *mp;
911 int error;
912
913 /* getvnode() will use the descriptor for us */
914 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
915 return (error);
916 mp = ((struct vnode *)fp->f_data)->v_mount;
917 error = dostatvfs(mp, sb, l, flags, 1);
918 FILE_UNUSE(fp, l);
919 return error;
920 }
921
922 /* ARGSUSED */
923 int
924 sys_fstatvfs1(struct lwp *l, void *v, register_t *retval)
925 {
926 struct sys_fstatvfs1_args /* {
927 syscallarg(int) fd;
928 syscallarg(struct statvfs *) buf;
929 syscallarg(int) flags;
930 } */ *uap = v;
931 struct statvfs *sb;
932 int error;
933
934 sb = STATVFSBUF_GET();
935 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
936 if (error == 0)
937 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
938 STATVFSBUF_PUT(sb);
939 return error;
940 }
941
942
943 /*
944 * Get statistics on all filesystems.
945 */
946 int
947 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
948 int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
949 register_t *retval)
950 {
951 int root = 0;
952 struct proc *p = l->l_proc;
953 struct mount *mp, *nmp;
954 struct statvfs *sb;
955 size_t count, maxcount;
956 int error = 0;
957
958 sb = STATVFSBUF_GET();
959 maxcount = bufsize / entry_sz;
960 mutex_enter(&mountlist_lock);
961 count = 0;
962 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
963 mp = nmp) {
964 if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
965 nmp = CIRCLEQ_NEXT(mp, mnt_list);
966 continue;
967 }
968 if (sfsp && count < maxcount) {
969 error = dostatvfs(mp, sb, l, flags, 0);
970 if (error) {
971 mutex_enter(&mountlist_lock);
972 nmp = CIRCLEQ_NEXT(mp, mnt_list);
973 vfs_unbusy(mp);
974 continue;
975 }
976 error = copyfn(sb, sfsp, entry_sz);
977 if (error) {
978 vfs_unbusy(mp);
979 goto out;
980 }
981 sfsp = (char *)sfsp + entry_sz;
982 root |= strcmp(sb->f_mntonname, "/") == 0;
983 }
984 count++;
985 mutex_enter(&mountlist_lock);
986 nmp = CIRCLEQ_NEXT(mp, mnt_list);
987 vfs_unbusy(mp);
988 }
989 mutex_exit(&mountlist_lock);
990 if (root == 0 && p->p_cwdi->cwdi_rdir) {
991 /*
992 * fake a root entry
993 */
994 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount, sb, l, flags, 1);
995 if (error != 0)
996 goto out;
997 if (sfsp)
998 error = copyfn(sb, sfsp, entry_sz);
999 count++;
1000 }
1001 if (sfsp && count > maxcount)
1002 *retval = maxcount;
1003 else
1004 *retval = count;
1005 out:
1006 STATVFSBUF_PUT(sb);
1007 return error;
1008 }
1009
1010 int
1011 sys_getvfsstat(struct lwp *l, void *v, register_t *retval)
1012 {
1013 struct sys_getvfsstat_args /* {
1014 syscallarg(struct statvfs *) buf;
1015 syscallarg(size_t) bufsize;
1016 syscallarg(int) flags;
1017 } */ *uap = v;
1018
1019 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
1020 SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
1021 }
1022
1023 /*
1024 * Change current working directory to a given file descriptor.
1025 */
1026 /* ARGSUSED */
1027 int
1028 sys_fchdir(struct lwp *l, void *v, register_t *retval)
1029 {
1030 struct sys_fchdir_args /* {
1031 syscallarg(int) fd;
1032 } */ *uap = v;
1033 struct proc *p = l->l_proc;
1034 struct filedesc *fdp = p->p_fd;
1035 struct cwdinfo *cwdi;
1036 struct vnode *vp, *tdp;
1037 struct mount *mp;
1038 struct file *fp;
1039 int error;
1040
1041 /* getvnode() will use the descriptor for us */
1042 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
1043 return (error);
1044 vp = (struct vnode *)fp->f_data;
1045
1046 VREF(vp);
1047 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1048 if (vp->v_type != VDIR)
1049 error = ENOTDIR;
1050 else
1051 error = VOP_ACCESS(vp, VEXEC, l->l_cred, l);
1052 if (error) {
1053 vput(vp);
1054 goto out;
1055 }
1056 while ((mp = vp->v_mountedhere) != NULL) {
1057 if (vfs_busy(mp, 0, 0))
1058 continue;
1059
1060 vput(vp);
1061 error = VFS_ROOT(mp, &tdp);
1062 vfs_unbusy(mp);
1063 if (error)
1064 goto out;
1065 vp = tdp;
1066 }
1067 VOP_UNLOCK(vp, 0);
1068
1069 /*
1070 * Disallow changing to a directory not under the process's
1071 * current root directory (if there is one).
1072 */
1073 cwdi = p->p_cwdi;
1074 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1075 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
1076 vrele(vp);
1077 error = EPERM; /* operation not permitted */
1078 } else {
1079 vrele(cwdi->cwdi_cdir);
1080 cwdi->cwdi_cdir = vp;
1081 }
1082 rw_exit(&cwdi->cwdi_lock);
1083
1084 out:
1085 FILE_UNUSE(fp, l);
1086 return (error);
1087 }
1088
1089 /*
1090 * Change this process's notion of the root directory to a given file
1091 * descriptor.
1092 */
1093 int
1094 sys_fchroot(struct lwp *l, void *v, register_t *retval)
1095 {
1096 struct sys_fchroot_args *uap = v;
1097 struct proc *p = l->l_proc;
1098 struct filedesc *fdp = p->p_fd;
1099 struct cwdinfo *cwdi;
1100 struct vnode *vp;
1101 struct file *fp;
1102 int error;
1103
1104 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1105 KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
1106 return error;
1107 /* getvnode() will use the descriptor for us */
1108 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
1109 return error;
1110 vp = (struct vnode *) fp->f_data;
1111 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1112 if (vp->v_type != VDIR)
1113 error = ENOTDIR;
1114 else
1115 error = VOP_ACCESS(vp, VEXEC, l->l_cred, l);
1116 VOP_UNLOCK(vp, 0);
1117 if (error)
1118 goto out;
1119 VREF(vp);
1120
1121 /*
1122 * Prevent escaping from chroot by putting the root under
1123 * the working directory. Silently chdir to / if we aren't
1124 * already there.
1125 */
1126 cwdi = p->p_cwdi;
1127 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1128 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1129 /*
1130 * XXX would be more failsafe to change directory to a
1131 * deadfs node here instead
1132 */
1133 vrele(cwdi->cwdi_cdir);
1134 VREF(vp);
1135 cwdi->cwdi_cdir = vp;
1136 }
1137
1138 if (cwdi->cwdi_rdir != NULL)
1139 vrele(cwdi->cwdi_rdir);
1140 cwdi->cwdi_rdir = vp;
1141 rw_exit(&cwdi->cwdi_lock);
1142
1143 out:
1144 FILE_UNUSE(fp, l);
1145 return (error);
1146 }
1147
1148 /*
1149 * Change current working directory (``.'').
1150 */
1151 /* ARGSUSED */
1152 int
1153 sys_chdir(struct lwp *l, void *v, register_t *retval)
1154 {
1155 struct sys_chdir_args /* {
1156 syscallarg(const char *) path;
1157 } */ *uap = v;
1158 struct proc *p = l->l_proc;
1159 struct cwdinfo *cwdi;
1160 int error;
1161 struct nameidata nd;
1162
1163 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1164 SCARG(uap, path), l);
1165 if ((error = change_dir(&nd, l)) != 0)
1166 return (error);
1167 cwdi = p->p_cwdi;
1168 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1169 vrele(cwdi->cwdi_cdir);
1170 cwdi->cwdi_cdir = nd.ni_vp;
1171 rw_exit(&cwdi->cwdi_lock);
1172 return (0);
1173 }
1174
1175 /*
1176 * Change notion of root (``/'') directory.
1177 */
1178 /* ARGSUSED */
1179 int
1180 sys_chroot(struct lwp *l, void *v, register_t *retval)
1181 {
1182 struct sys_chroot_args /* {
1183 syscallarg(const char *) path;
1184 } */ *uap = v;
1185 struct proc *p = l->l_proc;
1186 struct cwdinfo *cwdi;
1187 struct vnode *vp;
1188 int error;
1189 struct nameidata nd;
1190
1191 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1192 KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
1193 return (error);
1194 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1195 SCARG(uap, path), l);
1196 if ((error = change_dir(&nd, l)) != 0)
1197 return (error);
1198
1199 cwdi = p->p_cwdi;
1200 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1201 if (cwdi->cwdi_rdir != NULL)
1202 vrele(cwdi->cwdi_rdir);
1203 vp = nd.ni_vp;
1204 cwdi->cwdi_rdir = vp;
1205
1206 /*
1207 * Prevent escaping from chroot by putting the root under
1208 * the working directory. Silently chdir to / if we aren't
1209 * already there.
1210 */
1211 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1212 /*
1213 * XXX would be more failsafe to change directory to a
1214 * deadfs node here instead
1215 */
1216 vrele(cwdi->cwdi_cdir);
1217 VREF(vp);
1218 cwdi->cwdi_cdir = vp;
1219 }
1220 rw_exit(&cwdi->cwdi_lock);
1221
1222 return (0);
1223 }
1224
1225 /*
1226 * Common routine for chroot and chdir.
1227 */
1228 static int
1229 change_dir(struct nameidata *ndp, struct lwp *l)
1230 {
1231 struct vnode *vp;
1232 int error;
1233
1234 if ((error = namei(ndp)) != 0)
1235 return (error);
1236 vp = ndp->ni_vp;
1237 if (vp->v_type != VDIR)
1238 error = ENOTDIR;
1239 else
1240 error = VOP_ACCESS(vp, VEXEC, l->l_cred, l);
1241
1242 if (error)
1243 vput(vp);
1244 else
1245 VOP_UNLOCK(vp, 0);
1246 return (error);
1247 }
1248
1249 /*
1250 * Check permissions, allocate an open file structure,
1251 * and call the device open routine if any.
1252 */
1253 int
1254 sys_open(struct lwp *l, void *v, register_t *retval)
1255 {
1256 struct sys_open_args /* {
1257 syscallarg(const char *) path;
1258 syscallarg(int) flags;
1259 syscallarg(int) mode;
1260 } */ *uap = v;
1261 struct proc *p = l->l_proc;
1262 struct cwdinfo *cwdi = p->p_cwdi;
1263 struct filedesc *fdp = p->p_fd;
1264 struct file *fp;
1265 struct vnode *vp;
1266 int flags, cmode;
1267 int type, indx, error;
1268 struct flock lf;
1269 struct nameidata nd;
1270
1271 flags = FFLAGS(SCARG(uap, flags));
1272 if ((flags & (FREAD | FWRITE)) == 0)
1273 return (EINVAL);
1274 /* falloc() will use the file descriptor for us */
1275 if ((error = falloc(l, &fp, &indx)) != 0)
1276 return (error);
1277 /* We're going to read cwdi->cwdi_cmask unlocked here. */
1278 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1279 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1280 l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1281 if ((error = vn_open(&nd, flags, cmode)) != 0) {
1282 rw_enter(&fdp->fd_lock, RW_WRITER);
1283 FILE_UNUSE(fp, l);
1284 fdp->fd_ofiles[indx] = NULL;
1285 rw_exit(&fdp->fd_lock);
1286 ffree(fp);
1287 if ((error == EDUPFD || error == EMOVEFD) &&
1288 l->l_dupfd >= 0 && /* XXX from fdopen */
1289 (error =
1290 dupfdopen(l, indx, l->l_dupfd, flags, error)) == 0) {
1291 *retval = indx;
1292 return (0);
1293 }
1294 if (error == ERESTART)
1295 error = EINTR;
1296 fdremove(fdp, indx);
1297 return (error);
1298 }
1299 l->l_dupfd = 0;
1300 vp = nd.ni_vp;
1301 fp->f_flag = flags & FMASK;
1302 fp->f_type = DTYPE_VNODE;
1303 fp->f_ops = &vnops;
1304 fp->f_data = vp;
1305 if (flags & (O_EXLOCK | O_SHLOCK)) {
1306 lf.l_whence = SEEK_SET;
1307 lf.l_start = 0;
1308 lf.l_len = 0;
1309 if (flags & O_EXLOCK)
1310 lf.l_type = F_WRLCK;
1311 else
1312 lf.l_type = F_RDLCK;
1313 type = F_FLOCK;
1314 if ((flags & FNONBLOCK) == 0)
1315 type |= F_WAIT;
1316 VOP_UNLOCK(vp, 0);
1317 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1318 if (error) {
1319 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1320 FILE_UNUSE(fp, l);
1321 ffree(fp);
1322 fdremove(fdp, indx);
1323 return (error);
1324 }
1325 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1326 fp->f_flag |= FHASLOCK;
1327 }
1328 VOP_UNLOCK(vp, 0);
1329 *retval = indx;
1330 FILE_SET_MATURE(fp);
1331 FILE_UNUSE(fp, l);
1332 return (0);
1333 }
1334
1335 static void
1336 vfs__fhfree(fhandle_t *fhp)
1337 {
1338 size_t fhsize;
1339
1340 if (fhp == NULL) {
1341 return;
1342 }
1343 fhsize = FHANDLE_SIZE(fhp);
1344 kmem_free(fhp, fhsize);
1345 }
1346
1347 /*
1348 * vfs_composefh: compose a filehandle.
1349 */
1350
1351 int
1352 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
1353 {
1354 struct mount *mp;
1355 struct fid *fidp;
1356 int error;
1357 size_t needfhsize;
1358 size_t fidsize;
1359
1360 mp = vp->v_mount;
1361 fidp = NULL;
1362 if (*fh_size < FHANDLE_SIZE_MIN) {
1363 fidsize = 0;
1364 } else {
1365 fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1366 if (fhp != NULL) {
1367 memset(fhp, 0, *fh_size);
1368 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1369 fidp = &fhp->fh_fid;
1370 }
1371 }
1372 error = VFS_VPTOFH(vp, fidp, &fidsize);
1373 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1374 if (error == 0 && *fh_size < needfhsize) {
1375 error = E2BIG;
1376 }
1377 *fh_size = needfhsize;
1378 return error;
1379 }
1380
1381 int
1382 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1383 {
1384 struct mount *mp;
1385 fhandle_t *fhp;
1386 size_t fhsize;
1387 size_t fidsize;
1388 int error;
1389
1390 *fhpp = NULL;
1391 mp = vp->v_mount;
1392 fidsize = 0;
1393 error = VFS_VPTOFH(vp, NULL, &fidsize);
1394 KASSERT(error != 0);
1395 if (error != E2BIG) {
1396 goto out;
1397 }
1398 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1399 fhp = kmem_zalloc(fhsize, KM_SLEEP);
1400 if (fhp == NULL) {
1401 error = ENOMEM;
1402 goto out;
1403 }
1404 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1405 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1406 if (error == 0) {
1407 KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1408 FHANDLE_FILEID(fhp)->fid_len == fidsize));
1409 *fhpp = fhp;
1410 } else {
1411 kmem_free(fhp, fhsize);
1412 }
1413 out:
1414 return error;
1415 }
1416
1417 void
1418 vfs_composefh_free(fhandle_t *fhp)
1419 {
1420
1421 vfs__fhfree(fhp);
1422 }
1423
1424 /*
1425 * vfs_fhtovp: lookup a vnode by a filehandle.
1426 */
1427
1428 int
1429 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1430 {
1431 struct mount *mp;
1432 int error;
1433
1434 *vpp = NULL;
1435 mp = vfs_getvfs(FHANDLE_FSID(fhp));
1436 if (mp == NULL) {
1437 error = ESTALE;
1438 goto out;
1439 }
1440 if (mp->mnt_op->vfs_fhtovp == NULL) {
1441 error = EOPNOTSUPP;
1442 goto out;
1443 }
1444 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1445 out:
1446 return error;
1447 }
1448
1449 /*
1450 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1451 * the needed size.
1452 */
1453
1454 int
1455 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1456 {
1457 fhandle_t *fhp;
1458 int error;
1459
1460 *fhpp = NULL;
1461 if (fhsize > FHANDLE_SIZE_MAX) {
1462 return EINVAL;
1463 }
1464 if (fhsize < FHANDLE_SIZE_MIN) {
1465 return EINVAL;
1466 }
1467 again:
1468 fhp = kmem_alloc(fhsize, KM_SLEEP);
1469 if (fhp == NULL) {
1470 return ENOMEM;
1471 }
1472 error = copyin(ufhp, fhp, fhsize);
1473 if (error == 0) {
1474 /* XXX this check shouldn't be here */
1475 if (FHANDLE_SIZE(fhp) == fhsize) {
1476 *fhpp = fhp;
1477 return 0;
1478 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1479 /*
1480 * a kludge for nfsv2 padded handles.
1481 */
1482 size_t sz;
1483
1484 sz = FHANDLE_SIZE(fhp);
1485 kmem_free(fhp, fhsize);
1486 fhsize = sz;
1487 goto again;
1488 } else {
1489 /*
1490 * userland told us wrong size.
1491 */
1492 error = EINVAL;
1493 }
1494 }
1495 kmem_free(fhp, fhsize);
1496 return error;
1497 }
1498
1499 void
1500 vfs_copyinfh_free(fhandle_t *fhp)
1501 {
1502
1503 vfs__fhfree(fhp);
1504 }
1505
1506 /*
1507 * Get file handle system call
1508 */
1509 int
1510 sys___getfh30(struct lwp *l, void *v, register_t *retval)
1511 {
1512 struct sys___getfh30_args /* {
1513 syscallarg(char *) fname;
1514 syscallarg(fhandle_t *) fhp;
1515 syscallarg(size_t *) fh_size;
1516 } */ *uap = v;
1517 struct vnode *vp;
1518 fhandle_t *fh;
1519 int error;
1520 struct nameidata nd;
1521 size_t sz;
1522 size_t usz;
1523
1524 /*
1525 * Must be super user
1526 */
1527 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1528 0, NULL, NULL, NULL);
1529 if (error)
1530 return (error);
1531 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1532 SCARG(uap, fname), l);
1533 error = namei(&nd);
1534 if (error)
1535 return (error);
1536 vp = nd.ni_vp;
1537 error = vfs_composefh_alloc(vp, &fh);
1538 vput(vp);
1539 if (error != 0) {
1540 goto out;
1541 }
1542 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1543 if (error != 0) {
1544 goto out;
1545 }
1546 sz = FHANDLE_SIZE(fh);
1547 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1548 if (error != 0) {
1549 goto out;
1550 }
1551 if (usz >= sz) {
1552 error = copyout(fh, SCARG(uap, fhp), sz);
1553 } else {
1554 error = E2BIG;
1555 }
1556 out:
1557 vfs_composefh_free(fh);
1558 return (error);
1559 }
1560
1561 /*
1562 * Open a file given a file handle.
1563 *
1564 * Check permissions, allocate an open file structure,
1565 * and call the device open routine if any.
1566 */
1567
1568 int
1569 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1570 register_t *retval)
1571 {
1572 struct filedesc *fdp = l->l_proc->p_fd;
1573 struct file *fp;
1574 struct vnode *vp = NULL;
1575 kauth_cred_t cred = l->l_cred;
1576 struct file *nfp;
1577 int type, indx, error=0;
1578 struct flock lf;
1579 struct vattr va;
1580 fhandle_t *fh;
1581 int flags;
1582
1583 /*
1584 * Must be super user
1585 */
1586 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1587 0, NULL, NULL, NULL)))
1588 return (error);
1589
1590 flags = FFLAGS(oflags);
1591 if ((flags & (FREAD | FWRITE)) == 0)
1592 return (EINVAL);
1593 if ((flags & O_CREAT))
1594 return (EINVAL);
1595 /* falloc() will use the file descriptor for us */
1596 if ((error = falloc(l, &nfp, &indx)) != 0)
1597 return (error);
1598 fp = nfp;
1599 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1600 if (error != 0) {
1601 goto bad;
1602 }
1603 error = vfs_fhtovp(fh, &vp);
1604 if (error != 0) {
1605 goto bad;
1606 }
1607
1608 /* Now do an effective vn_open */
1609
1610 if (vp->v_type == VSOCK) {
1611 error = EOPNOTSUPP;
1612 goto bad;
1613 }
1614 if (flags & FREAD) {
1615 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
1616 goto bad;
1617 }
1618 if (flags & (FWRITE | O_TRUNC)) {
1619 if (vp->v_type == VDIR) {
1620 error = EISDIR;
1621 goto bad;
1622 }
1623 if ((error = vn_writechk(vp)) != 0 ||
1624 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
1625 goto bad;
1626 }
1627 if (flags & O_TRUNC) {
1628 VOP_UNLOCK(vp, 0); /* XXX */
1629 VOP_LEASE(vp, l, cred, LEASE_WRITE);
1630 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1631 VATTR_NULL(&va);
1632 va.va_size = 0;
1633 error = VOP_SETATTR(vp, &va, cred, l);
1634 if (error)
1635 goto bad;
1636 }
1637 if ((error = VOP_OPEN(vp, flags, cred, l)) != 0)
1638 goto bad;
1639 if (flags & FWRITE)
1640 vp->v_writecount++;
1641
1642 /* done with modified vn_open, now finish what sys_open does. */
1643
1644 fp->f_flag = flags & FMASK;
1645 fp->f_type = DTYPE_VNODE;
1646 fp->f_ops = &vnops;
1647 fp->f_data = vp;
1648 if (flags & (O_EXLOCK | O_SHLOCK)) {
1649 lf.l_whence = SEEK_SET;
1650 lf.l_start = 0;
1651 lf.l_len = 0;
1652 if (flags & O_EXLOCK)
1653 lf.l_type = F_WRLCK;
1654 else
1655 lf.l_type = F_RDLCK;
1656 type = F_FLOCK;
1657 if ((flags & FNONBLOCK) == 0)
1658 type |= F_WAIT;
1659 VOP_UNLOCK(vp, 0);
1660 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1661 if (error) {
1662 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1663 FILE_UNUSE(fp, l);
1664 ffree(fp);
1665 fdremove(fdp, indx);
1666 return (error);
1667 }
1668 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1669 fp->f_flag |= FHASLOCK;
1670 }
1671 VOP_UNLOCK(vp, 0);
1672 *retval = indx;
1673 FILE_SET_MATURE(fp);
1674 FILE_UNUSE(fp, l);
1675 vfs_copyinfh_free(fh);
1676 return (0);
1677
1678 bad:
1679 FILE_UNUSE(fp, l);
1680 ffree(fp);
1681 fdremove(fdp, indx);
1682 if (vp != NULL)
1683 vput(vp);
1684 vfs_copyinfh_free(fh);
1685 return (error);
1686 }
1687
1688 int
1689 sys___fhopen40(struct lwp *l, void *v, register_t *retval)
1690 {
1691 struct sys___fhopen40_args /* {
1692 syscallarg(const void *) fhp;
1693 syscallarg(size_t) fh_size;
1694 syscallarg(int) flags;
1695 } */ *uap = v;
1696
1697 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
1698 SCARG(uap, flags), retval);
1699 }
1700
1701 int
1702 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
1703 {
1704 int error;
1705 fhandle_t *fh;
1706 struct vnode *vp;
1707
1708 /*
1709 * Must be super user
1710 */
1711 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1712 0, NULL, NULL, NULL)))
1713 return (error);
1714
1715 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1716 if (error != 0)
1717 return error;
1718
1719 error = vfs_fhtovp(fh, &vp);
1720 vfs_copyinfh_free(fh);
1721 if (error != 0)
1722 return error;
1723
1724 error = vn_stat(vp, sb, l);
1725 vput(vp);
1726 return error;
1727 }
1728
1729
1730 /* ARGSUSED */
1731 int
1732 sys___fhstat40(struct lwp *l, void *v, register_t *retval)
1733 {
1734 struct sys___fhstat40_args /* {
1735 syscallarg(const void *) fhp;
1736 syscallarg(size_t) fh_size;
1737 syscallarg(struct stat *) sb;
1738 } */ *uap = v;
1739 struct stat sb;
1740 int error;
1741
1742 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
1743 if (error)
1744 return error;
1745 return copyout(&sb, SCARG(uap, sb), sizeof(sb));
1746 }
1747
1748 int
1749 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
1750 int flags)
1751 {
1752 fhandle_t *fh;
1753 struct mount *mp;
1754 struct vnode *vp;
1755 int error;
1756
1757 /*
1758 * Must be super user
1759 */
1760 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1761 0, NULL, NULL, NULL)))
1762 return error;
1763
1764 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1765 if (error != 0)
1766 return error;
1767
1768 error = vfs_fhtovp(fh, &vp);
1769 vfs_copyinfh_free(fh);
1770 if (error != 0)
1771 return error;
1772
1773 mp = vp->v_mount;
1774 error = dostatvfs(mp, sb, l, flags, 1);
1775 vput(vp);
1776 return error;
1777 }
1778
1779 /* ARGSUSED */
1780 int
1781 sys___fhstatvfs140(struct lwp *l, void *v, register_t *retval)
1782 {
1783 struct sys___fhstatvfs140_args /* {
1784 syscallarg(const void *) fhp;
1785 syscallarg(size_t) fh_size;
1786 syscallarg(struct statvfs *) buf;
1787 syscallarg(int) flags;
1788 } */ *uap = v;
1789 struct statvfs *sb = STATVFSBUF_GET();
1790 int error;
1791
1792 error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
1793 SCARG(uap, flags));
1794 if (error == 0)
1795 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1796 STATVFSBUF_PUT(sb);
1797 return error;
1798 }
1799
1800 /*
1801 * Create a special file.
1802 */
1803 /* ARGSUSED */
1804 int
1805 sys_mknod(struct lwp *l, void *v, register_t *retval)
1806 {
1807 struct sys_mknod_args /* {
1808 syscallarg(const char *) path;
1809 syscallarg(int) mode;
1810 syscallarg(int) dev;
1811 } */ *uap = v;
1812 struct proc *p = l->l_proc;
1813 struct vnode *vp;
1814 struct vattr vattr;
1815 int error, optype;
1816 struct nameidata nd;
1817 char *path;
1818 const char *cpath;
1819 enum uio_seg seg = UIO_USERSPACE;
1820
1821 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
1822 0, NULL, NULL, NULL)) != 0)
1823 return (error);
1824
1825 optype = VOP_MKNOD_DESCOFFSET;
1826
1827 VERIEXEC_PATH_GET(SCARG(uap, path), seg, cpath, path);
1828 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, seg, cpath, l);
1829
1830 if ((error = namei(&nd)) != 0)
1831 goto out;
1832 vp = nd.ni_vp;
1833 if (vp != NULL)
1834 error = EEXIST;
1835 else {
1836 VATTR_NULL(&vattr);
1837 /* We will read cwdi->cwdi_cmask unlocked. */
1838 vattr.va_mode =
1839 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1840 vattr.va_rdev = SCARG(uap, dev);
1841
1842 switch (SCARG(uap, mode) & S_IFMT) {
1843 case S_IFMT: /* used by badsect to flag bad sectors */
1844 vattr.va_type = VBAD;
1845 break;
1846 case S_IFCHR:
1847 vattr.va_type = VCHR;
1848 break;
1849 case S_IFBLK:
1850 vattr.va_type = VBLK;
1851 break;
1852 case S_IFWHT:
1853 optype = VOP_WHITEOUT_DESCOFFSET;
1854 break;
1855 case S_IFREG:
1856 #if NVERIEXEC > 0
1857 error = veriexec_openchk(l, nd.ni_vp, nd.ni_dirp,
1858 O_CREAT);
1859 #endif /* NVERIEXEC > 0 */
1860 vattr.va_type = VREG;
1861 vattr.va_rdev = VNOVAL;
1862 optype = VOP_CREATE_DESCOFFSET;
1863 break;
1864 default:
1865 error = EINVAL;
1866 break;
1867 }
1868 }
1869 if (!error) {
1870 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1871 switch (optype) {
1872 case VOP_WHITEOUT_DESCOFFSET:
1873 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1874 if (error)
1875 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1876 vput(nd.ni_dvp);
1877 break;
1878
1879 case VOP_MKNOD_DESCOFFSET:
1880 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1881 &nd.ni_cnd, &vattr);
1882 if (error == 0)
1883 vput(nd.ni_vp);
1884 break;
1885
1886 case VOP_CREATE_DESCOFFSET:
1887 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
1888 &nd.ni_cnd, &vattr);
1889 if (error == 0)
1890 vput(nd.ni_vp);
1891 break;
1892 }
1893 } else {
1894 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1895 if (nd.ni_dvp == vp)
1896 vrele(nd.ni_dvp);
1897 else
1898 vput(nd.ni_dvp);
1899 if (vp)
1900 vrele(vp);
1901 }
1902 out:
1903 VERIEXEC_PATH_PUT(path);
1904 return (error);
1905 }
1906
1907 /*
1908 * Create a named pipe.
1909 */
1910 /* ARGSUSED */
1911 int
1912 sys_mkfifo(struct lwp *l, void *v, register_t *retval)
1913 {
1914 struct sys_mkfifo_args /* {
1915 syscallarg(const char *) path;
1916 syscallarg(int) mode;
1917 } */ *uap = v;
1918 struct proc *p = l->l_proc;
1919 struct vattr vattr;
1920 int error;
1921 struct nameidata nd;
1922
1923 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1924 if ((error = namei(&nd)) != 0)
1925 return (error);
1926 if (nd.ni_vp != NULL) {
1927 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1928 if (nd.ni_dvp == nd.ni_vp)
1929 vrele(nd.ni_dvp);
1930 else
1931 vput(nd.ni_dvp);
1932 vrele(nd.ni_vp);
1933 return (EEXIST);
1934 }
1935 VATTR_NULL(&vattr);
1936 vattr.va_type = VFIFO;
1937 /* We will read cwdi->cwdi_cmask unlocked. */
1938 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1939 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1940 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1941 if (error == 0)
1942 vput(nd.ni_vp);
1943 return (error);
1944 }
1945
1946 /*
1947 * Make a hard file link.
1948 */
1949 /* ARGSUSED */
1950 int
1951 sys_link(struct lwp *l, void *v, register_t *retval)
1952 {
1953 struct sys_link_args /* {
1954 syscallarg(const char *) path;
1955 syscallarg(const char *) link;
1956 } */ *uap = v;
1957 struct vnode *vp;
1958 struct nameidata nd;
1959 int error;
1960
1961 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1962 if ((error = namei(&nd)) != 0)
1963 return (error);
1964 vp = nd.ni_vp;
1965 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, link), l);
1966 if ((error = namei(&nd)) != 0)
1967 goto out;
1968 if (nd.ni_vp) {
1969 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1970 if (nd.ni_dvp == nd.ni_vp)
1971 vrele(nd.ni_dvp);
1972 else
1973 vput(nd.ni_dvp);
1974 vrele(nd.ni_vp);
1975 error = EEXIST;
1976 goto out;
1977 }
1978 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1979 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
1980 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1981 out:
1982 vrele(vp);
1983 return (error);
1984 }
1985
1986 /*
1987 * Make a symbolic link.
1988 */
1989 /* ARGSUSED */
1990 int
1991 sys_symlink(struct lwp *l, void *v, register_t *retval)
1992 {
1993 struct sys_symlink_args /* {
1994 syscallarg(const char *) path;
1995 syscallarg(const char *) link;
1996 } */ *uap = v;
1997 struct proc *p = l->l_proc;
1998 struct vattr vattr;
1999 char *path;
2000 int error;
2001 struct nameidata nd;
2002
2003 path = PNBUF_GET();
2004 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
2005 if (error)
2006 goto out;
2007 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, link), l);
2008 if ((error = namei(&nd)) != 0)
2009 goto out;
2010 if (nd.ni_vp) {
2011 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2012 if (nd.ni_dvp == nd.ni_vp)
2013 vrele(nd.ni_dvp);
2014 else
2015 vput(nd.ni_dvp);
2016 vrele(nd.ni_vp);
2017 error = EEXIST;
2018 goto out;
2019 }
2020 VATTR_NULL(&vattr);
2021 vattr.va_type = VLNK;
2022 /* We will read cwdi->cwdi_cmask unlocked. */
2023 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
2024 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
2025 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
2026 if (error == 0)
2027 vput(nd.ni_vp);
2028 out:
2029 PNBUF_PUT(path);
2030 return (error);
2031 }
2032
2033 /*
2034 * Delete a whiteout from the filesystem.
2035 */
2036 /* ARGSUSED */
2037 int
2038 sys_undelete(struct lwp *l, void *v, register_t *retval)
2039 {
2040 struct sys_undelete_args /* {
2041 syscallarg(const char *) path;
2042 } */ *uap = v;
2043 int error;
2044 struct nameidata nd;
2045
2046 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, UIO_USERSPACE,
2047 SCARG(uap, path), l);
2048 error = namei(&nd);
2049 if (error)
2050 return (error);
2051
2052 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
2053 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2054 if (nd.ni_dvp == nd.ni_vp)
2055 vrele(nd.ni_dvp);
2056 else
2057 vput(nd.ni_dvp);
2058 if (nd.ni_vp)
2059 vrele(nd.ni_vp);
2060 return (EEXIST);
2061 }
2062 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
2063 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
2064 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2065 vput(nd.ni_dvp);
2066 return (error);
2067 }
2068
2069 /*
2070 * Delete a name from the filesystem.
2071 */
2072 /* ARGSUSED */
2073 int
2074 sys_unlink(struct lwp *l, void *v, register_t *retval)
2075 {
2076 struct sys_unlink_args /* {
2077 syscallarg(const char *) path;
2078 } */ *uap = v;
2079 struct vnode *vp;
2080 int error;
2081 struct nameidata nd;
2082 char *path;
2083 const char *cpath;
2084 enum uio_seg seg = UIO_USERSPACE;
2085
2086 VERIEXEC_PATH_GET(SCARG(uap, path), seg, cpath, path);
2087 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, seg, cpath, l);
2088
2089 if ((error = namei(&nd)) != 0)
2090 goto out;
2091 vp = nd.ni_vp;
2092
2093 /*
2094 * The root of a mounted filesystem cannot be deleted.
2095 */
2096 if (vp->v_vflag & VV_ROOT) {
2097 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2098 if (nd.ni_dvp == vp)
2099 vrele(nd.ni_dvp);
2100 else
2101 vput(nd.ni_dvp);
2102 vput(vp);
2103 error = EBUSY;
2104 goto out;
2105 }
2106
2107 #if NVERIEXEC > 0
2108 /* Handle remove requests for veriexec entries. */
2109 if ((error = veriexec_removechk(l, nd.ni_vp, nd.ni_dirp)) != 0) {
2110 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2111 if (nd.ni_dvp == vp)
2112 vrele(nd.ni_dvp);
2113 else
2114 vput(nd.ni_dvp);
2115 vput(vp);
2116 goto out;
2117 }
2118 #endif /* NVERIEXEC > 0 */
2119
2120 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
2121 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2122 #ifdef FILEASSOC
2123 (void)fileassoc_file_delete(vp);
2124 #endif /* FILEASSOC */
2125 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2126 out:
2127 VERIEXEC_PATH_PUT(path);
2128 return (error);
2129 }
2130
2131 /*
2132 * Reposition read/write file offset.
2133 */
2134 int
2135 sys_lseek(struct lwp *l, void *v, register_t *retval)
2136 {
2137 struct sys_lseek_args /* {
2138 syscallarg(int) fd;
2139 syscallarg(int) pad;
2140 syscallarg(off_t) offset;
2141 syscallarg(int) whence;
2142 } */ *uap = v;
2143 struct proc *p = l->l_proc;
2144 kauth_cred_t cred = l->l_cred;
2145 struct filedesc *fdp = p->p_fd;
2146 struct file *fp;
2147 struct vnode *vp;
2148 struct vattr vattr;
2149 off_t newoff;
2150 int error;
2151
2152 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
2153 return (EBADF);
2154
2155 vp = (struct vnode *)fp->f_data;
2156 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2157 error = ESPIPE;
2158 mutex_exit(&fp->f_lock);
2159 goto out;
2160 }
2161
2162 switch (SCARG(uap, whence)) {
2163 case SEEK_CUR:
2164 newoff = fp->f_offset + SCARG(uap, offset);
2165 FILE_USE(fp);
2166 break;
2167 case SEEK_END:
2168 FILE_USE(fp);
2169 error = VOP_GETATTR(vp, &vattr, cred, l);
2170 if (error) {
2171 FILE_UNUSE(fp, l);
2172 goto out;
2173 }
2174 newoff = SCARG(uap, offset) + vattr.va_size;
2175 break;
2176 case SEEK_SET:
2177 FILE_USE(fp);
2178 newoff = SCARG(uap, offset);
2179 break;
2180 default:
2181 mutex_exit(&fp->f_lock);
2182 error = EINVAL;
2183 goto out;
2184 }
2185 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
2186 mutex_enter(&fp->f_lock);
2187 *(off_t *)retval = fp->f_offset = newoff;
2188 mutex_exit(&fp->f_lock);
2189 }
2190 FILE_UNUSE(fp, l);
2191 out:
2192 return (error);
2193 }
2194
2195 /*
2196 * Positional read system call.
2197 */
2198 int
2199 sys_pread(struct lwp *l, void *v, register_t *retval)
2200 {
2201 struct sys_pread_args /* {
2202 syscallarg(int) fd;
2203 syscallarg(void *) buf;
2204 syscallarg(size_t) nbyte;
2205 syscallarg(off_t) offset;
2206 } */ *uap = v;
2207 struct proc *p = l->l_proc;
2208 struct filedesc *fdp = p->p_fd;
2209 struct file *fp;
2210 struct vnode *vp;
2211 off_t offset;
2212 int error, fd = SCARG(uap, fd);
2213
2214 if ((fp = fd_getfile(fdp, fd)) == NULL)
2215 return (EBADF);
2216
2217 if ((fp->f_flag & FREAD) == 0) {
2218 mutex_exit(&fp->f_lock);
2219 return (EBADF);
2220 }
2221
2222 FILE_USE(fp);
2223
2224 vp = (struct vnode *)fp->f_data;
2225 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2226 error = ESPIPE;
2227 goto out;
2228 }
2229
2230 offset = SCARG(uap, offset);
2231
2232 /*
2233 * XXX This works because no file systems actually
2234 * XXX take any action on the seek operation.
2235 */
2236 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2237 goto out;
2238
2239 /* dofileread() will unuse the descriptor for us */
2240 return (dofileread(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2241 &offset, 0, retval));
2242
2243 out:
2244 FILE_UNUSE(fp, l);
2245 return (error);
2246 }
2247
2248 /*
2249 * Positional scatter read system call.
2250 */
2251 int
2252 sys_preadv(struct lwp *l, void *v, register_t *retval)
2253 {
2254 struct sys_preadv_args /* {
2255 syscallarg(int) fd;
2256 syscallarg(const struct iovec *) iovp;
2257 syscallarg(int) iovcnt;
2258 syscallarg(off_t) offset;
2259 } */ *uap = v;
2260
2261 return do_filereadv(l, SCARG(uap, fd), SCARG(uap, iovp),
2262 SCARG(uap, iovcnt), &SCARG(uap, offset), 0, retval);
2263 }
2264
2265 /*
2266 * Positional write system call.
2267 */
2268 int
2269 sys_pwrite(struct lwp *l, void *v, register_t *retval)
2270 {
2271 struct sys_pwrite_args /* {
2272 syscallarg(int) fd;
2273 syscallarg(const void *) buf;
2274 syscallarg(size_t) nbyte;
2275 syscallarg(off_t) offset;
2276 } */ *uap = v;
2277 struct proc *p = l->l_proc;
2278 struct filedesc *fdp = p->p_fd;
2279 struct file *fp;
2280 struct vnode *vp;
2281 off_t offset;
2282 int error, fd = SCARG(uap, fd);
2283
2284 if ((fp = fd_getfile(fdp, fd)) == NULL)
2285 return (EBADF);
2286
2287 if ((fp->f_flag & FWRITE) == 0) {
2288 mutex_exit(&fp->f_lock);
2289 return (EBADF);
2290 }
2291
2292 FILE_USE(fp);
2293
2294 vp = (struct vnode *)fp->f_data;
2295 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2296 error = ESPIPE;
2297 goto out;
2298 }
2299
2300 offset = SCARG(uap, offset);
2301
2302 /*
2303 * XXX This works because no file systems actually
2304 * XXX take any action on the seek operation.
2305 */
2306 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2307 goto out;
2308
2309 /* dofilewrite() will unuse the descriptor for us */
2310 return (dofilewrite(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2311 &offset, 0, retval));
2312
2313 out:
2314 FILE_UNUSE(fp, l);
2315 return (error);
2316 }
2317
2318 /*
2319 * Positional gather write system call.
2320 */
2321 int
2322 sys_pwritev(struct lwp *l, void *v, register_t *retval)
2323 {
2324 struct sys_pwritev_args /* {
2325 syscallarg(int) fd;
2326 syscallarg(const struct iovec *) iovp;
2327 syscallarg(int) iovcnt;
2328 syscallarg(off_t) offset;
2329 } */ *uap = v;
2330
2331 return do_filewritev(l, SCARG(uap, fd), SCARG(uap, iovp),
2332 SCARG(uap, iovcnt), &SCARG(uap, offset), 0, retval);
2333 }
2334
2335 /*
2336 * Check access permissions.
2337 */
2338 int
2339 sys_access(struct lwp *l, void *v, register_t *retval)
2340 {
2341 struct sys_access_args /* {
2342 syscallarg(const char *) path;
2343 syscallarg(int) flags;
2344 } */ *uap = v;
2345 kauth_cred_t cred;
2346 struct vnode *vp;
2347 int error, flags;
2348 struct nameidata nd;
2349
2350 cred = kauth_cred_dup(l->l_cred);
2351 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2352 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2353 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2354 SCARG(uap, path), l);
2355 /* Override default credentials */
2356 nd.ni_cnd.cn_cred = cred;
2357 if ((error = namei(&nd)) != 0)
2358 goto out;
2359 vp = nd.ni_vp;
2360
2361 /* Flags == 0 means only check for existence. */
2362 if (SCARG(uap, flags)) {
2363 flags = 0;
2364 if (SCARG(uap, flags) & R_OK)
2365 flags |= VREAD;
2366 if (SCARG(uap, flags) & W_OK)
2367 flags |= VWRITE;
2368 if (SCARG(uap, flags) & X_OK)
2369 flags |= VEXEC;
2370
2371 error = VOP_ACCESS(vp, flags, cred, l);
2372 if (!error && (flags & VWRITE))
2373 error = vn_writechk(vp);
2374 }
2375 vput(vp);
2376 out:
2377 kauth_cred_free(cred);
2378 return (error);
2379 }
2380
2381 /*
2382 * Common code for all sys_stat functions, including compat versions.
2383 */
2384 int
2385 do_sys_stat(struct lwp *l, const char *path, unsigned int nd_flags,
2386 struct stat *sb)
2387 {
2388 int error;
2389 struct nameidata nd;
2390
2391 NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT , UIO_USERSPACE, path, l);
2392 error = namei(&nd);
2393 if (error != 0)
2394 return error;
2395 error = vn_stat(nd.ni_vp, sb, l);
2396 vput(nd.ni_vp);
2397 return error;
2398 }
2399
2400 /*
2401 * Get file status; this version follows links.
2402 */
2403 /* ARGSUSED */
2404 int
2405 sys___stat30(struct lwp *l, void *v, register_t *retval)
2406 {
2407 struct sys___stat30_args /* {
2408 syscallarg(const char *) path;
2409 syscallarg(struct stat *) ub;
2410 } */ *uap = v;
2411 struct stat sb;
2412 int error;
2413
2414 error = do_sys_stat(l, SCARG(uap, path), FOLLOW, &sb);
2415 if (error)
2416 return error;
2417 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2418 }
2419
2420 /*
2421 * Get file status; this version does not follow links.
2422 */
2423 /* ARGSUSED */
2424 int
2425 sys___lstat30(struct lwp *l, void *v, register_t *retval)
2426 {
2427 struct sys___lstat30_args /* {
2428 syscallarg(const char *) path;
2429 syscallarg(struct stat *) ub;
2430 } */ *uap = v;
2431 struct stat sb;
2432 int error;
2433
2434 error = do_sys_stat(l, SCARG(uap, path), NOFOLLOW, &sb);
2435 if (error)
2436 return error;
2437 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2438 }
2439
2440 /*
2441 * Get configurable pathname variables.
2442 */
2443 /* ARGSUSED */
2444 int
2445 sys_pathconf(struct lwp *l, void *v, register_t *retval)
2446 {
2447 struct sys_pathconf_args /* {
2448 syscallarg(const char *) path;
2449 syscallarg(int) name;
2450 } */ *uap = v;
2451 int error;
2452 struct nameidata nd;
2453
2454 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2455 SCARG(uap, path), l);
2456 if ((error = namei(&nd)) != 0)
2457 return (error);
2458 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2459 vput(nd.ni_vp);
2460 return (error);
2461 }
2462
2463 /*
2464 * Return target name of a symbolic link.
2465 */
2466 /* ARGSUSED */
2467 int
2468 sys_readlink(struct lwp *l, void *v, register_t *retval)
2469 {
2470 struct sys_readlink_args /* {
2471 syscallarg(const char *) path;
2472 syscallarg(char *) buf;
2473 syscallarg(size_t) count;
2474 } */ *uap = v;
2475 struct vnode *vp;
2476 struct iovec aiov;
2477 struct uio auio;
2478 int error;
2479 struct nameidata nd;
2480
2481 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2482 SCARG(uap, path), l);
2483 if ((error = namei(&nd)) != 0)
2484 return (error);
2485 vp = nd.ni_vp;
2486 if (vp->v_type != VLNK)
2487 error = EINVAL;
2488 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2489 (error = VOP_ACCESS(vp, VREAD, l->l_cred, l)) == 0) {
2490 aiov.iov_base = SCARG(uap, buf);
2491 aiov.iov_len = SCARG(uap, count);
2492 auio.uio_iov = &aiov;
2493 auio.uio_iovcnt = 1;
2494 auio.uio_offset = 0;
2495 auio.uio_rw = UIO_READ;
2496 KASSERT(l == curlwp);
2497 auio.uio_vmspace = l->l_proc->p_vmspace;
2498 auio.uio_resid = SCARG(uap, count);
2499 error = VOP_READLINK(vp, &auio, l->l_cred);
2500 }
2501 vput(vp);
2502 *retval = SCARG(uap, count) - auio.uio_resid;
2503 return (error);
2504 }
2505
2506 /*
2507 * Change flags of a file given a path name.
2508 */
2509 /* ARGSUSED */
2510 int
2511 sys_chflags(struct lwp *l, void *v, register_t *retval)
2512 {
2513 struct sys_chflags_args /* {
2514 syscallarg(const char *) path;
2515 syscallarg(u_long) flags;
2516 } */ *uap = v;
2517 struct vnode *vp;
2518 int error;
2519 struct nameidata nd;
2520
2521 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2522 if ((error = namei(&nd)) != 0)
2523 return (error);
2524 vp = nd.ni_vp;
2525 error = change_flags(vp, SCARG(uap, flags), l);
2526 vput(vp);
2527 return (error);
2528 }
2529
2530 /*
2531 * Change flags of a file given a file descriptor.
2532 */
2533 /* ARGSUSED */
2534 int
2535 sys_fchflags(struct lwp *l, void *v, register_t *retval)
2536 {
2537 struct sys_fchflags_args /* {
2538 syscallarg(int) fd;
2539 syscallarg(u_long) flags;
2540 } */ *uap = v;
2541 struct proc *p = l->l_proc;
2542 struct vnode *vp;
2543 struct file *fp;
2544 int error;
2545
2546 /* getvnode() will use the descriptor for us */
2547 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2548 return (error);
2549 vp = (struct vnode *)fp->f_data;
2550 error = change_flags(vp, SCARG(uap, flags), l);
2551 VOP_UNLOCK(vp, 0);
2552 FILE_UNUSE(fp, l);
2553 return (error);
2554 }
2555
2556 /*
2557 * Change flags of a file given a path name; this version does
2558 * not follow links.
2559 */
2560 int
2561 sys_lchflags(struct lwp *l, void *v, register_t *retval)
2562 {
2563 struct sys_lchflags_args /* {
2564 syscallarg(const char *) path;
2565 syscallarg(u_long) flags;
2566 } */ *uap = v;
2567 struct vnode *vp;
2568 int error;
2569 struct nameidata nd;
2570
2571 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2572 if ((error = namei(&nd)) != 0)
2573 return (error);
2574 vp = nd.ni_vp;
2575 error = change_flags(vp, SCARG(uap, flags), l);
2576 vput(vp);
2577 return (error);
2578 }
2579
2580 /*
2581 * Common routine to change flags of a file.
2582 */
2583 int
2584 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
2585 {
2586 struct vattr vattr;
2587 int error;
2588
2589 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2590 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2591 /*
2592 * Non-superusers cannot change the flags on devices, even if they
2593 * own them.
2594 */
2595 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
2596 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
2597 goto out;
2598 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2599 error = EINVAL;
2600 goto out;
2601 }
2602 }
2603 VATTR_NULL(&vattr);
2604 vattr.va_flags = flags;
2605 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2606 out:
2607 return (error);
2608 }
2609
2610 /*
2611 * Change mode of a file given path name; this version follows links.
2612 */
2613 /* ARGSUSED */
2614 int
2615 sys_chmod(struct lwp *l, void *v, register_t *retval)
2616 {
2617 struct sys_chmod_args /* {
2618 syscallarg(const char *) path;
2619 syscallarg(int) mode;
2620 } */ *uap = v;
2621 int error;
2622 struct nameidata nd;
2623
2624 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2625 if ((error = namei(&nd)) != 0)
2626 return (error);
2627
2628 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2629
2630 vrele(nd.ni_vp);
2631 return (error);
2632 }
2633
2634 /*
2635 * Change mode of a file given a file descriptor.
2636 */
2637 /* ARGSUSED */
2638 int
2639 sys_fchmod(struct lwp *l, void *v, register_t *retval)
2640 {
2641 struct sys_fchmod_args /* {
2642 syscallarg(int) fd;
2643 syscallarg(int) mode;
2644 } */ *uap = v;
2645 struct proc *p = l->l_proc;
2646 struct file *fp;
2647 int error;
2648
2649 /* getvnode() will use the descriptor for us */
2650 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2651 return (error);
2652
2653 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), l);
2654 FILE_UNUSE(fp, l);
2655 return (error);
2656 }
2657
2658 /*
2659 * Change mode of a file given path name; this version does not follow links.
2660 */
2661 /* ARGSUSED */
2662 int
2663 sys_lchmod(struct lwp *l, void *v, register_t *retval)
2664 {
2665 struct sys_lchmod_args /* {
2666 syscallarg(const char *) path;
2667 syscallarg(int) mode;
2668 } */ *uap = v;
2669 int error;
2670 struct nameidata nd;
2671
2672 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2673 if ((error = namei(&nd)) != 0)
2674 return (error);
2675
2676 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2677
2678 vrele(nd.ni_vp);
2679 return (error);
2680 }
2681
2682 /*
2683 * Common routine to set mode given a vnode.
2684 */
2685 static int
2686 change_mode(struct vnode *vp, int mode, struct lwp *l)
2687 {
2688 struct vattr vattr;
2689 int error;
2690
2691 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2692 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2693 VATTR_NULL(&vattr);
2694 vattr.va_mode = mode & ALLPERMS;
2695 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2696 VOP_UNLOCK(vp, 0);
2697 return (error);
2698 }
2699
2700 /*
2701 * Set ownership given a path name; this version follows links.
2702 */
2703 /* ARGSUSED */
2704 int
2705 sys_chown(struct lwp *l, void *v, register_t *retval)
2706 {
2707 struct sys_chown_args /* {
2708 syscallarg(const char *) path;
2709 syscallarg(uid_t) uid;
2710 syscallarg(gid_t) gid;
2711 } */ *uap = v;
2712 int error;
2713 struct nameidata nd;
2714
2715 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2716 if ((error = namei(&nd)) != 0)
2717 return (error);
2718
2719 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2720
2721 vrele(nd.ni_vp);
2722 return (error);
2723 }
2724
2725 /*
2726 * Set ownership given a path name; this version follows links.
2727 * Provides POSIX semantics.
2728 */
2729 /* ARGSUSED */
2730 int
2731 sys___posix_chown(struct lwp *l, void *v, register_t *retval)
2732 {
2733 struct sys_chown_args /* {
2734 syscallarg(const char *) path;
2735 syscallarg(uid_t) uid;
2736 syscallarg(gid_t) gid;
2737 } */ *uap = v;
2738 int error;
2739 struct nameidata nd;
2740
2741 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2742 if ((error = namei(&nd)) != 0)
2743 return (error);
2744
2745 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2746
2747 vrele(nd.ni_vp);
2748 return (error);
2749 }
2750
2751 /*
2752 * Set ownership given a file descriptor.
2753 */
2754 /* ARGSUSED */
2755 int
2756 sys_fchown(struct lwp *l, void *v, register_t *retval)
2757 {
2758 struct sys_fchown_args /* {
2759 syscallarg(int) fd;
2760 syscallarg(uid_t) uid;
2761 syscallarg(gid_t) gid;
2762 } */ *uap = v;
2763 struct proc *p = l->l_proc;
2764 int error;
2765 struct file *fp;
2766
2767 /* getvnode() will use the descriptor for us */
2768 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2769 return (error);
2770
2771 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2772 SCARG(uap, gid), l, 0);
2773 FILE_UNUSE(fp, l);
2774 return (error);
2775 }
2776
2777 /*
2778 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2779 */
2780 /* ARGSUSED */
2781 int
2782 sys___posix_fchown(struct lwp *l, void *v, register_t *retval)
2783 {
2784 struct sys_fchown_args /* {
2785 syscallarg(int) fd;
2786 syscallarg(uid_t) uid;
2787 syscallarg(gid_t) gid;
2788 } */ *uap = v;
2789 struct proc *p = l->l_proc;
2790 int error;
2791 struct file *fp;
2792
2793 /* getvnode() will use the descriptor for us */
2794 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2795 return (error);
2796
2797 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2798 SCARG(uap, gid), l, 1);
2799 FILE_UNUSE(fp, l);
2800 return (error);
2801 }
2802
2803 /*
2804 * Set ownership given a path name; this version does not follow links.
2805 */
2806 /* ARGSUSED */
2807 int
2808 sys_lchown(struct lwp *l, void *v, register_t *retval)
2809 {
2810 struct sys_lchown_args /* {
2811 syscallarg(const char *) path;
2812 syscallarg(uid_t) uid;
2813 syscallarg(gid_t) gid;
2814 } */ *uap = v;
2815 int error;
2816 struct nameidata nd;
2817
2818 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2819 if ((error = namei(&nd)) != 0)
2820 return (error);
2821
2822 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2823
2824 vrele(nd.ni_vp);
2825 return (error);
2826 }
2827
2828 /*
2829 * Set ownership given a path name; this version does not follow links.
2830 * Provides POSIX/XPG semantics.
2831 */
2832 /* ARGSUSED */
2833 int
2834 sys___posix_lchown(struct lwp *l, void *v, register_t *retval)
2835 {
2836 struct sys_lchown_args /* {
2837 syscallarg(const char *) path;
2838 syscallarg(uid_t) uid;
2839 syscallarg(gid_t) gid;
2840 } */ *uap = v;
2841 int error;
2842 struct nameidata nd;
2843
2844 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2845 if ((error = namei(&nd)) != 0)
2846 return (error);
2847
2848 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2849
2850 vrele(nd.ni_vp);
2851 return (error);
2852 }
2853
2854 /*
2855 * Common routine to set ownership given a vnode.
2856 */
2857 static int
2858 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2859 int posix_semantics)
2860 {
2861 struct vattr vattr;
2862 mode_t newmode;
2863 int error;
2864
2865 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2866 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2867 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
2868 goto out;
2869
2870 #define CHANGED(x) ((int)(x) != -1)
2871 newmode = vattr.va_mode;
2872 if (posix_semantics) {
2873 /*
2874 * POSIX/XPG semantics: if the caller is not the super-user,
2875 * clear set-user-id and set-group-id bits. Both POSIX and
2876 * the XPG consider the behaviour for calls by the super-user
2877 * implementation-defined; we leave the set-user-id and set-
2878 * group-id settings intact in that case.
2879 */
2880 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2881 NULL) != 0)
2882 newmode &= ~(S_ISUID | S_ISGID);
2883 } else {
2884 /*
2885 * NetBSD semantics: when changing owner and/or group,
2886 * clear the respective bit(s).
2887 */
2888 if (CHANGED(uid))
2889 newmode &= ~S_ISUID;
2890 if (CHANGED(gid))
2891 newmode &= ~S_ISGID;
2892 }
2893 /* Update va_mode iff altered. */
2894 if (vattr.va_mode == newmode)
2895 newmode = VNOVAL;
2896
2897 VATTR_NULL(&vattr);
2898 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2899 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2900 vattr.va_mode = newmode;
2901 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2902 #undef CHANGED
2903
2904 out:
2905 VOP_UNLOCK(vp, 0);
2906 return (error);
2907 }
2908
2909 /*
2910 * Set the access and modification times given a path name; this
2911 * version follows links.
2912 */
2913 /* ARGSUSED */
2914 int
2915 sys_utimes(struct lwp *l, void *v, register_t *retval)
2916 {
2917 struct sys_utimes_args /* {
2918 syscallarg(const char *) path;
2919 syscallarg(const struct timeval *) tptr;
2920 } */ *uap = v;
2921
2922 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
2923 SCARG(uap, tptr), UIO_USERSPACE);
2924 }
2925
2926 /*
2927 * Set the access and modification times given a file descriptor.
2928 */
2929 /* ARGSUSED */
2930 int
2931 sys_futimes(struct lwp *l, void *v, register_t *retval)
2932 {
2933 struct sys_futimes_args /* {
2934 syscallarg(int) fd;
2935 syscallarg(const struct timeval *) tptr;
2936 } */ *uap = v;
2937 int error;
2938 struct file *fp;
2939
2940 /* getvnode() will use the descriptor for us */
2941 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2942 return (error);
2943
2944 error = do_sys_utimes(l, fp->f_data, NULL, 0,
2945 SCARG(uap, tptr), UIO_USERSPACE);
2946
2947 FILE_UNUSE(fp, l);
2948 return (error);
2949 }
2950
2951 /*
2952 * Set the access and modification times given a path name; this
2953 * version does not follow links.
2954 */
2955 int
2956 sys_lutimes(struct lwp *l, void *v, register_t *retval)
2957 {
2958 struct sys_lutimes_args /* {
2959 syscallarg(const char *) path;
2960 syscallarg(const struct timeval *) tptr;
2961 } */ *uap = v;
2962
2963 return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
2964 SCARG(uap, tptr), UIO_USERSPACE);
2965 }
2966
2967 /*
2968 * Common routine to set access and modification times given a vnode.
2969 */
2970 int
2971 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
2972 const struct timeval *tptr, enum uio_seg seg)
2973 {
2974 struct vattr vattr;
2975 struct nameidata nd;
2976 int error;
2977
2978 VATTR_NULL(&vattr);
2979 if (tptr == NULL) {
2980 nanotime(&vattr.va_atime);
2981 vattr.va_mtime = vattr.va_atime;
2982 vattr.va_vaflags |= VA_UTIMES_NULL;
2983 } else {
2984 struct timeval tv[2];
2985
2986 if (seg != UIO_SYSSPACE) {
2987 error = copyin(tptr, &tv, sizeof (tv));
2988 if (error != 0)
2989 return error;
2990 tptr = tv;
2991 }
2992 TIMEVAL_TO_TIMESPEC(tptr, &vattr.va_atime);
2993 TIMEVAL_TO_TIMESPEC(tptr + 1, &vattr.va_mtime);
2994 }
2995
2996 if (vp == NULL) {
2997 NDINIT(&nd, LOOKUP, flag | TRYEMULROOT, UIO_USERSPACE, path, l);
2998 if ((error = namei(&nd)) != 0)
2999 return (error);
3000 vp = nd.ni_vp;
3001 } else
3002 nd.ni_vp = NULL;
3003
3004 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3005 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3006 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
3007 VOP_UNLOCK(vp, 0);
3008
3009 if (nd.ni_vp != NULL)
3010 vrele(nd.ni_vp);
3011
3012 return (error);
3013 }
3014
3015 /*
3016 * Truncate a file given its path name.
3017 */
3018 /* ARGSUSED */
3019 int
3020 sys_truncate(struct lwp *l, void *v, register_t *retval)
3021 {
3022 struct sys_truncate_args /* {
3023 syscallarg(const char *) path;
3024 syscallarg(int) pad;
3025 syscallarg(off_t) length;
3026 } */ *uap = v;
3027 struct vnode *vp;
3028 struct vattr vattr;
3029 int error;
3030 struct nameidata nd;
3031
3032 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
3033 if ((error = namei(&nd)) != 0)
3034 return (error);
3035 vp = nd.ni_vp;
3036 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3037 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3038 if (vp->v_type == VDIR)
3039 error = EISDIR;
3040 else if ((error = vn_writechk(vp)) == 0 &&
3041 (error = VOP_ACCESS(vp, VWRITE, l->l_cred, l)) == 0) {
3042 VATTR_NULL(&vattr);
3043 vattr.va_size = SCARG(uap, length);
3044 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
3045 }
3046 vput(vp);
3047 return (error);
3048 }
3049
3050 /*
3051 * Truncate a file given a file descriptor.
3052 */
3053 /* ARGSUSED */
3054 int
3055 sys_ftruncate(struct lwp *l, void *v, register_t *retval)
3056 {
3057 struct sys_ftruncate_args /* {
3058 syscallarg(int) fd;
3059 syscallarg(int) pad;
3060 syscallarg(off_t) length;
3061 } */ *uap = v;
3062 struct proc *p = l->l_proc;
3063 struct vattr vattr;
3064 struct vnode *vp;
3065 struct file *fp;
3066 int error;
3067
3068 /* getvnode() will use the descriptor for us */
3069 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3070 return (error);
3071 if ((fp->f_flag & FWRITE) == 0) {
3072 error = EINVAL;
3073 goto out;
3074 }
3075 vp = (struct vnode *)fp->f_data;
3076 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3077 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3078 if (vp->v_type == VDIR)
3079 error = EISDIR;
3080 else if ((error = vn_writechk(vp)) == 0) {
3081 VATTR_NULL(&vattr);
3082 vattr.va_size = SCARG(uap, length);
3083 error = VOP_SETATTR(vp, &vattr, fp->f_cred, l);
3084 }
3085 VOP_UNLOCK(vp, 0);
3086 out:
3087 FILE_UNUSE(fp, l);
3088 return (error);
3089 }
3090
3091 /*
3092 * Sync an open file.
3093 */
3094 /* ARGSUSED */
3095 int
3096 sys_fsync(struct lwp *l, void *v, register_t *retval)
3097 {
3098 struct sys_fsync_args /* {
3099 syscallarg(int) fd;
3100 } */ *uap = v;
3101 struct proc *p = l->l_proc;
3102 struct vnode *vp;
3103 struct file *fp;
3104 int error;
3105
3106 /* getvnode() will use the descriptor for us */
3107 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3108 return (error);
3109 vp = (struct vnode *)fp->f_data;
3110 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3111 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, l);
3112 if (error == 0 && bioops != NULL &&
3113 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3114 (*bioops->io_fsync)(vp, 0);
3115 VOP_UNLOCK(vp, 0);
3116 FILE_UNUSE(fp, l);
3117 return (error);
3118 }
3119
3120 /*
3121 * Sync a range of file data. API modeled after that found in AIX.
3122 *
3123 * FDATASYNC indicates that we need only save enough metadata to be able
3124 * to re-read the written data. Note we duplicate AIX's requirement that
3125 * the file be open for writing.
3126 */
3127 /* ARGSUSED */
3128 int
3129 sys_fsync_range(struct lwp *l, void *v, register_t *retval)
3130 {
3131 struct sys_fsync_range_args /* {
3132 syscallarg(int) fd;
3133 syscallarg(int) flags;
3134 syscallarg(off_t) start;
3135 syscallarg(off_t) length;
3136 } */ *uap = v;
3137 struct proc *p = l->l_proc;
3138 struct vnode *vp;
3139 struct file *fp;
3140 int flags, nflags;
3141 off_t s, e, len;
3142 int error;
3143
3144 /* getvnode() will use the descriptor for us */
3145 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3146 return (error);
3147
3148 if ((fp->f_flag & FWRITE) == 0) {
3149 error = EBADF;
3150 goto out;
3151 }
3152
3153 flags = SCARG(uap, flags);
3154 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3155 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3156 error = EINVAL;
3157 goto out;
3158 }
3159 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3160 if (flags & FDATASYNC)
3161 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3162 else
3163 nflags = FSYNC_WAIT;
3164 if (flags & FDISKSYNC)
3165 nflags |= FSYNC_CACHE;
3166
3167 len = SCARG(uap, length);
3168 /* If length == 0, we do the whole file, and s = l = 0 will do that */
3169 if (len) {
3170 s = SCARG(uap, start);
3171 e = s + len;
3172 if (e < s) {
3173 error = EINVAL;
3174 goto out;
3175 }
3176 } else {
3177 e = 0;
3178 s = 0;
3179 }
3180
3181 vp = (struct vnode *)fp->f_data;
3182 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3183 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e, l);
3184
3185 if (error == 0 && bioops != NULL &&
3186 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3187 (*bioops->io_fsync)(vp, nflags);
3188
3189 VOP_UNLOCK(vp, 0);
3190 out:
3191 FILE_UNUSE(fp, l);
3192 return (error);
3193 }
3194
3195 /*
3196 * Sync the data of an open file.
3197 */
3198 /* ARGSUSED */
3199 int
3200 sys_fdatasync(struct lwp *l, void *v, register_t *retval)
3201 {
3202 struct sys_fdatasync_args /* {
3203 syscallarg(int) fd;
3204 } */ *uap = v;
3205 struct proc *p = l->l_proc;
3206 struct vnode *vp;
3207 struct file *fp;
3208 int error;
3209
3210 /* getvnode() will use the descriptor for us */
3211 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3212 return (error);
3213 if ((fp->f_flag & FWRITE) == 0) {
3214 FILE_UNUSE(fp, l);
3215 return (EBADF);
3216 }
3217 vp = (struct vnode *)fp->f_data;
3218 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3219 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, l);
3220 VOP_UNLOCK(vp, 0);
3221 FILE_UNUSE(fp, l);
3222 return (error);
3223 }
3224
3225 /*
3226 * Rename files, (standard) BSD semantics frontend.
3227 */
3228 /* ARGSUSED */
3229 int
3230 sys_rename(struct lwp *l, void *v, register_t *retval)
3231 {
3232 struct sys_rename_args /* {
3233 syscallarg(const char *) from;
3234 syscallarg(const char *) to;
3235 } */ *uap = v;
3236
3237 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 0));
3238 }
3239
3240 /*
3241 * Rename files, POSIX semantics frontend.
3242 */
3243 /* ARGSUSED */
3244 int
3245 sys___posix_rename(struct lwp *l, void *v, register_t *retval)
3246 {
3247 struct sys___posix_rename_args /* {
3248 syscallarg(const char *) from;
3249 syscallarg(const char *) to;
3250 } */ *uap = v;
3251
3252 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 1));
3253 }
3254
3255 /*
3256 * Rename files. Source and destination must either both be directories,
3257 * or both not be directories. If target is a directory, it must be empty.
3258 * If `from' and `to' refer to the same object, the value of the `retain'
3259 * argument is used to determine whether `from' will be
3260 *
3261 * (retain == 0) deleted unless `from' and `to' refer to the same
3262 * object in the file system's name space (BSD).
3263 * (retain == 1) always retained (POSIX).
3264 */
3265 static int
3266 rename_files(const char *from, const char *to, struct lwp *l, int retain)
3267 {
3268 struct vnode *tvp, *fvp, *tdvp;
3269 struct nameidata fromnd, tond;
3270 struct proc *p;
3271 int error;
3272
3273 NDINIT(&fromnd, DELETE, LOCKPARENT | SAVESTART | TRYEMULROOT, UIO_USERSPACE,
3274 from, l);
3275 if ((error = namei(&fromnd)) != 0)
3276 return (error);
3277 if (fromnd.ni_dvp != fromnd.ni_vp)
3278 VOP_UNLOCK(fromnd.ni_dvp, 0);
3279 fvp = fromnd.ni_vp;
3280 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | TRYEMULROOT |
3281 (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, l);
3282 if ((error = namei(&tond)) != 0) {
3283 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3284 vrele(fromnd.ni_dvp);
3285 vrele(fvp);
3286 goto out1;
3287 }
3288 tdvp = tond.ni_dvp;
3289 tvp = tond.ni_vp;
3290
3291 if (tvp != NULL) {
3292 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3293 error = ENOTDIR;
3294 goto out;
3295 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3296 error = EISDIR;
3297 goto out;
3298 }
3299 }
3300
3301 if (fvp == tdvp)
3302 error = EINVAL;
3303
3304 /*
3305 * Source and destination refer to the same object.
3306 */
3307 if (fvp == tvp) {
3308 if (retain)
3309 error = -1;
3310 else if (fromnd.ni_dvp == tdvp &&
3311 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3312 !memcmp(fromnd.ni_cnd.cn_nameptr,
3313 tond.ni_cnd.cn_nameptr,
3314 fromnd.ni_cnd.cn_namelen))
3315 error = -1;
3316 }
3317
3318 #if NVERIEXEC > 0
3319 if (!error) {
3320 char *f1, *f2;
3321
3322 f1 = malloc(fromnd.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3323 strlcpy(f1, fromnd.ni_cnd.cn_nameptr, fromnd.ni_cnd.cn_namelen);
3324
3325 f2 = malloc(tond.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3326 strlcpy(f2, tond.ni_cnd.cn_nameptr, tond.ni_cnd.cn_namelen);
3327
3328 error = veriexec_renamechk(l, fvp, f1, tvp, f2);
3329
3330 free(f1, M_TEMP);
3331 free(f2, M_TEMP);
3332 }
3333 #endif /* NVERIEXEC > 0 */
3334
3335 out:
3336 p = l->l_proc;
3337 if (!error) {
3338 VOP_LEASE(tdvp, l, l->l_cred, LEASE_WRITE);
3339 if (fromnd.ni_dvp != tdvp)
3340 VOP_LEASE(fromnd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3341 if (tvp) {
3342 VOP_LEASE(tvp, l, l->l_cred, LEASE_WRITE);
3343 }
3344 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3345 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3346 } else {
3347 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3348 if (tdvp == tvp)
3349 vrele(tdvp);
3350 else
3351 vput(tdvp);
3352 if (tvp)
3353 vput(tvp);
3354 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3355 vrele(fromnd.ni_dvp);
3356 vrele(fvp);
3357 }
3358 vrele(tond.ni_startdir);
3359 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
3360 out1:
3361 if (fromnd.ni_startdir)
3362 vrele(fromnd.ni_startdir);
3363 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3364 return (error == -1 ? 0 : error);
3365 }
3366
3367 /*
3368 * Make a directory file.
3369 */
3370 /* ARGSUSED */
3371 int
3372 sys_mkdir(struct lwp *l, void *v, register_t *retval)
3373 {
3374 struct sys_mkdir_args /* {
3375 syscallarg(const char *) path;
3376 syscallarg(int) mode;
3377 } */ *uap = v;
3378 struct proc *p = l->l_proc;
3379 struct vnode *vp;
3380 struct vattr vattr;
3381 int error;
3382 struct nameidata nd;
3383
3384 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, UIO_USERSPACE,
3385 SCARG(uap, path), l);
3386 if ((error = namei(&nd)) != 0)
3387 return (error);
3388 vp = nd.ni_vp;
3389 if (vp != NULL) {
3390 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3391 if (nd.ni_dvp == vp)
3392 vrele(nd.ni_dvp);
3393 else
3394 vput(nd.ni_dvp);
3395 vrele(vp);
3396 return (EEXIST);
3397 }
3398 VATTR_NULL(&vattr);
3399 vattr.va_type = VDIR;
3400 /* We will read cwdi->cwdi_cmask unlocked. */
3401 vattr.va_mode =
3402 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3403 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3404 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3405 if (!error)
3406 vput(nd.ni_vp);
3407 return (error);
3408 }
3409
3410 /*
3411 * Remove a directory file.
3412 */
3413 /* ARGSUSED */
3414 int
3415 sys_rmdir(struct lwp *l, void *v, register_t *retval)
3416 {
3417 struct sys_rmdir_args /* {
3418 syscallarg(const char *) path;
3419 } */ *uap = v;
3420 struct vnode *vp;
3421 int error;
3422 struct nameidata nd;
3423
3424 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
3425 SCARG(uap, path), l);
3426 if ((error = namei(&nd)) != 0)
3427 return (error);
3428 vp = nd.ni_vp;
3429 if (vp->v_type != VDIR) {
3430 error = ENOTDIR;
3431 goto out;
3432 }
3433 /*
3434 * No rmdir "." please.
3435 */
3436 if (nd.ni_dvp == vp) {
3437 error = EINVAL;
3438 goto out;
3439 }
3440 /*
3441 * The root of a mounted filesystem cannot be deleted.
3442 */
3443 if (vp->v_vflag & VV_ROOT) {
3444 error = EBUSY;
3445 goto out;
3446 }
3447 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3448 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3449 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3450 return (error);
3451
3452 out:
3453 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3454 if (nd.ni_dvp == vp)
3455 vrele(nd.ni_dvp);
3456 else
3457 vput(nd.ni_dvp);
3458 vput(vp);
3459 return (error);
3460 }
3461
3462 /*
3463 * Read a block of directory entries in a file system independent format.
3464 */
3465 int
3466 sys___getdents30(struct lwp *l, void *v, register_t *retval)
3467 {
3468 struct sys___getdents30_args /* {
3469 syscallarg(int) fd;
3470 syscallarg(char *) buf;
3471 syscallarg(size_t) count;
3472 } */ *uap = v;
3473 struct proc *p = l->l_proc;
3474 struct file *fp;
3475 int error, done;
3476
3477 /* getvnode() will use the descriptor for us */
3478 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3479 return (error);
3480 if ((fp->f_flag & FREAD) == 0) {
3481 error = EBADF;
3482 goto out;
3483 }
3484 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3485 SCARG(uap, count), &done, l, 0, 0);
3486 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
3487 *retval = done;
3488 out:
3489 FILE_UNUSE(fp, l);
3490 return (error);
3491 }
3492
3493 /*
3494 * Set the mode mask for creation of filesystem nodes.
3495 */
3496 int
3497 sys_umask(struct lwp *l, void *v, register_t *retval)
3498 {
3499 struct sys_umask_args /* {
3500 syscallarg(mode_t) newmask;
3501 } */ *uap = v;
3502 struct proc *p = l->l_proc;
3503 struct cwdinfo *cwdi;
3504
3505 /*
3506 * cwdi->cwdi_cmask will be read unlocked elsewhere. What's
3507 * important is that we serialize changes to the mask. The
3508 * rw_exit() will issue a write memory barrier on our behalf,
3509 * and force the changes out to other CPUs (as it must use an
3510 * atomic operation, draining the local CPU's store buffers).
3511 */
3512 cwdi = p->p_cwdi;
3513 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
3514 *retval = cwdi->cwdi_cmask;
3515 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3516 rw_exit(&cwdi->cwdi_lock);
3517
3518 return (0);
3519 }
3520
3521 /*
3522 * Void all references to file by ripping underlying filesystem
3523 * away from vnode.
3524 */
3525 /* ARGSUSED */
3526 int
3527 sys_revoke(struct lwp *l, void *v, register_t *retval)
3528 {
3529 struct sys_revoke_args /* {
3530 syscallarg(const char *) path;
3531 } */ *uap = v;
3532 struct vnode *vp;
3533 struct vattr vattr;
3534 int error;
3535 bool revoke;
3536 struct nameidata nd;
3537
3538 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
3539 if ((error = namei(&nd)) != 0)
3540 return (error);
3541 vp = nd.ni_vp;
3542 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
3543 goto out;
3544 if (kauth_cred_geteuid(l->l_cred) != vattr.va_uid &&
3545 (error = kauth_authorize_generic(l->l_cred,
3546 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
3547 goto out;
3548 mutex_enter(&vp->v_interlock);
3549 revoke = (vp->v_usecount > 1 || (vp->v_iflag & (VI_ALIASED | VI_LAYER)));
3550 mutex_exit(&vp->v_interlock);
3551 if (revoke)
3552 VOP_REVOKE(vp, REVOKEALL);
3553 out:
3554 vrele(vp);
3555 return (error);
3556 }
3557
3558 /*
3559 * Convert a user file descriptor to a kernel file entry.
3560 */
3561 int
3562 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3563 {
3564 struct vnode *vp;
3565 struct file *fp;
3566
3567 if ((fp = fd_getfile(fdp, fd)) == NULL)
3568 return (EBADF);
3569
3570 FILE_USE(fp);
3571
3572 if (fp->f_type != DTYPE_VNODE) {
3573 FILE_UNUSE(fp, NULL);
3574 return (EINVAL);
3575 }
3576
3577 vp = (struct vnode *)fp->f_data;
3578 if (vp->v_type == VBAD) {
3579 FILE_UNUSE(fp, NULL);
3580 return (EBADF);
3581 }
3582
3583 *fpp = fp;
3584 return (0);
3585 }
3586