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