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