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