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