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