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