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