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