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