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