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