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