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