Augment the LWKT thread creation APIs to allow a cpu to be specified. This
[dragonfly.git] / sys / kern / vfs_syscalls.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.21 2003/10/21 01:05:09 daver Exp $
41 */
42
43/* For 4.3 integer FS ID compatibility */
44#include "opt_compat.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/buf.h>
49#include <sys/sysent.h>
50#include <sys/malloc.h>
51#include <sys/mount.h>
52#include <sys/sysproto.h>
53#include <sys/filedesc.h>
54#include <sys/kernel.h>
55#include <sys/fcntl.h>
56#include <sys/file.h>
57#include <sys/linker.h>
58#include <sys/stat.h>
59#include <sys/unistd.h>
60#include <sys/vnode.h>
61#include <sys/proc.h>
62#include <sys/namei.h>
63#include <sys/dirent.h>
64#include <sys/extattr.h>
65#include <sys/kern_syscall.h>
66
67#include <machine/limits.h>
68#include <vfs/union/union.h>
69#include <sys/sysctl.h>
70#include <vm/vm.h>
71#include <vm/vm_object.h>
72#include <vm/vm_zone.h>
73#include <vm/vm_page.h>
74
75#include <sys/file2.h>
76
77static int change_dir (struct nameidata *ndp, struct thread *td);
78static void checkdirs (struct vnode *olddp);
79static int chroot_refuse_vdir_fds (struct filedesc *fdp);
80static int getutimes (const struct timeval *, struct timespec *);
81static int setfown (struct vnode *, uid_t, gid_t);
82static int setfmode (struct vnode *, int);
83static int setfflags (struct vnode *, int);
84static int setutimes (struct vnode *, const struct timespec *, int);
85static int usermount = 0; /* if 1, non-root can mount fs. */
86
87int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
88
89SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
90
91/*
92 * Virtual File System System Calls
93 */
94
95/*
96 * Mount a file system.
97 */
98/*
99 * mount_args(char *type, char *path, int flags, caddr_t data)
100 */
101/* ARGSUSED */
102int
103mount(struct mount_args *uap)
104{
105 struct thread *td = curthread;
106 struct proc *p = td->td_proc;
107 struct vnode *vp;
108 struct mount *mp;
109 struct vfsconf *vfsp;
110 int error, flag = 0, flag2 = 0;
111 struct vattr va;
112#ifdef COMPAT_43
113 u_long fstypenum;
114#endif
115 struct nameidata nd;
116 char fstypename[MFSNAMELEN];
117
118 if (usermount == 0 && (error = suser(td)))
119 return (error);
120 /*
121 * Do not allow NFS export by non-root users.
122 */
123 if (SCARG(uap, flags) & MNT_EXPORTED) {
124 error = suser(td);
125 if (error)
126 return (error);
127 }
128 /*
129 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
130 */
131 if (suser(td))
132 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
133 /*
134 * Get vnode to be covered
135 */
136 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
137 SCARG(uap, path), td);
138 if ((error = namei(&nd)) != 0)
139 return (error);
140 NDFREE(&nd, NDF_ONLY_PNBUF);
141 vp = nd.ni_vp;
142 if (SCARG(uap, flags) & MNT_UPDATE) {
143 if ((vp->v_flag & VROOT) == 0) {
144 vput(vp);
145 return (EINVAL);
146 }
147 mp = vp->v_mount;
148 flag = mp->mnt_flag;
149 flag2 = mp->mnt_kern_flag;
150 /*
151 * We only allow the filesystem to be reloaded if it
152 * is currently mounted read-only.
153 */
154 if ((SCARG(uap, flags) & MNT_RELOAD) &&
155 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
156 vput(vp);
157 return (EOPNOTSUPP); /* Needs translation */
158 }
159 /*
160 * Only root, or the user that did the original mount is
161 * permitted to update it.
162 */
163 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
164 (error = suser(td))) {
165 vput(vp);
166 return (error);
167 }
168 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
169 vput(vp);
170 return (EBUSY);
171 }
172 lwkt_gettoken(&vp->v_interlock);
173 if ((vp->v_flag & VMOUNT) != 0 ||
174 vp->v_mountedhere != NULL) {
175 lwkt_reltoken(&vp->v_interlock);
176 vfs_unbusy(mp, td);
177 vput(vp);
178 return (EBUSY);
179 }
180 vp->v_flag |= VMOUNT;
181 lwkt_reltoken(&vp->v_interlock);
182 mp->mnt_flag |=
183 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
184 VOP_UNLOCK(vp, 0, td);
185 goto update;
186 }
187 /*
188 * If the user is not root, ensure that they own the directory
189 * onto which we are attempting to mount.
190 */
191 if ((error = VOP_GETATTR(vp, &va, td)) ||
192 (va.va_uid != p->p_ucred->cr_uid &&
193 (error = suser(td)))) {
194 vput(vp);
195 return (error);
196 }
197 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
198 vput(vp);
199 return (error);
200 }
201 if (vp->v_type != VDIR) {
202 vput(vp);
203 return (ENOTDIR);
204 }
205#ifdef COMPAT_43
206 /*
207 * Historically filesystem types were identified by number. If we
208 * get an integer for the filesystem type instead of a string, we
209 * check to see if it matches one of the historic filesystem types.
210 */
211 fstypenum = (uintptr_t)SCARG(uap, type);
212 if (fstypenum < maxvfsconf) {
213 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
214 if (vfsp->vfc_typenum == fstypenum)
215 break;
216 if (vfsp == NULL) {
217 vput(vp);
218 return (ENODEV);
219 }
220 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
221 } else
222#endif /* COMPAT_43 */
223 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
224 vput(vp);
225 return (error);
226 }
227 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
228 if (!strcmp(vfsp->vfc_name, fstypename))
229 break;
230 if (vfsp == NULL) {
231 linker_file_t lf;
232
233 /* Only load modules for root (very important!) */
234 if ((error = suser(td)) != 0) {
235 vput(vp);
236 return error;
237 }
238 error = linker_load_file(fstypename, &lf);
239 if (error || lf == NULL) {
240 vput(vp);
241 if (lf == NULL)
242 error = ENODEV;
243 return error;
244 }
245 lf->userrefs++;
246 /* lookup again, see if the VFS was loaded */
247 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
248 if (!strcmp(vfsp->vfc_name, fstypename))
249 break;
250 if (vfsp == NULL) {
251 lf->userrefs--;
252 linker_file_unload(lf);
253 vput(vp);
254 return (ENODEV);
255 }
256 }
257 lwkt_gettoken(&vp->v_interlock);
258 if ((vp->v_flag & VMOUNT) != 0 ||
259 vp->v_mountedhere != NULL) {
260 lwkt_reltoken(&vp->v_interlock);
261 vput(vp);
262 return (EBUSY);
263 }
264 vp->v_flag |= VMOUNT;
265 lwkt_reltoken(&vp->v_interlock);
266
267 /*
268 * Allocate and initialize the filesystem.
269 */
270 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
271 bzero((char *)mp, (u_long)sizeof(struct mount));
272 TAILQ_INIT(&mp->mnt_nvnodelist);
273 TAILQ_INIT(&mp->mnt_reservedvnlist);
274 mp->mnt_nvnodelistsize = 0;
275 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
276 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
277 mp->mnt_op = vfsp->vfc_vfsops;
278 mp->mnt_vfc = vfsp;
279 vfsp->vfc_refcount++;
280 mp->mnt_stat.f_type = vfsp->vfc_typenum;
281 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
282 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
283 mp->mnt_vnodecovered = vp;
284 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
285 mp->mnt_iosize_max = DFLTPHYS;
286 VOP_UNLOCK(vp, 0, td);
287update:
288 /*
289 * Set the mount level flags.
290 */
291 if (SCARG(uap, flags) & MNT_RDONLY)
292 mp->mnt_flag |= MNT_RDONLY;
293 else if (mp->mnt_flag & MNT_RDONLY)
294 mp->mnt_kern_flag |= MNTK_WANTRDWR;
295 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
296 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
297 MNT_NOSYMFOLLOW | MNT_IGNORE |
298 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
299 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
300 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
301 MNT_NOSYMFOLLOW | MNT_IGNORE |
302 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
303 /*
304 * Mount the filesystem.
305 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
306 * get. No freeing of cn_pnbuf.
307 */
308 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
309 if (mp->mnt_flag & MNT_UPDATE) {
310 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
311 mp->mnt_flag &= ~MNT_RDONLY;
312 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
313 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
314 if (error) {
315 mp->mnt_flag = flag;
316 mp->mnt_kern_flag = flag2;
317 }
318 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
319 if (mp->mnt_syncer == NULL)
320 error = vfs_allocate_syncvnode(mp);
321 } else {
322 if (mp->mnt_syncer != NULL)
323 vrele(mp->mnt_syncer);
324 mp->mnt_syncer = NULL;
325 }
326 vfs_unbusy(mp, td);
327 lwkt_gettoken(&vp->v_interlock);
328 vp->v_flag &= ~VMOUNT;
329 lwkt_reltoken(&vp->v_interlock);
330 vrele(vp);
331 return (error);
332 }
333 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
334 /*
335 * Put the new filesystem on the mount list after root.
336 */
337 cache_purge(vp);
338 if (!error) {
339 lwkt_gettoken(&vp->v_interlock);
340 vp->v_flag &= ~VMOUNT;
341 vp->v_mountedhere = mp;
342 lwkt_reltoken(&vp->v_interlock);
343 lwkt_gettoken(&mountlist_token);
344 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
345 lwkt_reltoken(&mountlist_token);
346 checkdirs(vp);
347 VOP_UNLOCK(vp, 0, td);
348 if ((mp->mnt_flag & MNT_RDONLY) == 0)
349 error = vfs_allocate_syncvnode(mp);
350 vfs_unbusy(mp, td);
351 if ((error = VFS_START(mp, 0, td)) != 0)
352 vrele(vp);
353 } else {
354 lwkt_gettoken(&vp->v_interlock);
355 vp->v_flag &= ~VMOUNT;
356 lwkt_reltoken(&vp->v_interlock);
357 mp->mnt_vfc->vfc_refcount--;
358 vfs_unbusy(mp, td);
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 */
370static void
371checkdirs(struct vnode *olddp)
372{
373 struct filedesc *fdp;
374 struct vnode *newdp;
375 struct proc *p;
376
377 if (olddp->v_usecount == 1)
378 return;
379 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
380 panic("mount: lost mount");
381 FOREACH_PROC_IN_SYSTEM(p) {
382 fdp = p->p_fd;
383 if (fdp->fd_cdir == olddp) {
384 vrele(fdp->fd_cdir);
385 VREF(newdp);
386 fdp->fd_cdir = newdp;
387 }
388 if (fdp->fd_rdir == olddp) {
389 vrele(fdp->fd_rdir);
390 VREF(newdp);
391 fdp->fd_rdir = newdp;
392 }
393 }
394 if (rootvnode == olddp) {
395 vrele(rootvnode);
396 VREF(newdp);
397 rootvnode = newdp;
398 vfs_cache_setroot(rootvnode);
399 }
400 vput(newdp);
401}
402
403/*
404 * Unmount a file system.
405 *
406 * Note: unmount takes a path to the vnode mounted on as argument,
407 * not special file (as before).
408 */
409/*
410 * umount_args(char *path, int flags)
411 */
412/* ARGSUSED */
413int
414unmount(struct unmount_args *uap)
415{
416 struct thread *td = curthread;
417 struct proc *p = td->td_proc;
418 struct vnode *vp;
419 struct mount *mp;
420 int error;
421 struct nameidata nd;
422
423 KKASSERT(p);
424 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
425 SCARG(uap, path), td);
426 if ((error = namei(&nd)) != 0)
427 return (error);
428 vp = nd.ni_vp;
429 NDFREE(&nd, NDF_ONLY_PNBUF);
430 mp = vp->v_mount;
431
432 /*
433 * Only root, or the user that did the original mount is
434 * permitted to unmount this filesystem.
435 */
436 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
437 (error = suser(td))) {
438 vput(vp);
439 return (error);
440 }
441
442 /*
443 * Don't allow unmounting the root file system.
444 */
445 if (mp->mnt_flag & MNT_ROOTFS) {
446 vput(vp);
447 return (EINVAL);
448 }
449
450 /*
451 * Must be the root of the filesystem
452 */
453 if ((vp->v_flag & VROOT) == 0) {
454 vput(vp);
455 return (EINVAL);
456 }
457 vput(vp);
458 return (dounmount(mp, SCARG(uap, flags), td));
459}
460
461/*
462 * Do the actual file system unmount.
463 */
464int
465dounmount(struct mount *mp, int flags, struct thread *td)
466{
467 struct vnode *coveredvp;
468 int error;
469 int async_flag;
470
471 lwkt_gettoken(&mountlist_token);
472 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
473 lwkt_reltoken(&mountlist_token);
474 return (EBUSY);
475 }
476 mp->mnt_kern_flag |= MNTK_UNMOUNT;
477 /* Allow filesystems to detect that a forced unmount is in progress. */
478 if (flags & MNT_FORCE)
479 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
480 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
481 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_token, td);
482 if (error) {
483 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
484 if (mp->mnt_kern_flag & MNTK_MWAIT)
485 wakeup((caddr_t)mp);
486 return (error);
487 }
488
489 if (mp->mnt_flag & MNT_EXPUBLIC)
490 vfs_setpublicfs(NULL, NULL, NULL);
491
492 vfs_msync(mp, MNT_WAIT);
493 async_flag = mp->mnt_flag & MNT_ASYNC;
494 mp->mnt_flag &=~ MNT_ASYNC;
495 cache_purgevfs(mp); /* remove cache entries for this file sys */
496 if (mp->mnt_syncer != NULL)
497 vrele(mp->mnt_syncer);
498 if (((mp->mnt_flag & MNT_RDONLY) ||
499 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
500 (flags & MNT_FORCE))
501 error = VFS_UNMOUNT(mp, flags, td);
502 lwkt_gettoken(&mountlist_token);
503 if (error) {
504 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
505 (void) vfs_allocate_syncvnode(mp);
506 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
507 mp->mnt_flag |= async_flag;
508 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
509 &mountlist_token, td);
510 if (mp->mnt_kern_flag & MNTK_MWAIT)
511 wakeup((caddr_t)mp);
512 return (error);
513 }
514 TAILQ_REMOVE(&mountlist, mp, mnt_list);
515 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
516 coveredvp->v_mountedhere = NULL;
517 vrele(coveredvp);
518 }
519 mp->mnt_vfc->vfc_refcount--;
520 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
521 panic("unmount: dangling vnode");
522 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_token, td);
523 if (mp->mnt_kern_flag & MNTK_MWAIT)
524 wakeup((caddr_t)mp);
525 free((caddr_t)mp, M_MOUNT);
526 return (0);
527}
528
529/*
530 * Sync each mounted filesystem.
531 */
532
533#ifdef DEBUG
534static int syncprt = 0;
535SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
536#endif
537
538/* ARGSUSED */
539int
540sync(struct sync_args *uap)
541{
542 struct thread *td = curthread;
543 struct mount *mp, *nmp;
544 int asyncflag;
545
546 lwkt_gettoken(&mountlist_token);
547 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
548 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
549 nmp = TAILQ_NEXT(mp, mnt_list);
550 continue;
551 }
552 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
553 asyncflag = mp->mnt_flag & MNT_ASYNC;
554 mp->mnt_flag &= ~MNT_ASYNC;
555 vfs_msync(mp, MNT_NOWAIT);
556 VFS_SYNC(mp, MNT_NOWAIT, td);
557 mp->mnt_flag |= asyncflag;
558 }
559 lwkt_gettoken(&mountlist_token);
560 nmp = TAILQ_NEXT(mp, mnt_list);
561 vfs_unbusy(mp, td);
562 }
563 lwkt_reltoken(&mountlist_token);
564#if 0
565/*
566 * XXX don't call vfs_bufstats() yet because that routine
567 * was not imported in the Lite2 merge.
568 */
569#ifdef DIAGNOSTIC
570 if (syncprt)
571 vfs_bufstats();
572#endif /* DIAGNOSTIC */
573#endif
574 return (0);
575}
576
577/* XXX PRISON: could be per prison flag */
578static int prison_quotas;
579#if 0
580SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
581#endif
582
583/*
584 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
585 *
586 * Change filesystem quotas.
587 */
588/* ARGSUSED */
589int
590quotactl(struct quotactl_args *uap)
591{
592 struct thread *td = curthread;
593 struct proc *p = td->td_proc;
594 struct mount *mp;
595 int error;
596 struct nameidata nd;
597
598 KKASSERT(p);
599 if (p->p_ucred->cr_prison && !prison_quotas)
600 return (EPERM);
601 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
602 SCARG(uap, path), td);
603 if ((error = namei(&nd)) != 0)
604 return (error);
605 mp = nd.ni_vp->v_mount;
606 NDFREE(&nd, NDF_ONLY_PNBUF);
607 vrele(nd.ni_vp);
608 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
609 SCARG(uap, arg), td));
610}
611
612/*
613 * statfs_args(char *path, struct statfs *buf)
614 *
615 * Get filesystem statistics.
616 */
617/* ARGSUSED */
618int
619statfs(struct statfs_args *uap)
620{
621 struct thread *td = curthread;
622 struct mount *mp;
623 struct statfs *sp;
624 int error;
625 struct nameidata nd;
626 struct statfs sb;
627
628 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
629 SCARG(uap, path), td);
630 if ((error = namei(&nd)) != 0)
631 return (error);
632 mp = nd.ni_vp->v_mount;
633 sp = &mp->mnt_stat;
634 NDFREE(&nd, NDF_ONLY_PNBUF);
635 vrele(nd.ni_vp);
636 error = VFS_STATFS(mp, sp, td);
637 if (error)
638 return (error);
639 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
640 if (suser(td)) {
641 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
642 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
643 sp = &sb;
644 }
645 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
646}
647
648/*
649 * fstatfs_args(int fd, struct statfs *buf)
650 *
651 * Get filesystem statistics.
652 */
653/* ARGSUSED */
654int
655fstatfs(struct fstatfs_args *uap)
656{
657 struct thread *td = curthread;
658 struct proc *p = td->td_proc;
659 struct file *fp;
660 struct mount *mp;
661 struct statfs *sp;
662 int error;
663 struct statfs sb;
664
665 KKASSERT(p);
666 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
667 return (error);
668 mp = ((struct vnode *)fp->f_data)->v_mount;
669 if (mp == NULL)
670 return (EBADF);
671 sp = &mp->mnt_stat;
672 error = VFS_STATFS(mp, sp, td);
673 if (error)
674 return (error);
675 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
676 if (suser(td)) {
677 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
678 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
679 sp = &sb;
680 }
681 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
682}
683
684/*
685 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
686 *
687 * Get statistics on all filesystems.
688 */
689/* ARGSUSED */
690int
691getfsstat(struct getfsstat_args *uap)
692{
693 struct thread *td = curthread;
694 struct mount *mp, *nmp;
695 struct statfs *sp;
696 caddr_t sfsp;
697 long count, maxcount, error;
698
699 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
700 sfsp = (caddr_t)SCARG(uap, buf);
701 count = 0;
702 lwkt_gettoken(&mountlist_token);
703 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
704 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
705 nmp = TAILQ_NEXT(mp, mnt_list);
706 continue;
707 }
708 if (sfsp && count < maxcount) {
709 sp = &mp->mnt_stat;
710 /*
711 * If MNT_NOWAIT or MNT_LAZY is specified, do not
712 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
713 * overrides MNT_WAIT.
714 */
715 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
716 (SCARG(uap, flags) & MNT_WAIT)) &&
717 (error = VFS_STATFS(mp, sp, td))) {
718 lwkt_gettoken(&mountlist_token);
719 nmp = TAILQ_NEXT(mp, mnt_list);
720 vfs_unbusy(mp, td);
721 continue;
722 }
723 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
724 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
725 if (error) {
726 vfs_unbusy(mp, td);
727 return (error);
728 }
729 sfsp += sizeof(*sp);
730 }
731 count++;
732 lwkt_gettoken(&mountlist_token);
733 nmp = TAILQ_NEXT(mp, mnt_list);
734 vfs_unbusy(mp, td);
735 }
736 lwkt_reltoken(&mountlist_token);
737 if (sfsp && count > maxcount)
738 uap->sysmsg_result = maxcount;
739 else
740 uap->sysmsg_result = count;
741 return (0);
742}
743
744/*
745 * fchdir_args(int fd)
746 *
747 * Change current working directory to a given file descriptor.
748 */
749/* ARGSUSED */
750int
751fchdir(struct fchdir_args *uap)
752{
753 struct thread *td = curthread;
754 struct proc *p = td->td_proc;
755 struct filedesc *fdp = p->p_fd;
756 struct vnode *vp, *tdp;
757 struct mount *mp;
758 struct file *fp;
759 int error;
760
761 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
762 return (error);
763 vp = (struct vnode *)fp->f_data;
764 VREF(vp);
765 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
766 if (vp->v_type != VDIR)
767 error = ENOTDIR;
768 else
769 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
770 while (!error && (mp = vp->v_mountedhere) != NULL) {
771 if (vfs_busy(mp, 0, 0, td))
772 continue;
773 error = VFS_ROOT(mp, &tdp);
774 vfs_unbusy(mp, td);
775 if (error)
776 break;
777 vput(vp);
778 vp = tdp;
779 }
780 if (error) {
781 vput(vp);
782 return (error);
783 }
784 VOP_UNLOCK(vp, 0, td);
785 vrele(fdp->fd_cdir);
786 fdp->fd_cdir = vp;
787 return (0);
788}
789
790/*
791 * chdir_args(char *path)
792 *
793 * Change current working directory (``.'').
794 */
795/* ARGSUSED */
796int
797chdir(struct chdir_args *uap)
798{
799 struct thread *td = curthread;
800 struct proc *p = td->td_proc;
801 struct filedesc *fdp = p->p_fd;
802 int error;
803 struct nameidata nd;
804
805 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
806 SCARG(uap, path), td);
807 if ((error = change_dir(&nd, td)) != 0)
808 return (error);
809 NDFREE(&nd, NDF_ONLY_PNBUF);
810 vrele(fdp->fd_cdir);
811 fdp->fd_cdir = nd.ni_vp;
812 return (0);
813}
814
815/*
816 * Helper function for raised chroot(2) security function: Refuse if
817 * any filedescriptors are open directories.
818 */
819static int
820chroot_refuse_vdir_fds(fdp)
821 struct filedesc *fdp;
822{
823 struct vnode *vp;
824 struct file *fp;
825 int error;
826 int fd;
827
828 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
829 error = getvnode(fdp, fd, &fp);
830 if (error)
831 continue;
832 vp = (struct vnode *)fp->f_data;
833 if (vp->v_type != VDIR)
834 continue;
835 return(EPERM);
836 }
837 return (0);
838}
839
840/*
841 * This sysctl determines if we will allow a process to chroot(2) if it
842 * has a directory open:
843 * 0: disallowed for all processes.
844 * 1: allowed for processes that were not already chroot(2)'ed.
845 * 2: allowed for all processes.
846 */
847
848static int chroot_allow_open_directories = 1;
849
850SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
851 &chroot_allow_open_directories, 0, "");
852
853/*
854 * chroot_args(char *path)
855 *
856 * Change notion of root (``/'') directory.
857 */
858/* ARGSUSED */
859int
860chroot(struct chroot_args *uap)
861{
862 struct thread *td = curthread;
863 struct proc *p = td->td_proc;
864 struct filedesc *fdp = p->p_fd;
865 int error;
866 struct nameidata nd;
867
868 KKASSERT(p);
869 error = suser_cred(p->p_ucred, PRISON_ROOT);
870 if (error)
871 return (error);
872 if (chroot_allow_open_directories == 0 ||
873 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
874 error = chroot_refuse_vdir_fds(fdp);
875 if (error)
876 return (error);
877 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
878 SCARG(uap, path), td);
879 if ((error = change_dir(&nd, td)) != 0)
880 return (error);
881 NDFREE(&nd, NDF_ONLY_PNBUF);
882 vrele(fdp->fd_rdir);
883 fdp->fd_rdir = nd.ni_vp;
884 if (!fdp->fd_jdir) {
885 fdp->fd_jdir = nd.ni_vp;
886 VREF(fdp->fd_jdir);
887 }
888 return (0);
889}
890
891/*
892 * Common routine for chroot and chdir.
893 */
894static int
895change_dir(struct nameidata *ndp, struct thread *td)
896{
897 struct vnode *vp;
898 int error;
899
900 error = namei(ndp);
901 if (error)
902 return (error);
903 vp = ndp->ni_vp;
904 if (vp->v_type != VDIR)
905 error = ENOTDIR;
906 else
907 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
908 if (error)
909 vput(vp);
910 else
911 VOP_UNLOCK(vp, 0, td);
912 return (error);
913}
914
915/*
916 * open_args(char *path, int flags, int mode)
917 *
918 * Check permissions, allocate an open file structure,
919 * and call the device open routine if any.
920 */
921int
922open(struct open_args *uap)
923{
924 struct thread *td = curthread;
925 struct proc *p = td->td_proc;
926 struct filedesc *fdp = p->p_fd;
927 struct file *fp;
928 struct vnode *vp;
929 int cmode, flags, oflags;
930 struct file *nfp;
931 int type, indx, error;
932 struct flock lf;
933 struct nameidata nd;
934
935 oflags = SCARG(uap, flags);
936 if ((oflags & O_ACCMODE) == O_ACCMODE)
937 return (EINVAL);
938 flags = FFLAGS(oflags);
939 error = falloc(p, &nfp, &indx);
940 if (error)
941 return (error);
942 fp = nfp;
943 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
944 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
945 SCARG(uap, path), td);
946 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
947 /*
948 * Bump the ref count to prevent another process from closing
949 * the descriptor while we are blocked in vn_open()
950 */
951 fhold(fp);
952 error = vn_open(&nd, flags, cmode);
953 if (error) {
954 /*
955 * release our own reference
956 */
957 fdrop(fp, td);
958
959 /*
960 * handle special fdopen() case. bleh. dupfdopen() is
961 * responsible for dropping the old contents of ofiles[indx]
962 * if it succeeds.
963 */
964 if ((error == ENODEV || error == ENXIO) &&
965 p->p_dupfd >= 0 && /* XXX from fdopen */
966 (error =
967 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
968 uap->sysmsg_result = indx;
969 return (0);
970 }
971 /*
972 * Clean up the descriptor, but only if another thread hadn't
973 * replaced or closed it.
974 */
975 if (fdp->fd_ofiles[indx] == fp) {
976 fdp->fd_ofiles[indx] = NULL;
977 fdrop(fp, td);
978 }
979
980 if (error == ERESTART)
981 error = EINTR;
982 return (error);
983 }
984 p->p_dupfd = 0;
985 NDFREE(&nd, NDF_ONLY_PNBUF);
986 vp = nd.ni_vp;
987
988 /*
989 * There should be 2 references on the file, one from the descriptor
990 * table, and one for us.
991 *
992 * Handle the case where someone closed the file (via its file
993 * descriptor) while we were blocked. The end result should look
994 * like opening the file succeeded but it was immediately closed.
995 */
996 if (fp->f_count == 1) {
997 KASSERT(fdp->fd_ofiles[indx] != fp,
998 ("Open file descriptor lost all refs"));
999 VOP_UNLOCK(vp, 0, td);
1000 vn_close(vp, flags & FMASK, td);
1001 fdrop(fp, td);
1002 uap->sysmsg_result = indx;
1003 return 0;
1004 }
1005
1006 fp->f_data = (caddr_t)vp;
1007 fp->f_flag = flags & FMASK;
1008 fp->f_ops = &vnops;
1009 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1010 if (flags & (O_EXLOCK | O_SHLOCK)) {
1011 lf.l_whence = SEEK_SET;
1012 lf.l_start = 0;
1013 lf.l_len = 0;
1014 if (flags & O_EXLOCK)
1015 lf.l_type = F_WRLCK;
1016 else
1017 lf.l_type = F_RDLCK;
1018 type = F_FLOCK;
1019 if ((flags & FNONBLOCK) == 0)
1020 type |= F_WAIT;
1021 VOP_UNLOCK(vp, 0, td);
1022 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1023 /*
1024 * lock request failed. Normally close the descriptor
1025 * but handle the case where someone might have dup()d
1026 * it when we weren't looking. One reference is
1027 * owned by the descriptor array, the other by us.
1028 */
1029 if (fdp->fd_ofiles[indx] == fp) {
1030 fdp->fd_ofiles[indx] = NULL;
1031 fdrop(fp, td);
1032 }
1033 fdrop(fp, td);
1034 return (error);
1035 }
1036 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1037 fp->f_flag |= FHASLOCK;
1038 }
1039 /* assert that vn_open created a backing object if one is needed */
1040 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1041 ("open: vmio vnode has no backing object after vn_open"));
1042 VOP_UNLOCK(vp, 0, td);
1043
1044 /*
1045 * release our private reference, leaving the one associated with the
1046 * descriptor table intact.
1047 */
1048 fdrop(fp, td);
1049 uap->sysmsg_result = indx;
1050 return (0);
1051}
1052
1053#ifdef COMPAT_43
1054/*
1055 * ocreat(char *path, int mode)
1056 *
1057 * Create a file.
1058 */
1059int
1060ocreat(struct ocreat_args *uap)
1061{
1062 struct open_args /* {
1063 syscallarg(char *) path;
1064 syscallarg(int) flags;
1065 syscallarg(int) mode;
1066 } */ nuap;
1067
1068 SCARG(&nuap, path) = SCARG(uap, path);
1069 SCARG(&nuap, mode) = SCARG(uap, mode);
1070 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1071 return (open(&nuap));
1072}
1073#endif /* COMPAT_43 */
1074
1075/*
1076 * mknod_args(char *path, int mode, int dev)
1077 *
1078 * Create a special file.
1079 */
1080/* ARGSUSED */
1081int
1082mknod(struct mknod_args *uap)
1083{
1084 struct thread *td = curthread;
1085 struct proc *p = td->td_proc;
1086 struct vnode *vp;
1087 struct vattr vattr;
1088 int error;
1089 int whiteout = 0;
1090 struct nameidata nd;
1091
1092 KKASSERT(p);
1093
1094 switch (SCARG(uap, mode) & S_IFMT) {
1095 case S_IFCHR:
1096 case S_IFBLK:
1097 error = suser(td);
1098 break;
1099 default:
1100 error = suser_cred(p->p_ucred, PRISON_ROOT);
1101 break;
1102 }
1103 if (error)
1104 return (error);
1105 bwillwrite();
1106 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1107 SCARG(uap, path), td);
1108 if ((error = namei(&nd)) != 0)
1109 return (error);
1110 vp = nd.ni_vp;
1111 if (vp != NULL)
1112 error = EEXIST;
1113 else {
1114 VATTR_NULL(&vattr);
1115 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1116 vattr.va_rdev = SCARG(uap, dev);
1117 whiteout = 0;
1118
1119 switch (SCARG(uap, mode) & S_IFMT) {
1120 case S_IFMT: /* used by badsect to flag bad sectors */
1121 vattr.va_type = VBAD;
1122 break;
1123 case S_IFCHR:
1124 vattr.va_type = VCHR;
1125 break;
1126 case S_IFBLK:
1127 vattr.va_type = VBLK;
1128 break;
1129 case S_IFWHT:
1130 whiteout = 1;
1131 break;
1132 default:
1133 error = EINVAL;
1134 break;
1135 }
1136 }
1137 if (!error) {
1138 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1139 if (whiteout)
1140 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_CREATE);
1141 else {
1142 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1143 &nd.ni_cnd, &vattr);
1144 if (error == 0)
1145 vput(nd.ni_vp);
1146 }
1147 NDFREE(&nd, NDF_ONLY_PNBUF);
1148 vput(nd.ni_dvp);
1149 } else {
1150 NDFREE(&nd, NDF_ONLY_PNBUF);
1151 if (nd.ni_dvp == vp)
1152 vrele(nd.ni_dvp);
1153 else
1154 vput(nd.ni_dvp);
1155 if (vp)
1156 vrele(vp);
1157 }
1158 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1159 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1160 return (error);
1161}
1162
1163/*
1164 * mkfifo_args(char *path, int mode)
1165 *
1166 * Create a named pipe.
1167 */
1168/* ARGSUSED */
1169int
1170mkfifo(struct mkfifo_args *uap)
1171{
1172 struct thread *td = curthread;
1173 struct proc *p = td->td_proc;
1174 struct vattr vattr;
1175 int error;
1176 struct nameidata nd;
1177
1178 bwillwrite();
1179 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1180 SCARG(uap, path), td);
1181 if ((error = namei(&nd)) != 0)
1182 return (error);
1183 if (nd.ni_vp != NULL) {
1184 NDFREE(&nd, NDF_ONLY_PNBUF);
1185 if (nd.ni_dvp == nd.ni_vp)
1186 vrele(nd.ni_dvp);
1187 else
1188 vput(nd.ni_dvp);
1189 vrele(nd.ni_vp);
1190 return (EEXIST);
1191 }
1192 VATTR_NULL(&vattr);
1193 vattr.va_type = VFIFO;
1194 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1195 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1196 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr);
1197 if (error == 0)
1198 vput(nd.ni_vp);
1199 NDFREE(&nd, NDF_ONLY_PNBUF);
1200 vput(nd.ni_dvp);
1201 return (error);
1202}
1203
1204/*
1205 * link_args(char *path, char *link)
1206 *
1207 * Make a hard file link.
1208 */
1209/* ARGSUSED */
1210int
1211link(struct link_args *uap)
1212{
1213 struct thread *td = curthread;
1214 struct proc *p = td->td_proc;
1215 struct vnode *vp;
1216 struct nameidata nd;
1217 int error;
1218
1219 bwillwrite();
1220 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1221 SCARG(uap, path), td);
1222 if ((error = namei(&nd)) != 0)
1223 return (error);
1224 NDFREE(&nd, NDF_ONLY_PNBUF);
1225 vp = nd.ni_vp;
1226 if (vp->v_type == VDIR)
1227 error = EPERM; /* POSIX */
1228 else {
1229 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1230 UIO_USERSPACE, SCARG(uap, link), td);
1231 error = namei(&nd);
1232 if (!error) {
1233 if (nd.ni_vp != NULL) {
1234 if (nd.ni_vp)
1235 vrele(nd.ni_vp);
1236 error = EEXIST;
1237 } else {
1238 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
1239 LEASE_WRITE);
1240 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1241 error = VOP_LINK(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
1242 }
1243 NDFREE(&nd, NDF_ONLY_PNBUF);
1244 if (nd.ni_dvp == nd.ni_vp)
1245 vrele(nd.ni_dvp);
1246 else
1247 vput(nd.ni_dvp);
1248 }
1249 }
1250 vrele(vp);
1251 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1252 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1253 return (error);
1254}
1255
1256/*
1257 * symlink(char *path, char *link)
1258 *
1259 * Make a symbolic link.
1260 */
1261/* ARGSUSED */
1262int
1263symlink(struct symlink_args *uap)
1264{
1265 struct thread *td = curthread;
1266 struct proc *p = td->td_proc;
1267 struct vattr vattr;
1268 char *path;
1269 int error;
1270 struct nameidata nd;
1271
1272 path = zalloc(namei_zone);
1273 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1274 goto out;
1275 bwillwrite();
1276 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ, UIO_USERSPACE,
1277 SCARG(uap, link), td);
1278 if ((error = namei(&nd)) != 0)
1279 goto out;
1280 if (nd.ni_vp) {
1281 NDFREE(&nd, NDF_ONLY_PNBUF);
1282 if (nd.ni_dvp == nd.ni_vp)
1283 vrele(nd.ni_dvp);
1284 else
1285 vput(nd.ni_dvp);
1286 vrele(nd.ni_vp);
1287 error = EEXIST;
1288 goto out;
1289 }
1290 VATTR_NULL(&vattr);
1291 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1292 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1293 error = VOP_SYMLINK(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1294 NDFREE(&nd, NDF_ONLY_PNBUF);
1295 if (error == 0)
1296 vput(nd.ni_vp);
1297 vput(nd.ni_dvp);
1298 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1299 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1300out:
1301 zfree(namei_zone, path);
1302 return (error);
1303}
1304
1305/*
1306 * undelete_args(char *path)
1307 *
1308 * Delete a whiteout from the filesystem.
1309 */
1310/* ARGSUSED */
1311int
1312undelete(struct undelete_args *uap)
1313{
1314 struct thread *td = curthread;
1315 struct proc *p = td->td_proc;
1316 int error;
1317 struct nameidata nd;
1318
1319 bwillwrite();
1320 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1321 SCARG(uap, path), td);
1322 error = namei(&nd);
1323 if (error)
1324 return (error);
1325
1326 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1327 NDFREE(&nd, NDF_ONLY_PNBUF);
1328 if (nd.ni_dvp == nd.ni_vp)
1329 vrele(nd.ni_dvp);
1330 else
1331 vput(nd.ni_dvp);
1332 if (nd.ni_vp)
1333 vrele(nd.ni_vp);
1334 return (EEXIST);
1335 }
1336
1337 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1338 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1339 NDFREE(&nd, NDF_ONLY_PNBUF);
1340 vput(nd.ni_dvp);
1341 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1342 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1343 return (error);
1344}
1345
1346/*
1347 * unlink_args(char *path)
1348 *
1349 * Delete a name from the filesystem.
1350 */
1351int
1352unlink(struct unlink_args *uap)
1353{
1354 struct thread *td = curthread;
1355 struct proc *p = td->td_proc;
1356 struct vnode *vp;
1357 int error;
1358 struct nameidata nd;
1359
1360 bwillwrite();
1361 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1362 if ((error = namei(&nd)) != 0)
1363 return (error);
1364 vp = nd.ni_vp;
1365 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1366 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1367
1368 if (vp->v_type == VDIR)
1369 error = EPERM; /* POSIX */
1370 else {
1371 /*
1372 * The root of a mounted filesystem cannot be deleted.
1373 *
1374 * XXX: can this only be a VDIR case?
1375 */
1376 if (vp->v_flag & VROOT)
1377 error = EBUSY;
1378 }
1379
1380 if (!error) {
1381 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1382 error = VOP_REMOVE(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
1383 }
1384 NDFREE(&nd, NDF_ONLY_PNBUF);
1385 if (nd.ni_dvp == vp)
1386 vrele(nd.ni_dvp);
1387 else
1388 vput(nd.ni_dvp);
1389 if (vp != NULLVP)
1390 vput(vp);
1391 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1392 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1393 return (error);
1394}
1395
1396/*
1397 * lseek_args(int fd, int pad, off_t offset, int whence)
1398 *
1399 * Reposition read/write file offset.
1400 */
1401int
1402lseek(struct lseek_args *uap)
1403{
1404 struct thread *td = curthread;
1405 struct proc *p = td->td_proc;
1406 struct filedesc *fdp = p->p_fd;
1407 struct file *fp;
1408 struct vattr vattr;
1409 int error;
1410
1411 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1412 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1413 return (EBADF);
1414 if (fp->f_type != DTYPE_VNODE)
1415 return (ESPIPE);
1416 switch (SCARG(uap, whence)) {
1417 case L_INCR:
1418 fp->f_offset += SCARG(uap, offset);
1419 break;
1420 case L_XTND:
1421 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1422 if (error)
1423 return (error);
1424 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1425 break;
1426 case L_SET:
1427 fp->f_offset = SCARG(uap, offset);
1428 break;
1429 default:
1430 return (EINVAL);
1431 }
1432 uap->sysmsg_offset = fp->f_offset;
1433 return (0);
1434}
1435
1436#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1437/*
1438 * Reposition read/write file offset.
1439 *
1440 * olseek_args(int fd, long offset, int whence)
1441 */
1442int
1443olseek(struct olseek_args *uap)
1444{
1445 struct lseek_args /* {
1446 syscallarg(int) fd;
1447 syscallarg(int) pad;
1448 syscallarg(off_t) offset;
1449 syscallarg(int) whence;
1450 } */ nuap;
1451 int error;
1452
1453 SCARG(&nuap, fd) = SCARG(uap, fd);
1454 SCARG(&nuap, offset) = SCARG(uap, offset);
1455 SCARG(&nuap, whence) = SCARG(uap, whence);
1456 error = lseek(&nuap);
1457 return (error);
1458}
1459#endif /* COMPAT_43 */
1460
1461/*
1462 * access_args(char *path, int flags)
1463 *
1464 * Check access permissions.
1465 */
1466int
1467access(struct access_args *uap)
1468{
1469 struct thread *td = curthread;
1470 struct proc *p = td->td_proc;
1471 struct ucred *cred, *tmpcred;
1472 struct vnode *vp;
1473 int error, flags;
1474 struct nameidata nd;
1475
1476 cred = p->p_ucred;
1477 /*
1478 * Create and modify a temporary credential instead of one that
1479 * is potentially shared. This could also mess up socket
1480 * buffer accounting which can run in an interrupt context.
1481 */
1482 tmpcred = crdup(cred);
1483 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1484 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1485 p->p_ucred = tmpcred;
1486 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1487 UIO_USERSPACE, SCARG(uap, path), td);
1488 if ((error = namei(&nd)) != 0)
1489 goto out1;
1490 vp = nd.ni_vp;
1491
1492 /* Flags == 0 means only check for existence. */
1493 if (SCARG(uap, flags)) {
1494 flags = 0;
1495 if (SCARG(uap, flags) & R_OK)
1496 flags |= VREAD;
1497 if (SCARG(uap, flags) & W_OK)
1498 flags |= VWRITE;
1499 if (SCARG(uap, flags) & X_OK)
1500 flags |= VEXEC;
1501 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1502 error = VOP_ACCESS(vp, flags, tmpcred, td);
1503 }
1504 NDFREE(&nd, NDF_ONLY_PNBUF);
1505 vput(vp);
1506out1:
1507 p->p_ucred = cred;
1508 crfree(tmpcred);
1509 return (error);
1510}
1511
1512#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1513/*
1514 * ostat_args(char *path, struct ostat *ub)
1515 *
1516 * Get file status; this version follows links.
1517 */
1518/* ARGSUSED */
1519int
1520ostat(struct ostat_args *uap)
1521{
1522 struct thread *td = curthread;
1523 struct stat sb;
1524 struct ostat osb;
1525 int error;
1526 struct nameidata nd;
1527
1528 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1529 UIO_USERSPACE, SCARG(uap, path), td);
1530 if ((error = namei(&nd)) != 0)
1531 return (error);
1532 NDFREE(&nd, NDF_ONLY_PNBUF);
1533 error = vn_stat(nd.ni_vp, &sb, td);
1534 vput(nd.ni_vp);
1535 if (error)
1536 return (error);
1537 cvtstat(&sb, &osb);
1538 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1539 return (error);
1540}
1541
1542/*
1543 * olstat_args(char *path, struct ostat *ub)
1544 *
1545 * Get file status; this version does not follow links.
1546 */
1547/* ARGSUSED */
1548int
1549olstat(struct olstat_args *uap)
1550{
1551 struct thread *td = curthread;
1552 struct vnode *vp;
1553 struct stat sb;
1554 struct ostat osb;
1555 int error;
1556 struct nameidata nd;
1557
1558 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1559 UIO_USERSPACE, SCARG(uap, path), td);
1560 if ((error = namei(&nd)) != 0)
1561 return (error);
1562 vp = nd.ni_vp;
1563 error = vn_stat(vp, &sb, td);
1564 NDFREE(&nd, NDF_ONLY_PNBUF);
1565 vput(vp);
1566 if (error)
1567 return (error);
1568 cvtstat(&sb, &osb);
1569 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1570 return (error);
1571}
1572
1573/*
1574 * Convert from an old to a new stat structure.
1575 */
1576void
1577cvtstat(st, ost)
1578 struct stat *st;
1579 struct ostat *ost;
1580{
1581 ost->st_dev = st->st_dev;
1582 ost->st_ino = st->st_ino;
1583 ost->st_mode = st->st_mode;
1584 ost->st_nlink = st->st_nlink;
1585 ost->st_uid = st->st_uid;
1586 ost->st_gid = st->st_gid;
1587 ost->st_rdev = st->st_rdev;
1588 if (st->st_size < (quad_t)1 << 32)
1589 ost->st_size = st->st_size;
1590 else
1591 ost->st_size = -2;
1592 ost->st_atime = st->st_atime;
1593 ost->st_mtime = st->st_mtime;
1594 ost->st_ctime = st->st_ctime;
1595 ost->st_blksize = st->st_blksize;
1596 ost->st_blocks = st->st_blocks;
1597 ost->st_flags = st->st_flags;
1598 ost->st_gen = st->st_gen;
1599}
1600#endif /* COMPAT_43 || COMPAT_SUNOS */
1601
1602/*
1603 * stat_args(char *path, struct stat *ub)
1604 *
1605 * Get file status; this version follows links.
1606 */
1607/* ARGSUSED */
1608int
1609stat(struct stat_args *uap)
1610{
1611 struct thread *td = curthread;
1612 struct stat sb;
1613 int error;
1614 struct nameidata nd;
1615
1616 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1617 UIO_USERSPACE, SCARG(uap, path), td);
1618 if ((error = namei(&nd)) != 0)
1619 return (error);
1620 error = vn_stat(nd.ni_vp, &sb, td);
1621 NDFREE(&nd, NDF_ONLY_PNBUF);
1622 vput(nd.ni_vp);
1623 if (error)
1624 return (error);
1625 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1626 return (error);
1627}
1628
1629/*
1630 * lstat_args(char *path, struct stat *ub)
1631 *
1632 * Get file status; this version does not follow links.
1633 */
1634/* ARGSUSED */
1635int
1636lstat(struct lstat_args *uap)
1637{
1638 struct thread *td = curthread;
1639 int error;
1640 struct vnode *vp;
1641 struct stat sb;
1642 struct nameidata nd;
1643
1644 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1645 UIO_USERSPACE, SCARG(uap, path), td);
1646 if ((error = namei(&nd)) != 0)
1647 return (error);
1648 vp = nd.ni_vp;
1649 error = vn_stat(vp, &sb, td);
1650 NDFREE(&nd, NDF_ONLY_PNBUF);
1651 vput(vp);
1652 if (error)
1653 return (error);
1654 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1655 return (error);
1656}
1657
1658void
1659cvtnstat(sb, nsb)
1660 struct stat *sb;
1661 struct nstat *nsb;
1662{
1663 nsb->st_dev = sb->st_dev;
1664 nsb->st_ino = sb->st_ino;
1665 nsb->st_mode = sb->st_mode;
1666 nsb->st_nlink = sb->st_nlink;
1667 nsb->st_uid = sb->st_uid;
1668 nsb->st_gid = sb->st_gid;
1669 nsb->st_rdev = sb->st_rdev;
1670 nsb->st_atimespec = sb->st_atimespec;
1671 nsb->st_mtimespec = sb->st_mtimespec;
1672 nsb->st_ctimespec = sb->st_ctimespec;
1673 nsb->st_size = sb->st_size;
1674 nsb->st_blocks = sb->st_blocks;
1675 nsb->st_blksize = sb->st_blksize;
1676 nsb->st_flags = sb->st_flags;
1677 nsb->st_gen = sb->st_gen;
1678 nsb->st_qspare[0] = sb->st_qspare[0];
1679 nsb->st_qspare[1] = sb->st_qspare[1];
1680}
1681
1682/*
1683 * nstat_args(char *path, struct nstat *ub)
1684 */
1685/* ARGSUSED */
1686int
1687nstat(struct nstat_args *uap)
1688{
1689 struct thread *td = curthread;
1690 struct stat sb;
1691 struct nstat nsb;
1692 int error;
1693 struct nameidata nd;
1694
1695 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1696 UIO_USERSPACE, SCARG(uap, path), td);
1697 if ((error = namei(&nd)) != 0)
1698 return (error);
1699 NDFREE(&nd, NDF_ONLY_PNBUF);
1700 error = vn_stat(nd.ni_vp, &sb, td);
1701 vput(nd.ni_vp);
1702 if (error)
1703 return (error);
1704 cvtnstat(&sb, &nsb);
1705 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1706 return (error);
1707}
1708
1709/*
1710 * lstat_args(char *path, struct stat *ub)
1711 *
1712 * Get file status; this version does not follow links.
1713 */
1714/* ARGSUSED */
1715int
1716nlstat(struct nlstat_args *uap)
1717{
1718 struct thread *td = curthread;
1719 int error;
1720 struct vnode *vp;
1721 struct stat sb;
1722 struct nstat nsb;
1723 struct nameidata nd;
1724
1725 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1726 UIO_USERSPACE, SCARG(uap, path), td);
1727 if ((error = namei(&nd)) != 0)
1728 return (error);
1729 vp = nd.ni_vp;
1730 NDFREE(&nd, NDF_ONLY_PNBUF);
1731 error = vn_stat(vp, &sb, td);
1732 vput(vp);
1733 if (error)
1734 return (error);
1735 cvtnstat(&sb, &nsb);
1736 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1737 return (error);
1738}
1739
1740/*
1741 * pathconf_Args(char *path, int name)
1742 *
1743 * Get configurable pathname variables.
1744 */
1745/* ARGSUSED */
1746int
1747pathconf(struct pathconf_args *uap)
1748{
1749 struct thread *td = curthread;
1750 int error;
1751 struct nameidata nd;
1752
1753 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1754 UIO_USERSPACE, SCARG(uap, path), td);
1755 if ((error = namei(&nd)) != 0)
1756 return (error);
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1759 vput(nd.ni_vp);
1760 return (error);
1761}
1762
1763/*
1764 * readlink_args(char *path, char *buf, int count)
1765 *
1766 * Return target name of a symbolic link.
1767 */
1768/* ARGSUSED */
1769int
1770readlink(struct readlink_args *uap)
1771{
1772 struct thread *td = curthread;
1773 struct proc *p = td->td_proc;
1774 struct vnode *vp;
1775 struct iovec aiov;
1776 struct uio auio;
1777 int error;
1778 struct nameidata nd;
1779
1780 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1781 UIO_USERSPACE, SCARG(uap, path), td);
1782 if ((error = namei(&nd)) != 0)
1783 return (error);
1784 NDFREE(&nd, NDF_ONLY_PNBUF);
1785 vp = nd.ni_vp;
1786 if (vp->v_type != VLNK)
1787 error = EINVAL;
1788 else {
1789 aiov.iov_base = SCARG(uap, buf);
1790 aiov.iov_len = SCARG(uap, count);
1791 auio.uio_iov = &aiov;
1792 auio.uio_iovcnt = 1;
1793 auio.uio_offset = 0;
1794 auio.uio_rw = UIO_READ;
1795 auio.uio_segflg = UIO_USERSPACE;
1796 auio.uio_td = td;
1797 auio.uio_resid = SCARG(uap, count);
1798 error = VOP_READLINK(vp, &auio, p->p_ucred);
1799 }
1800 vput(vp);
1801 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
1802 return (error);
1803}
1804
1805static int
1806setfflags(struct vnode *vp, int flags)
1807{
1808 struct thread *td = curthread;
1809 struct proc *p = td->td_proc;
1810 int error;
1811 struct vattr vattr;
1812
1813 /*
1814 * Prevent non-root users from setting flags on devices. When
1815 * a device is reused, users can retain ownership of the device
1816 * if they are allowed to set flags and programs assume that
1817 * chown can't fail when done as root.
1818 */
1819 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1820 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1821 return (error);
1822
1823 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1824 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1825 VATTR_NULL(&vattr);
1826 vattr.va_flags = flags;
1827 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1828 VOP_UNLOCK(vp, 0, td);
1829 return (error);
1830}
1831
1832/*
1833 * chflags(char *path, int flags)
1834 *
1835 * Change flags of a file given a path name.
1836 */
1837/* ARGSUSED */
1838int
1839chflags(struct chflags_args *uap)
1840{
1841 struct thread *td = curthread;
1842 int error;
1843 struct nameidata nd;
1844
1845 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1846 SCARG(uap, path), td);
1847 if ((error = namei(&nd)) != 0)
1848 return (error);
1849 NDFREE(&nd, NDF_ONLY_PNBUF);
1850 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1851 vrele(nd.ni_vp);
1852 return error;
1853}
1854
1855/*
1856 * fchflags_args(int fd, int flags)
1857 *
1858 * Change flags of a file given a file descriptor.
1859 */
1860/* ARGSUSED */
1861int
1862fchflags(struct fchflags_args *uap)
1863{
1864 struct thread *td = curthread;
1865 struct proc *p = td->td_proc;
1866 struct file *fp;
1867 int error;
1868
1869 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1870 return (error);
1871 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1872}
1873
1874static int
1875setfmode(struct vnode *vp, int mode)
1876{
1877 struct thread *td = curthread;
1878 struct proc *p = td->td_proc;
1879 int error;
1880 struct vattr vattr;
1881
1882 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1883 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1884 VATTR_NULL(&vattr);
1885 vattr.va_mode = mode & ALLPERMS;
1886 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1887 VOP_UNLOCK(vp, 0, td);
1888 return error;
1889}
1890
1891/*
1892 * chmod_args(char *path, int mode)
1893 *
1894 * Change mode of a file given path name.
1895 */
1896/* ARGSUSED */
1897int
1898chmod(struct chmod_args *uap)
1899{
1900 struct thread *td = curthread;
1901 int error;
1902 struct nameidata nd;
1903
1904 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1905 SCARG(uap, path), td);
1906 if ((error = namei(&nd)) != 0)
1907 return (error);
1908 NDFREE(&nd, NDF_ONLY_PNBUF);
1909 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1910 vrele(nd.ni_vp);
1911 return error;
1912}
1913
1914/*
1915 * lchmod_args(char *path, int mode)
1916 *
1917 * Change mode of a file given path name (don't follow links.)
1918 */
1919/* ARGSUSED */
1920int
1921lchmod(struct lchmod_args *uap)
1922{
1923 struct thread *td = curthread;
1924 int error;
1925 struct nameidata nd;
1926
1927 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1928 if ((error = namei(&nd)) != 0)
1929 return (error);
1930 NDFREE(&nd, NDF_ONLY_PNBUF);
1931 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1932 vrele(nd.ni_vp);
1933 return error;
1934}
1935
1936/*
1937 * fchmod_args(int fd, int mode)
1938 *
1939 * Change mode of a file given a file descriptor.
1940 */
1941/* ARGSUSED */
1942int
1943fchmod(struct fchmod_args *uap)
1944{
1945 struct thread *td = curthread;
1946 struct proc *p = td->td_proc;
1947 struct file *fp;
1948 int error;
1949
1950 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1951 return (error);
1952 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1953}
1954
1955static int
1956setfown(struct vnode *vp, uid_t uid, gid_t gid)
1957{
1958 struct thread *td = curthread;
1959 struct proc *p = td->td_proc;
1960 int error;
1961 struct vattr vattr;
1962
1963 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1964 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1965 VATTR_NULL(&vattr);
1966 vattr.va_uid = uid;
1967 vattr.va_gid = gid;
1968 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1969 VOP_UNLOCK(vp, 0, td);
1970 return error;
1971}
1972
1973/*
1974 * chown(char *path, int uid, int gid)
1975 *
1976 * Set ownership given a path name.
1977 */
1978/* ARGSUSED */
1979int
1980chown(struct chown_args *uap)
1981{
1982 struct thread *td = curthread;
1983 int error;
1984 struct nameidata nd;
1985
1986 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1987 SCARG(uap, path), td);
1988 if ((error = namei(&nd)) != 0)
1989 return (error);
1990 NDFREE(&nd, NDF_ONLY_PNBUF);
1991 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
1992 vrele(nd.ni_vp);
1993 return (error);
1994}
1995
1996/*
1997 * lchown_args(char *path, int uid, int gid)
1998 *
1999 * Set ownership given a path name, do not cross symlinks.
2000 */
2001/* ARGSUSED */
2002int
2003lchown(struct lchown_args *uap)
2004{
2005 struct thread *td = curthread;
2006 int error;
2007 struct nameidata nd;
2008
2009 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
2010 if ((error = namei(&nd)) != 0)
2011 return (error);
2012 NDFREE(&nd, NDF_ONLY_PNBUF);
2013 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2014 vrele(nd.ni_vp);
2015 return (error);
2016}
2017
2018/*
2019 * fchown_args(int fd, int uid, int gid)
2020 *
2021 * Set ownership given a file descriptor.
2022 */
2023/* ARGSUSED */
2024int
2025fchown(struct fchown_args *uap)
2026{
2027 struct thread *td = curthread;
2028 struct proc *p = td->td_proc;
2029 struct file *fp;
2030 int error;
2031
2032 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2033 return (error);
2034 return setfown((struct vnode *)fp->f_data,
2035 SCARG(uap, uid), SCARG(uap, gid));
2036}
2037
2038static int
2039getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2040{
2041 struct timeval tv[2];
2042 int error;
2043
2044 if (usrtvp == NULL) {
2045 microtime(&tv[0]);
2046 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2047 tsp[1] = tsp[0];
2048 } else {
2049 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2050 return (error);
2051 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2052 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2053 }
2054 return 0;
2055}
2056
2057static int
2058setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2059{
2060 struct thread *td = curthread;
2061 struct proc *p = td->td_proc;
2062 int error;
2063 struct vattr vattr;
2064
2065 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2066 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2067 VATTR_NULL(&vattr);
2068 vattr.va_atime = ts[0];
2069 vattr.va_mtime = ts[1];
2070 if (nullflag)
2071 vattr.va_vaflags |= VA_UTIMES_NULL;
2072 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2073 VOP_UNLOCK(vp, 0, td);
2074 return error;
2075}
2076
2077/*
2078 * utimes_args(char *path, struct timeval *tptr)
2079 *
2080 * Set the access and modification times of a file.
2081 */
2082/* ARGSUSED */
2083int
2084utimes(struct utimes_args *uap)
2085{
2086 struct thread *td = curthread;
2087 struct timespec ts[2];
2088 struct timeval *usrtvp;
2089 int error;
2090 struct nameidata nd;
2091
2092 usrtvp = SCARG(uap, tptr);
2093 if ((error = getutimes(usrtvp, ts)) != 0)
2094 return (error);
2095 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2096 if ((error = namei(&nd)) != 0)
2097 return (error);
2098 NDFREE(&nd, NDF_ONLY_PNBUF);
2099 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2100 vrele(nd.ni_vp);
2101 return (error);
2102}
2103
2104/*
2105 * lutimes_args(char *path, struct timeval *tptr)
2106 *
2107 * Set the access and modification times of a file.
2108 */
2109/* ARGSUSED */
2110int
2111lutimes(struct lutimes_args *uap)
2112{
2113 struct thread *td = curthread;
2114 struct timespec ts[2];
2115 struct timeval *usrtvp;
2116 int error;
2117 struct nameidata nd;
2118
2119 usrtvp = SCARG(uap, tptr);
2120 if ((error = getutimes(usrtvp, ts)) != 0)
2121 return (error);
2122 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
2123 if ((error = namei(&nd)) != 0)
2124 return (error);
2125 NDFREE(&nd, NDF_ONLY_PNBUF);
2126 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2127 vrele(nd.ni_vp);
2128 return (error);
2129}
2130
2131/*
2132 * futimes_args(int fd, struct timeval *tptr)
2133 *
2134 * Set the access and modification times of a file.
2135 */
2136/* ARGSUSED */
2137int
2138futimes(struct futimes_args *uap)
2139{
2140 struct thread *td = curthread;
2141 struct proc *p = td->td_proc;
2142 struct timespec ts[2];
2143 struct file *fp;
2144 struct timeval *usrtvp;
2145 int error;
2146
2147 usrtvp = SCARG(uap, tptr);
2148 if ((error = getutimes(usrtvp, ts)) != 0)
2149 return (error);
2150 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2151 return (error);
2152 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2153}
2154
2155/*
2156 * truncate(char *path, int pad, off_t length)
2157 *
2158 * Truncate a file given its path name.
2159 */
2160/* ARGSUSED */
2161int
2162truncate(struct truncate_args *uap)
2163{
2164 struct thread *td = curthread;
2165 struct proc *p = td->td_proc;
2166 struct vnode *vp;
2167 struct vattr vattr;
2168 int error;
2169 struct nameidata nd;
2170
2171 if (uap->length < 0)
2172 return(EINVAL);
2173 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2174 if ((error = namei(&nd)) != 0)
2175 return (error);
2176 vp = nd.ni_vp;
2177 NDFREE(&nd, NDF_ONLY_PNBUF);
2178 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2179 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2180 if (vp->v_type == VDIR)
2181 error = EISDIR;
2182 else if ((error = vn_writechk(vp)) == 0 &&
2183 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2184 VATTR_NULL(&vattr);
2185 vattr.va_size = SCARG(uap, length);
2186 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2187 }
2188 vput(vp);
2189 return (error);
2190}
2191
2192int
2193kern_ftruncate(int fd, off_t length)
2194{
2195 struct thread *td = curthread;
2196 struct proc *p = td->td_proc;
2197 struct vattr vattr;
2198 struct vnode *vp;
2199 struct file *fp;
2200 int error;
2201
2202 if (length < 0)
2203 return(EINVAL);
2204 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2205 return (error);
2206 if ((fp->f_flag & FWRITE) == 0)
2207 return (EINVAL);
2208 vp = (struct vnode *)fp->f_data;
2209 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2210 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2211 if (vp->v_type == VDIR)
2212 error = EISDIR;
2213 else if ((error = vn_writechk(vp)) == 0) {
2214 VATTR_NULL(&vattr);
2215 vattr.va_size = length;
2216 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2217 }
2218 VOP_UNLOCK(vp, 0, td);
2219 return (error);
2220}
2221
2222/*
2223 * ftruncate_args(int fd, int pad, off_t length)
2224 *
2225 * Truncate a file given a file descriptor.
2226 */
2227/* ARGSUSED */
2228int
2229ftruncate(struct ftruncate_args *uap)
2230{
2231 int error;
2232
2233 error = kern_ftruncate(uap->fd, uap->length);
2234
2235 return (error);
2236}
2237
2238#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2239/*
2240 * otruncate_args(char *path, long length)
2241 *
2242 * Truncate a file given its path name.
2243 */
2244/* ARGSUSED */
2245int
2246otruncate(struct otruncate_args *uap)
2247{
2248 struct truncate_args /* {
2249 syscallarg(char *) path;
2250 syscallarg(int) pad;
2251 syscallarg(off_t) length;
2252 } */ nuap;
2253
2254 SCARG(&nuap, path) = SCARG(uap, path);
2255 SCARG(&nuap, length) = SCARG(uap, length);
2256 return (truncate(&nuap));
2257}
2258#endif /* COMPAT_43 || COMPAT_SUNOS */
2259
2260/*
2261 * fsync(int fd)
2262 *
2263 * Sync an open file.
2264 */
2265/* ARGSUSED */
2266int
2267fsync(struct fsync_args *uap)
2268{
2269 struct thread *td = curthread;
2270 struct proc *p = td->td_proc;
2271 struct vnode *vp;
2272 struct file *fp;
2273 vm_object_t obj;
2274 int error;
2275
2276 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2277 return (error);
2278 vp = (struct vnode *)fp->f_data;
2279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2280 if (VOP_GETVOBJECT(vp, &obj) == 0)
2281 vm_object_page_clean(obj, 0, 0, 0);
2282 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2283 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2284 bioops.io_fsync)
2285 error = (*bioops.io_fsync)(vp);
2286 VOP_UNLOCK(vp, 0, td);
2287 return (error);
2288}
2289
2290/*
2291 * rename_args(char *from, char *to)
2292 *
2293 * Rename files. Source and destination must either both be directories,
2294 * or both not be directories. If target is a directory, it must be empty.
2295 */
2296/* ARGSUSED */
2297int
2298rename(struct rename_args *uap)
2299{
2300 struct thread *td = curthread;
2301 struct proc *p = td->td_proc;
2302 struct vnode *tvp, *fvp, *tdvp;
2303 struct nameidata fromnd, tond;
2304 int error;
2305
2306 bwillwrite();
2307 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2308 UIO_USERSPACE, SCARG(uap, from), td);
2309 if ((error = namei(&fromnd)) != 0)
2310 return (error);
2311 fvp = fromnd.ni_vp;
2312 NDINIT(&tond, NAMEI_RENAME,
2313 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2314 CNP_SAVESTART | CNP_NOOBJ,
2315 UIO_USERSPACE, SCARG(uap, to), td);
2316 if (fromnd.ni_vp->v_type == VDIR)
2317 tond.ni_cnd.cn_flags |= CNP_WILLBEDIR;
2318 if ((error = namei(&tond)) != 0) {
2319 /* Translate error code for rename("dir1", "dir2/."). */
2320 if (error == EISDIR && fvp->v_type == VDIR)
2321 error = EINVAL;
2322 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2323 vrele(fromnd.ni_dvp);
2324 vrele(fvp);
2325 goto out1;
2326 }
2327 tdvp = tond.ni_dvp;
2328 tvp = tond.ni_vp;
2329 if (tvp != NULL) {
2330 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2331 error = ENOTDIR;
2332 goto out;
2333 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2334 error = EISDIR;
2335 goto out;
2336 }
2337 }
2338 if (fvp == tdvp)
2339 error = EINVAL;
2340 /*
2341 * If the source is the same as the destination (that is, if they
2342 * are links to the same vnode), then there is nothing to do.
2343 */
2344 if (fvp == tvp)
2345 error = -1;
2346out:
2347 if (!error) {
2348 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2349 if (fromnd.ni_dvp != tdvp) {
2350 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2351 }
2352 if (tvp) {
2353 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2354 }
2355 error = VOP_RENAME(fromnd.ni_dvp, NCPNULL, fromnd.ni_vp, &fromnd.ni_cnd,
2356 tond.ni_dvp, NCPNULL, tond.ni_vp, &tond.ni_cnd);
2357 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2358 NDFREE(&tond, NDF_ONLY_PNBUF);
2359 } else {
2360 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2361 NDFREE(&tond, NDF_ONLY_PNBUF);
2362 if (tdvp == tvp)
2363 vrele(tdvp);
2364 else
2365 vput(tdvp);
2366 if (tvp)
2367 vput(tvp);
2368 vrele(fromnd.ni_dvp);
2369 vrele(fvp);
2370 }
2371 vrele(tond.ni_startdir);
2372 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2373 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2374 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2375 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2376out1:
2377 if (fromnd.ni_startdir)
2378 vrele(fromnd.ni_startdir);
2379 if (error == -1)
2380 return (0);
2381 return (error);
2382}
2383
2384/*
2385 * mkdir_args(char *path, int mode)
2386 *
2387 * Make a directory file.
2388 */
2389/* ARGSUSED */
2390int
2391mkdir(struct mkdir_args *uap)
2392{
2393 struct thread *td = curthread;
2394 struct proc *p = td->td_proc;
2395 struct vnode *vp;
2396 struct vattr vattr;
2397 int error;
2398 struct nameidata nd;
2399
2400 bwillwrite();
2401 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
2402 SCARG(uap, path), td);
2403 nd.ni_cnd.cn_flags |= CNP_WILLBEDIR;
2404 if ((error = namei(&nd)) != 0)
2405 return (error);
2406 vp = nd.ni_vp;
2407 if (vp != NULL) {
2408 NDFREE(&nd, NDF_ONLY_PNBUF);
2409 if (nd.ni_dvp == vp)
2410 vrele(nd.ni_dvp);
2411 else
2412 vput(nd.ni_dvp);
2413 vrele(vp);
2414 return (EEXIST);
2415 }
2416 VATTR_NULL(&vattr);
2417 vattr.va_type = VDIR;
2418 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2419 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2420 error = VOP_MKDIR(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr);
2421 NDFREE(&nd, NDF_ONLY_PNBUF);
2422 vput(nd.ni_dvp);
2423 if (!error)
2424 vput(nd.ni_vp);
2425 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2426 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2427 return (error);
2428}
2429
2430/*
2431 * rmdir_args(char *path)
2432 *
2433 * Remove a directory file.
2434 */
2435/* ARGSUSED */
2436int
2437rmdir(struct rmdir_args *uap)
2438{
2439 struct thread *td = curthread;
2440 struct proc *p = td->td_proc;
2441 struct vnode *vp;
2442 int error;
2443 struct nameidata nd;
2444
2445 bwillwrite();
2446 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF, UIO_USERSPACE,
2447 SCARG(uap, path), td);
2448 if ((error = namei(&nd)) != 0)
2449 return (error);
2450 vp = nd.ni_vp;
2451 if (vp->v_type != VDIR) {
2452 error = ENOTDIR;
2453 goto out;
2454 }
2455 /*
2456 * No rmdir "." please.
2457 */
2458 if (nd.ni_dvp == vp) {
2459 error = EINVAL;
2460 goto out;
2461 }
2462 /*
2463 * The root of a mounted filesystem cannot be deleted.
2464 */
2465 if (vp->v_flag & VROOT)
2466 error = EBUSY;
2467 else {
2468 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2469 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2470 error = VOP_RMDIR(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
2471 }
2472out:
2473 NDFREE(&nd, NDF_ONLY_PNBUF);
2474 if (nd.ni_dvp == vp)
2475 vrele(nd.ni_dvp);
2476 else
2477 vput(nd.ni_dvp);
2478 if (vp != NULLVP)
2479 vput(vp);
2480 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2481 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2482 return (error);
2483}
2484
2485#ifdef COMPAT_43
2486/*
2487 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2488 *
2489 * Read a block of directory entries in a file system independent format.
2490 */
2491int
2492ogetdirentries(struct ogetdirentries_args *uap)
2493{
2494 struct thread *td = curthread;
2495 struct proc *p = td->td_proc;
2496 struct vnode *vp;
2497 struct file *fp;
2498 struct uio auio, kuio;
2499 struct iovec aiov, kiov;
2500 struct dirent *dp, *edp;
2501 caddr_t dirbuf;
2502 int error, eofflag, readcnt;
2503 long loff;
2504
2505 /* XXX arbitrary sanity limit on `count'. */
2506 if (SCARG(uap, count) > 64 * 1024)
2507 return (EINVAL);
2508 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2509 return (error);
2510 if ((fp->f_flag & FREAD) == 0)
2511 return (EBADF);
2512 vp = (struct vnode *)fp->f_data;
2513unionread:
2514 if (vp->v_type != VDIR)
2515 return (EINVAL);
2516 aiov.iov_base = SCARG(uap, buf);
2517 aiov.iov_len = SCARG(uap, count);
2518 auio.uio_iov = &aiov;
2519 auio.uio_iovcnt = 1;
2520 auio.uio_rw = UIO_READ;
2521 auio.uio_segflg = UIO_USERSPACE;
2522 auio.uio_td = td;
2523 auio.uio_resid = SCARG(uap, count);
2524 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2525 loff = auio.uio_offset = fp->f_offset;
2526# if (BYTE_ORDER != LITTLE_ENDIAN)
2527 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2528 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2529 NULL, NULL);
2530 fp->f_offset = auio.uio_offset;
2531 } else
2532# endif
2533 {
2534 kuio = auio;
2535 kuio.uio_iov = &kiov;
2536 kuio.uio_segflg = UIO_SYSSPACE;
2537 kiov.iov_len = SCARG(uap, count);
2538 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2539 kiov.iov_base = dirbuf;
2540 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2541 NULL, NULL);
2542 fp->f_offset = kuio.uio_offset;
2543 if (error == 0) {
2544 readcnt = SCARG(uap, count) - kuio.uio_resid;
2545 edp = (struct dirent *)&dirbuf[readcnt];
2546 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2547# if (BYTE_ORDER == LITTLE_ENDIAN)
2548 /*
2549 * The expected low byte of
2550 * dp->d_namlen is our dp->d_type.
2551 * The high MBZ byte of dp->d_namlen
2552 * is our dp->d_namlen.
2553 */
2554 dp->d_type = dp->d_namlen;
2555 dp->d_namlen = 0;
2556# else
2557 /*
2558 * The dp->d_type is the high byte
2559 * of the expected dp->d_namlen,
2560 * so must be zero'ed.
2561 */
2562 dp->d_type = 0;
2563# endif
2564 if (dp->d_reclen > 0) {
2565 dp = (struct dirent *)
2566 ((char *)dp + dp->d_reclen);
2567 } else {
2568 error = EIO;
2569 break;
2570 }
2571 }
2572 if (dp >= edp)
2573 error = uiomove(dirbuf, readcnt, &auio);
2574 }
2575 FREE(dirbuf, M_TEMP);
2576 }
2577 VOP_UNLOCK(vp, 0, td);
2578 if (error)
2579 return (error);
2580 if (SCARG(uap, count) == auio.uio_resid) {
2581 if (union_dircheckp) {
2582 error = union_dircheckp(td, &vp, fp);
2583 if (error == -1)
2584 goto unionread;
2585 if (error)
2586 return (error);
2587 }
2588 if ((vp->v_flag & VROOT) &&
2589 (vp->v_mount->mnt_flag & MNT_UNION)) {
2590 struct vnode *tvp = vp;
2591 vp = vp->v_mount->mnt_vnodecovered;
2592 VREF(vp);
2593 fp->f_data = (caddr_t) vp;
2594 fp->f_offset = 0;
2595 vrele(tvp);
2596 goto unionread;
2597 }
2598 }
2599 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2600 sizeof(long));
2601 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2602 return (error);
2603}
2604#endif /* COMPAT_43 */
2605
2606/*
2607 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2608 *
2609 * Read a block of directory entries in a file system independent format.
2610 */
2611int
2612getdirentries(struct getdirentries_args *uap)
2613{
2614 struct thread *td = curthread;
2615 struct proc *p = td->td_proc;
2616 struct vnode *vp;
2617 struct file *fp;
2618 struct uio auio;
2619 struct iovec aiov;
2620 long loff;
2621 int error, eofflag;
2622
2623 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2624 return (error);
2625 if ((fp->f_flag & FREAD) == 0)
2626 return (EBADF);
2627 vp = (struct vnode *)fp->f_data;
2628unionread:
2629 if (vp->v_type != VDIR)
2630 return (EINVAL);
2631 aiov.iov_base = SCARG(uap, buf);
2632 aiov.iov_len = SCARG(uap, count);
2633 auio.uio_iov = &aiov;
2634 auio.uio_iovcnt = 1;
2635 auio.uio_rw = UIO_READ;
2636 auio.uio_segflg = UIO_USERSPACE;
2637 auio.uio_td = td;
2638 auio.uio_resid = SCARG(uap, count);
2639 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2640 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2641 loff = auio.uio_offset = fp->f_offset;
2642 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2643 fp->f_offset = auio.uio_offset;
2644 VOP_UNLOCK(vp, 0, td);
2645 if (error)
2646 return (error);
2647 if (SCARG(uap, count) == auio.uio_resid) {
2648 if (union_dircheckp) {
2649 error = union_dircheckp(td, &vp, fp);
2650 if (error == -1)
2651 goto unionread;
2652 if (error)
2653 return (error);
2654 }
2655 if ((vp->v_flag & VROOT) &&
2656 (vp->v_mount->mnt_flag & MNT_UNION)) {
2657 struct vnode *tvp = vp;
2658 vp = vp->v_mount->mnt_vnodecovered;
2659 VREF(vp);
2660 fp->f_data = (caddr_t) vp;
2661 fp->f_offset = 0;
2662 vrele(tvp);
2663 goto unionread;
2664 }
2665 }
2666 if (SCARG(uap, basep) != NULL) {
2667 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2668 sizeof(long));
2669 }
2670 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2671 return (error);
2672}
2673
2674/*
2675 * getdents_args(int fd, char *buf, size_t count)
2676 */
2677int
2678getdents(struct getdents_args *uap)
2679{
2680 struct getdirentries_args ap;
2681
2682 ap.fd = uap->fd;
2683 ap.buf = uap->buf;
2684 ap.count = uap->count;
2685 ap.basep = NULL;
2686 return getdirentries(&ap);
2687}
2688
2689/*
2690 * umask(int newmask)
2691 *
2692 * Set the mode mask for creation of filesystem nodes.
2693 *
2694 * MP SAFE
2695 */
2696int
2697umask(struct umask_args *uap)
2698{
2699 struct thread *td = curthread;
2700 struct proc *p = td->td_proc;
2701 struct filedesc *fdp;
2702
2703 fdp = p->p_fd;
2704 uap->sysmsg_result = fdp->fd_cmask;
2705 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2706 return (0);
2707}
2708
2709/*
2710 * revoke(char *path)
2711 *
2712 * Void all references to file by ripping underlying filesystem
2713 * away from vnode.
2714 */
2715/* ARGSUSED */
2716int
2717revoke(struct revoke_args *uap)
2718{
2719 struct thread *td = curthread;
2720 struct proc *p = td->td_proc;
2721 struct vnode *vp;
2722 struct vattr vattr;
2723 int error;
2724 struct nameidata nd;
2725
2726 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2727 if ((error = namei(&nd)) != 0)
2728 return (error);
2729 vp = nd.ni_vp;
2730 NDFREE(&nd, NDF_ONLY_PNBUF);
2731 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2732 error = EINVAL;
2733 goto out;
2734 }
2735 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2736 goto out;
2737 if (p->p_ucred->cr_uid != vattr.va_uid &&
2738 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2739 goto out;
2740 if (vcount(vp) > 1)
2741 VOP_REVOKE(vp, REVOKEALL);
2742out:
2743 vrele(vp);
2744 return (error);
2745}
2746
2747/*
2748 * Convert a user file descriptor to a kernel file entry.
2749 */
2750int
2751getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2752{
2753 struct file *fp;
2754
2755 if ((u_int)fd >= fdp->fd_nfiles ||
2756 (fp = fdp->fd_ofiles[fd]) == NULL)
2757 return (EBADF);
2758 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2759 return (EINVAL);
2760 *fpp = fp;
2761 return (0);
2762}
2763/*
2764 * getfh_args(char *fname, fhandle_t *fhp)
2765 *
2766 * Get (NFS) file handle
2767 */
2768int
2769getfh(struct getfh_args *uap)
2770{
2771 struct thread *td = curthread;
2772 struct nameidata nd;
2773 fhandle_t fh;
2774 struct vnode *vp;
2775 int error;
2776
2777 /*
2778 * Must be super user
2779 */
2780 error = suser(td);
2781 if (error)
2782 return (error);
2783 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2784 error = namei(&nd);
2785 if (error)
2786 return (error);
2787 NDFREE(&nd, NDF_ONLY_PNBUF);
2788 vp = nd.ni_vp;
2789 bzero(&fh, sizeof(fh));
2790 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2791 error = VFS_VPTOFH(vp, &fh.fh_fid);
2792 vput(vp);
2793 if (error)
2794 return (error);
2795 error = copyout(&fh, uap->fhp, sizeof (fh));
2796 return (error);
2797}
2798
2799/*
2800 * fhopen_args(const struct fhandle *u_fhp, int flags)
2801 *
2802 * syscall for the rpc.lockd to use to translate a NFS file handle into
2803 * an open descriptor.
2804 *
2805 * warning: do not remove the suser() call or this becomes one giant
2806 * security hole.
2807 */
2808int
2809fhopen(struct fhopen_args *uap)
2810{
2811 struct thread *td = curthread;
2812 struct proc *p = td->td_proc;
2813 struct mount *mp;
2814 struct vnode *vp;
2815 struct fhandle fhp;
2816 struct vattr vat;
2817 struct vattr *vap = &vat;
2818 struct flock lf;
2819 struct file *fp;
2820 struct filedesc *fdp = p->p_fd;
2821 int fmode, mode, error, type;
2822 struct file *nfp;
2823 int indx;
2824
2825 /*
2826 * Must be super user
2827 */
2828 error = suser(td);
2829 if (error)
2830 return (error);
2831
2832 fmode = FFLAGS(SCARG(uap, flags));
2833 /* why not allow a non-read/write open for our lockd? */
2834 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2835 return (EINVAL);
2836 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2837 if (error)
2838 return(error);
2839 /* find the mount point */
2840 mp = vfs_getvfs(&fhp.fh_fsid);
2841 if (mp == NULL)
2842 return (ESTALE);
2843 /* now give me my vnode, it gets returned to me locked */
2844 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2845 if (error)
2846 return (error);
2847 /*
2848 * from now on we have to make sure not
2849 * to forget about the vnode
2850 * any error that causes an abort must vput(vp)
2851 * just set error = err and 'goto bad;'.
2852 */
2853
2854 /*
2855 * from vn_open
2856 */
2857 if (vp->v_type == VLNK) {
2858 error = EMLINK;
2859 goto bad;
2860 }
2861 if (vp->v_type == VSOCK) {
2862 error = EOPNOTSUPP;
2863 goto bad;
2864 }
2865 mode = 0;
2866 if (fmode & (FWRITE | O_TRUNC)) {
2867 if (vp->v_type == VDIR) {
2868 error = EISDIR;
2869 goto bad;
2870 }
2871 error = vn_writechk(vp);
2872 if (error)
2873 goto bad;
2874 mode |= VWRITE;
2875 }
2876 if (fmode & FREAD)
2877 mode |= VREAD;
2878 if (mode) {
2879 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2880 if (error)
2881 goto bad;
2882 }
2883 if (fmode & O_TRUNC) {
2884 VOP_UNLOCK(vp, 0, td); /* XXX */
2885 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2886 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2887 VATTR_NULL(vap);
2888 vap->va_size = 0;
2889 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2890 if (error)
2891 goto bad;
2892 }
2893 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2894 if (error)
2895 goto bad;
2896 /*
2897 * Make sure that a VM object is created for VMIO support.
2898 */
2899 if (vn_canvmio(vp) == TRUE) {
2900 if ((error = vfs_object_create(vp, td)) != 0)
2901 goto bad;
2902 }
2903 if (fmode & FWRITE)
2904 vp->v_writecount++;
2905
2906 /*
2907 * end of vn_open code
2908 */
2909
2910 if ((error = falloc(p, &nfp, &indx)) != 0) {
2911 if (fmode & FWRITE)
2912 vp->v_writecount--;
2913 goto bad;
2914 }
2915 fp = nfp;
2916
2917 /*
2918 * hold an extra reference to avoid having fp ripped out
2919 * from under us while we block in the lock op.
2920 */
2921 fhold(fp);
2922 nfp->f_data = (caddr_t)vp;
2923 nfp->f_flag = fmode & FMASK;
2924 nfp->f_ops = &vnops;
2925 nfp->f_type = DTYPE_VNODE;
2926 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2927 lf.l_whence = SEEK_SET;
2928 lf.l_start = 0;
2929 lf.l_len = 0;
2930 if (fmode & O_EXLOCK)
2931 lf.l_type = F_WRLCK;
2932 else
2933 lf.l_type = F_RDLCK;
2934 type = F_FLOCK;
2935 if ((fmode & FNONBLOCK) == 0)
2936 type |= F_WAIT;
2937 VOP_UNLOCK(vp, 0, td);
2938 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2939 /*
2940 * lock request failed. Normally close the descriptor
2941 * but handle the case where someone might have dup()d
2942 * or close()d it when we weren't looking.
2943 */
2944 if (fdp->fd_ofiles[indx] == fp) {
2945 fdp->fd_ofiles[indx] = NULL;
2946 fdrop(fp, td);
2947 }
2948
2949 /*
2950 * release our private reference.
2951 */
2952 fdrop(fp, td);
2953 return (error);
2954 }
2955 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2956 fp->f_flag |= FHASLOCK;
2957 }
2958 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2959 vfs_object_create(vp, td);
2960
2961 VOP_UNLOCK(vp, 0, td);
2962 fdrop(fp, td);
2963 uap->sysmsg_result = indx;
2964 return (0);
2965
2966bad:
2967 vput(vp);
2968 return (error);
2969}
2970
2971/*
2972 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2973 */
2974int
2975fhstat(struct fhstat_args *uap)
2976{
2977 struct thread *td = curthread;
2978 struct stat sb;
2979 fhandle_t fh;
2980 struct mount *mp;
2981 struct vnode *vp;
2982 int error;
2983
2984 /*
2985 * Must be super user
2986 */
2987 error = suser(td);
2988 if (error)
2989 return (error);
2990
2991 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2992 if (error)
2993 return (error);
2994
2995 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2996 return (ESTALE);
2997 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2998 return (error);
2999 error = vn_stat(vp, &sb, td);
3000 vput(vp);
3001 if (error)
3002 return (error);
3003 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3004 return (error);
3005}
3006
3007/*
3008 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3009 */
3010int
3011fhstatfs(struct fhstatfs_args *uap)
3012{
3013 struct thread *td = curthread;
3014 struct statfs *sp;
3015 struct mount *mp;
3016 struct vnode *vp;
3017 struct statfs sb;
3018 fhandle_t fh;
3019 int error;
3020
3021 /*
3022 * Must be super user
3023 */
3024 if ((error = suser(td)))
3025 return (error);
3026
3027 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3028 return (error);
3029
3030 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3031 return (ESTALE);
3032 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3033 return (error);
3034 mp = vp->v_mount;
3035 sp = &mp->mnt_stat;
3036 vput(vp);
3037 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3038 return (error);
3039 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3040 if (suser(td)) {
3041 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3042 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3043 sp = &sb;
3044 }
3045 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3046}
3047
3048/*
3049 * Syscall to push extended attribute configuration information into the
3050 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3051 * a command (int cmd), and attribute name and misc data. For now, the
3052 * attribute name is left in userspace for consumption by the VFS_op.
3053 * It will probably be changed to be copied into sysspace by the
3054 * syscall in the future, once issues with various consumers of the
3055 * attribute code have raised their hands.
3056 *
3057 * Currently this is used only by UFS Extended Attributes.
3058 */
3059int
3060extattrctl(struct extattrctl_args *uap)
3061{
3062 struct thread *td = curthread;
3063 struct nameidata nd;
3064 struct mount *mp;
3065 int error;
3066
3067 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3068 if ((error = namei(&nd)) != 0)
3069 return (error);
3070 mp = nd.ni_vp->v_mount;
3071 NDFREE(&nd, 0);
3072 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3073 SCARG(uap, arg), td));
3074}
3075
3076/*
3077 * Syscall to set a named extended attribute on a file or directory.
3078 * Accepts attribute name, and a uio structure pointing to the data to set.
3079 * The uio is consumed in the style of writev(). The real work happens
3080 * in VOP_SETEXTATTR().
3081 */
3082int
3083extattr_set_file(struct extattr_set_file_args *uap)
3084{
3085 struct thread *td = curthread;
3086 struct proc *p = td->td_proc;
3087 struct nameidata nd;
3088 struct uio auio;
3089 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3090 char attrname[EXTATTR_MAXNAMELEN];
3091 u_int iovlen, cnt;
3092 int error, i;
3093
3094 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3095 if (error)
3096 return (error);
3097 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3098 SCARG(uap, path), td);
3099 if ((error = namei(&nd)) != 0)
3100 return(error);
3101 iovlen = uap->iovcnt * sizeof(struct iovec);
3102 if (uap->iovcnt > UIO_SMALLIOV) {
3103 if (uap->iovcnt > UIO_MAXIOV) {
3104 error = EINVAL;
3105 goto done;
3106 }
3107 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3108 needfree = iov;
3109 } else
3110 iov = aiov;
3111 auio.uio_iov = iov;
3112 auio.uio_iovcnt = uap->iovcnt;
3113 auio.uio_rw = UIO_WRITE;
3114 auio.uio_segflg = UIO_USERSPACE;
3115 auio.uio_td = td;
3116 auio.uio_offset = 0;
3117 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3118 goto done;
3119 auio.uio_resid = 0;
3120 for (i = 0; i < uap->iovcnt; i++) {
3121 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3122 error = EINVAL;
3123 goto done;
3124 }
3125 auio.uio_resid += iov->iov_len;
3126 iov++;
3127 }
3128 cnt = auio.uio_resid;
3129 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3130 cnt -= auio.uio_resid;
3131 uap->sysmsg_result = cnt;
3132done:
3133 if (needfree)
3134 FREE(needfree, M_IOV);
3135 NDFREE(&nd, 0);
3136 return (error);
3137}
3138
3139/*
3140 * Syscall to get a named extended attribute on a file or directory.
3141 * Accepts attribute name, and a uio structure pointing to a buffer for the
3142 * data. The uio is consumed in the style of readv(). The real work
3143 * happens in VOP_GETEXTATTR();
3144 */
3145int
3146extattr_get_file(struct extattr_get_file_args *uap)
3147{
3148 struct thread *td = curthread;
3149 struct proc *p = td->td_proc;
3150 struct nameidata nd;
3151 struct uio auio;
3152 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3153 char attrname[EXTATTR_MAXNAMELEN];
3154 u_int iovlen, cnt;
3155 int error, i;
3156
3157 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3158 if (error)
3159 return (error);
3160 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3161 SCARG(uap, path), td);
3162 if ((error = namei(&nd)) != 0)
3163 return (error);
3164 iovlen = uap->iovcnt * sizeof (struct iovec);
3165 if (uap->iovcnt > UIO_SMALLIOV) {
3166 if (uap->iovcnt > UIO_MAXIOV) {
3167 NDFREE(&nd, 0);
3168 return (EINVAL);
3169 }
3170 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3171 needfree = iov;
3172 } else {
3173 iov = aiov;
3174 needfree = NULL;
3175 }
3176 auio.uio_iov = iov;
3177 auio.uio_iovcnt = uap->iovcnt;
3178 auio.uio_rw = UIO_READ;
3179 auio.uio_segflg = UIO_USERSPACE;
3180 auio.uio_td = td;
3181 auio.uio_offset = 0;
3182 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3183 goto done;
3184 auio.uio_resid = 0;
3185 for (i = 0; i < uap->iovcnt; i++) {
3186 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3187 error = EINVAL;
3188 goto done;
3189 }
3190 auio.uio_resid += iov->iov_len;
3191 iov++;
3192 }
3193 cnt = auio.uio_resid;
3194 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3195 cnt -= auio.uio_resid;
3196 uap->sysmsg_result = cnt;
3197done:
3198 if (needfree)
3199 FREE(needfree, M_IOV);
3200 NDFREE(&nd, 0);
3201 return(error);
3202}
3203
3204/*
3205 * Syscall to delete a named extended attribute from a file or directory.
3206 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3207 */
3208int
3209extattr_delete_file(struct extattr_delete_file_args *uap)
3210{
3211 struct thread *td = curthread;
3212 struct proc *p = td->td_proc;
3213 struct nameidata nd;
3214 char attrname[EXTATTR_MAXNAMELEN];
3215 int error;
3216
3217 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3218 if (error)
3219 return(error);
3220 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3221 SCARG(uap, path), td);
3222 if ((error = namei(&nd)) != 0)
3223 return(error);
3224 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
3225 NDFREE(&nd, 0);
3226 return(error);
3227}