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