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