Augment the LWKT thread creation APIs to allow a cpu to be specified. This
[dragonfly.git] / sys / kern / vfs_syscalls.c
CommitLineData
984263bc
MD
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 $
8f6f8622 40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.21 2003/10/21 01:05:09 daver Exp $
984263bc
MD
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>
984263bc
MD
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>
dadab5e9 62#include <sys/namei.h>
984263bc
MD
63#include <sys/dirent.h>
64#include <sys/extattr.h>
8f6f8622 65#include <sys/kern_syscall.h>
984263bc
MD
66
67#include <machine/limits.h>
1f2de5d4 68#include <vfs/union/union.h>
984263bc
MD
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
dadab5e9
MD
75#include <sys/file2.h>
76
402ed7e1
RG
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);
984263bc
MD
85static int usermount = 0; /* if 1, non-root can mount fs. */
86
402ed7e1 87int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
984263bc
MD
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 */
41c20dac
MD
98/*
99 * mount_args(char *type, char *path, int flags, caddr_t data)
100 */
984263bc
MD
101/* ARGSUSED */
102int
41c20dac 103mount(struct mount_args *uap)
984263bc 104{
dadab5e9
MD
105 struct thread *td = curthread;
106 struct proc *p = td->td_proc;
984263bc
MD
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
dadab5e9 118 if (usermount == 0 && (error = suser(td)))
984263bc
MD
119 return (error);
120 /*
121 * Do not allow NFS export by non-root users.
122 */
123 if (SCARG(uap, flags) & MNT_EXPORTED) {
dadab5e9 124 error = suser(td);
984263bc
MD
125 if (error)
126 return (error);
127 }
128 /*
129 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
130 */
dadab5e9 131 if (suser(td))
984263bc
MD
132 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
133 /*
134 * Get vnode to be covered
135 */
2b69e610 136 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9 137 SCARG(uap, path), td);
984263bc
MD
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 &&
dadab5e9 164 (error = suser(td))) {
984263bc
MD
165 vput(vp);
166 return (error);
167 }
dadab5e9 168 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
984263bc
MD
169 vput(vp);
170 return (EBUSY);
171 }
8a8d5d85 172 lwkt_gettoken(&vp->v_interlock);
984263bc
MD
173 if ((vp->v_flag & VMOUNT) != 0 ||
174 vp->v_mountedhere != NULL) {
8a8d5d85 175 lwkt_reltoken(&vp->v_interlock);
dadab5e9 176 vfs_unbusy(mp, td);
984263bc
MD
177 vput(vp);
178 return (EBUSY);
179 }
180 vp->v_flag |= VMOUNT;
8a8d5d85 181 lwkt_reltoken(&vp->v_interlock);
984263bc
MD
182 mp->mnt_flag |=
183 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
dadab5e9 184 VOP_UNLOCK(vp, 0, td);
984263bc
MD
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 */
3b568787 191 if ((error = VOP_GETATTR(vp, &va, td)) ||
984263bc 192 (va.va_uid != p->p_ucred->cr_uid &&
dadab5e9 193 (error = suser(td)))) {
984263bc
MD
194 vput(vp);
195 return (error);
196 }
3b568787 197 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
984263bc
MD
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!) */
dadab5e9 234 if ((error = suser(td)) != 0) {
984263bc
MD
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 }
8a8d5d85 257 lwkt_gettoken(&vp->v_interlock);
984263bc
MD
258 if ((vp->v_flag & VMOUNT) != 0 ||
259 vp->v_mountedhere != NULL) {
8a8d5d85 260 lwkt_reltoken(&vp->v_interlock);
984263bc
MD
261 vput(vp);
262 return (EBUSY);
263 }
264 vp->v_flag |= VMOUNT;
8a8d5d85 265 lwkt_reltoken(&vp->v_interlock);
984263bc
MD
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;
377d4740 275 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
dadab5e9 276 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
984263bc
MD
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;
dadab5e9 286 VOP_UNLOCK(vp, 0, td);
984263bc
MD
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 */
dadab5e9 308 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
984263bc
MD
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 }
dadab5e9 326 vfs_unbusy(mp, td);
8a8d5d85 327 lwkt_gettoken(&vp->v_interlock);
984263bc 328 vp->v_flag &= ~VMOUNT;
8a8d5d85 329 lwkt_reltoken(&vp->v_interlock);
984263bc
MD
330 vrele(vp);
331 return (error);
332 }
dadab5e9 333 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
334 /*
335 * Put the new filesystem on the mount list after root.
336 */
337 cache_purge(vp);
338 if (!error) {
8a8d5d85 339 lwkt_gettoken(&vp->v_interlock);
984263bc
MD
340 vp->v_flag &= ~VMOUNT;
341 vp->v_mountedhere = mp;
8a8d5d85
MD
342 lwkt_reltoken(&vp->v_interlock);
343 lwkt_gettoken(&mountlist_token);
984263bc 344 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
8a8d5d85 345 lwkt_reltoken(&mountlist_token);
984263bc 346 checkdirs(vp);
dadab5e9 347 VOP_UNLOCK(vp, 0, td);
984263bc
MD
348 if ((mp->mnt_flag & MNT_RDONLY) == 0)
349 error = vfs_allocate_syncvnode(mp);
dadab5e9
MD
350 vfs_unbusy(mp, td);
351 if ((error = VFS_START(mp, 0, td)) != 0)
984263bc
MD
352 vrele(vp);
353 } else {
8a8d5d85 354 lwkt_gettoken(&vp->v_interlock);
984263bc 355 vp->v_flag &= ~VMOUNT;
8a8d5d85 356 lwkt_reltoken(&vp->v_interlock);
984263bc 357 mp->mnt_vfc->vfc_refcount--;
dadab5e9 358 vfs_unbusy(mp, td);
984263bc
MD
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
dadab5e9 371checkdirs(struct vnode *olddp)
984263bc
MD
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");
f62004ad 381 FOREACH_PROC_IN_SYSTEM(p) {
984263bc
MD
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;
7ea21ed1 398 vfs_cache_setroot(rootvnode);
984263bc
MD
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 */
41c20dac
MD
409/*
410 * umount_args(char *path, int flags)
411 */
984263bc
MD
412/* ARGSUSED */
413int
41c20dac 414unmount(struct unmount_args *uap)
984263bc 415{
dadab5e9
MD
416 struct thread *td = curthread;
417 struct proc *p = td->td_proc;
41c20dac 418 struct vnode *vp;
984263bc
MD
419 struct mount *mp;
420 int error;
421 struct nameidata nd;
422
dadab5e9 423 KKASSERT(p);
2b69e610 424 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9 425 SCARG(uap, path), td);
984263bc
MD
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) &&
dadab5e9 437 (error = suser(td))) {
984263bc
MD
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);
dadab5e9 458 return (dounmount(mp, SCARG(uap, flags), td));
984263bc
MD
459}
460
461/*
462 * Do the actual file system unmount.
463 */
464int
dadab5e9 465dounmount(struct mount *mp, int flags, struct thread *td)
984263bc
MD
466{
467 struct vnode *coveredvp;
468 int error;
469 int async_flag;
470
8a8d5d85 471 lwkt_gettoken(&mountlist_token);
984263bc 472 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
8a8d5d85 473 lwkt_reltoken(&mountlist_token);
984263bc
MD
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 |
8a8d5d85 481 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_token, td);
984263bc
MD
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) ||
3b568787 499 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
984263bc 500 (flags & MNT_FORCE))
dadab5e9 501 error = VFS_UNMOUNT(mp, flags, td);
8a8d5d85 502 lwkt_gettoken(&mountlist_token);
984263bc
MD
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,
8a8d5d85 509 &mountlist_token, td);
984263bc
MD
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) {
7ea21ed1 516 coveredvp->v_mountedhere = NULL;
984263bc
MD
517 vrele(coveredvp);
518 }
519 mp->mnt_vfc->vfc_refcount--;
520 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
521 panic("unmount: dangling vnode");
8a8d5d85 522 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_token, td);
984263bc
MD
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 */
984263bc
MD
532
533#ifdef DEBUG
534static int syncprt = 0;
535SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
536#endif
537
538/* ARGSUSED */
539int
41c20dac 540sync(struct sync_args *uap)
984263bc 541{
dadab5e9 542 struct thread *td = curthread;
41c20dac 543 struct mount *mp, *nmp;
984263bc
MD
544 int asyncflag;
545
8a8d5d85 546 lwkt_gettoken(&mountlist_token);
984263bc 547 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
8a8d5d85 548 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
984263bc
MD
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);
3b568787 556 VFS_SYNC(mp, MNT_NOWAIT, td);
984263bc
MD
557 mp->mnt_flag |= asyncflag;
558 }
8a8d5d85 559 lwkt_gettoken(&mountlist_token);
984263bc 560 nmp = TAILQ_NEXT(mp, mnt_list);
dadab5e9 561 vfs_unbusy(mp, td);
984263bc 562 }
8a8d5d85 563 lwkt_reltoken(&mountlist_token);
984263bc
MD
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/*
41c20dac
MD
584 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
585 *
984263bc
MD
586 * Change filesystem quotas.
587 */
984263bc
MD
588/* ARGSUSED */
589int
41c20dac 590quotactl(struct quotactl_args *uap)
984263bc 591{
dadab5e9
MD
592 struct thread *td = curthread;
593 struct proc *p = td->td_proc;
41c20dac 594 struct mount *mp;
984263bc
MD
595 int error;
596 struct nameidata nd;
597
dadab5e9 598 KKASSERT(p);
41c20dac 599 if (p->p_ucred->cr_prison && !prison_quotas)
984263bc 600 return (EPERM);
2b69e610
MD
601 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
602 SCARG(uap, path), td);
984263bc
MD
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),
dadab5e9 609 SCARG(uap, arg), td));
984263bc
MD
610}
611
612/*
41c20dac
MD
613 * statfs_args(char *path, struct statfs *buf)
614 *
984263bc
MD
615 * Get filesystem statistics.
616 */
984263bc
MD
617/* ARGSUSED */
618int
41c20dac 619statfs(struct statfs_args *uap)
984263bc 620{
dadab5e9 621 struct thread *td = curthread;
41c20dac
MD
622 struct mount *mp;
623 struct statfs *sp;
984263bc
MD
624 int error;
625 struct nameidata nd;
626 struct statfs sb;
627
2b69e610
MD
628 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
629 SCARG(uap, path), td);
984263bc
MD
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);
dadab5e9 636 error = VFS_STATFS(mp, sp, td);
984263bc
MD
637 if (error)
638 return (error);
639 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
dadab5e9 640 if (suser(td)) {
984263bc
MD
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/*
41c20dac
MD
649 * fstatfs_args(int fd, struct statfs *buf)
650 *
984263bc
MD
651 * Get filesystem statistics.
652 */
984263bc
MD
653/* ARGSUSED */
654int
41c20dac 655fstatfs(struct fstatfs_args *uap)
984263bc 656{
dadab5e9
MD
657 struct thread *td = curthread;
658 struct proc *p = td->td_proc;
984263bc
MD
659 struct file *fp;
660 struct mount *mp;
1fd87d54 661 struct statfs *sp;
984263bc
MD
662 int error;
663 struct statfs sb;
664
dadab5e9 665 KKASSERT(p);
984263bc
MD
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;
dadab5e9 672 error = VFS_STATFS(mp, sp, td);
984263bc
MD
673 if (error)
674 return (error);
675 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
dadab5e9 676 if (suser(td)) {
984263bc
MD
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/*
41c20dac
MD
685 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
686 *
984263bc
MD
687 * Get statistics on all filesystems.
688 */
41c20dac 689/* ARGSUSED */
984263bc 690int
41c20dac 691getfsstat(struct getfsstat_args *uap)
984263bc 692{
dadab5e9 693 struct thread *td = curthread;
41c20dac
MD
694 struct mount *mp, *nmp;
695 struct statfs *sp;
984263bc
MD
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;
8a8d5d85 702 lwkt_gettoken(&mountlist_token);
984263bc 703 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
8a8d5d85 704 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
984263bc
MD
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)) &&
dadab5e9 717 (error = VFS_STATFS(mp, sp, td))) {
8a8d5d85 718 lwkt_gettoken(&mountlist_token);
984263bc 719 nmp = TAILQ_NEXT(mp, mnt_list);
dadab5e9 720 vfs_unbusy(mp, td);
984263bc
MD
721 continue;
722 }
723 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
724 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
725 if (error) {
dadab5e9 726 vfs_unbusy(mp, td);
984263bc
MD
727 return (error);
728 }
729 sfsp += sizeof(*sp);
730 }
731 count++;
8a8d5d85 732 lwkt_gettoken(&mountlist_token);
984263bc 733 nmp = TAILQ_NEXT(mp, mnt_list);
dadab5e9 734 vfs_unbusy(mp, td);
984263bc 735 }
8a8d5d85 736 lwkt_reltoken(&mountlist_token);
984263bc 737 if (sfsp && count > maxcount)
c7114eea 738 uap->sysmsg_result = maxcount;
984263bc 739 else
c7114eea 740 uap->sysmsg_result = count;
984263bc
MD
741 return (0);
742}
743
744/*
41c20dac
MD
745 * fchdir_args(int fd)
746 *
984263bc
MD
747 * Change current working directory to a given file descriptor.
748 */
984263bc
MD
749/* ARGSUSED */
750int
41c20dac 751fchdir(struct fchdir_args *uap)
984263bc 752{
dadab5e9
MD
753 struct thread *td = curthread;
754 struct proc *p = td->td_proc;
41c20dac 755 struct filedesc *fdp = p->p_fd;
984263bc
MD
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);
dadab5e9 765 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
766 if (vp->v_type != VDIR)
767 error = ENOTDIR;
768 else
dadab5e9 769 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
984263bc 770 while (!error && (mp = vp->v_mountedhere) != NULL) {
dadab5e9 771 if (vfs_busy(mp, 0, 0, td))
984263bc
MD
772 continue;
773 error = VFS_ROOT(mp, &tdp);
dadab5e9 774 vfs_unbusy(mp, td);
984263bc
MD
775 if (error)
776 break;
777 vput(vp);
778 vp = tdp;
779 }
780 if (error) {
781 vput(vp);
782 return (error);
783 }
dadab5e9 784 VOP_UNLOCK(vp, 0, td);
984263bc
MD
785 vrele(fdp->fd_cdir);
786 fdp->fd_cdir = vp;
787 return (0);
788}
789
790/*
41c20dac
MD
791 * chdir_args(char *path)
792 *
984263bc
MD
793 * Change current working directory (``.'').
794 */
984263bc
MD
795/* ARGSUSED */
796int
41c20dac 797chdir(struct chdir_args *uap)
984263bc 798{
dadab5e9
MD
799 struct thread *td = curthread;
800 struct proc *p = td->td_proc;
41c20dac 801 struct filedesc *fdp = p->p_fd;
984263bc
MD
802 int error;
803 struct nameidata nd;
804
2b69e610 805 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9
MD
806 SCARG(uap, path), td);
807 if ((error = change_dir(&nd, td)) != 0)
984263bc
MD
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/*
41c20dac
MD
854 * chroot_args(char *path)
855 *
984263bc
MD
856 * Change notion of root (``/'') directory.
857 */
984263bc
MD
858/* ARGSUSED */
859int
41c20dac 860chroot(struct chroot_args *uap)
984263bc 861{
dadab5e9
MD
862 struct thread *td = curthread;
863 struct proc *p = td->td_proc;
41c20dac 864 struct filedesc *fdp = p->p_fd;
984263bc
MD
865 int error;
866 struct nameidata nd;
867
dadab5e9
MD
868 KKASSERT(p);
869 error = suser_cred(p->p_ucred, PRISON_ROOT);
984263bc
MD
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);
2b69e610 877 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9
MD
878 SCARG(uap, path), td);
879 if ((error = change_dir(&nd, td)) != 0)
984263bc
MD
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
dadab5e9 895change_dir(struct nameidata *ndp, struct thread *td)
984263bc
MD
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
dadab5e9 907 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
984263bc
MD
908 if (error)
909 vput(vp);
910 else
dadab5e9 911 VOP_UNLOCK(vp, 0, td);
984263bc
MD
912 return (error);
913}
914
915/*
41c20dac
MD
916 * open_args(char *path, int flags, int mode)
917 *
984263bc
MD
918 * Check permissions, allocate an open file structure,
919 * and call the device open routine if any.
920 */
984263bc 921int
41c20dac 922open(struct open_args *uap)
984263bc 923{
dadab5e9
MD
924 struct thread *td = curthread;
925 struct proc *p = td->td_proc;
41c20dac
MD
926 struct filedesc *fdp = p->p_fd;
927 struct file *fp;
928 struct vnode *vp;
984263bc
MD
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;
2b69e610
MD
944 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
945 SCARG(uap, path), td);
984263bc
MD
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 */
dadab5e9 957 fdrop(fp, td);
984263bc
MD
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 =
41c20dac 967 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
c7114eea 968 uap->sysmsg_result = indx;
984263bc
MD
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;
dadab5e9 977 fdrop(fp, td);
984263bc
MD
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"));
dadab5e9 999 VOP_UNLOCK(vp, 0, td);
3b568787 1000 vn_close(vp, flags & FMASK, td);
dadab5e9 1001 fdrop(fp, td);
c7114eea 1002 uap->sysmsg_result = indx;
984263bc
MD
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;
dadab5e9 1021 VOP_UNLOCK(vp, 0, td);
984263bc
MD
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;
dadab5e9 1031 fdrop(fp, td);
984263bc 1032 }
dadab5e9 1033 fdrop(fp, td);
984263bc
MD
1034 return (error);
1035 }
dadab5e9 1036 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
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"));
dadab5e9 1042 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1043
1044 /*
1045 * release our private reference, leaving the one associated with the
1046 * descriptor table intact.
1047 */
dadab5e9 1048 fdrop(fp, td);
c7114eea 1049 uap->sysmsg_result = indx;
984263bc
MD
1050 return (0);
1051}
1052
1053#ifdef COMPAT_43
1054/*
41c20dac
MD
1055 * ocreat(char *path, int mode)
1056 *
984263bc
MD
1057 * Create a file.
1058 */
984263bc 1059int
41c20dac 1060ocreat(struct ocreat_args *uap)
984263bc
MD
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;
41c20dac 1071 return (open(&nuap));
984263bc
MD
1072}
1073#endif /* COMPAT_43 */
1074
1075/*
41c20dac
MD
1076 * mknod_args(char *path, int mode, int dev)
1077 *
984263bc
MD
1078 * Create a special file.
1079 */
984263bc
MD
1080/* ARGSUSED */
1081int
41c20dac 1082mknod(struct mknod_args *uap)
984263bc 1083{
dadab5e9
MD
1084 struct thread *td = curthread;
1085 struct proc *p = td->td_proc;
41c20dac 1086 struct vnode *vp;
984263bc
MD
1087 struct vattr vattr;
1088 int error;
1089 int whiteout = 0;
1090 struct nameidata nd;
1091
dadab5e9
MD
1092 KKASSERT(p);
1093
984263bc
MD
1094 switch (SCARG(uap, mode) & S_IFMT) {
1095 case S_IFCHR:
1096 case S_IFBLK:
dadab5e9 1097 error = suser(td);
984263bc
MD
1098 break;
1099 default:
dadab5e9 1100 error = suser_cred(p->p_ucred, PRISON_ROOT);
984263bc
MD
1101 break;
1102 }
1103 if (error)
1104 return (error);
1105 bwillwrite();
2b69e610
MD
1106 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1107 SCARG(uap, path), td);
984263bc
MD
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) {
dadab5e9 1138 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
984263bc 1139 if (whiteout)
bc0c094e 1140 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_CREATE);
984263bc 1141 else {
bc0c094e 1142 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp,
984263bc
MD
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/*
41c20dac
MD
1164 * mkfifo_args(char *path, int mode)
1165 *
984263bc
MD
1166 * Create a named pipe.
1167 */
984263bc
MD
1168/* ARGSUSED */
1169int
41c20dac 1170mkfifo(struct mkfifo_args *uap)
984263bc 1171{
dadab5e9
MD
1172 struct thread *td = curthread;
1173 struct proc *p = td->td_proc;
984263bc
MD
1174 struct vattr vattr;
1175 int error;
1176 struct nameidata nd;
1177
1178 bwillwrite();
2b69e610
MD
1179 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1180 SCARG(uap, path), td);
984263bc
MD
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;
dadab5e9 1195 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
bc0c094e 1196 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr);
984263bc
MD
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/*
41c20dac
MD
1205 * link_args(char *path, char *link)
1206 *
984263bc
MD
1207 * Make a hard file link.
1208 */
984263bc
MD
1209/* ARGSUSED */
1210int
41c20dac 1211link(struct link_args *uap)
984263bc 1212{
dadab5e9
MD
1213 struct thread *td = curthread;
1214 struct proc *p = td->td_proc;
41c20dac 1215 struct vnode *vp;
984263bc
MD
1216 struct nameidata nd;
1217 int error;
1218
1219 bwillwrite();
2b69e610
MD
1220 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1221 SCARG(uap, path), td);
984263bc
MD
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 {
2b69e610
MD
1229 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1230 UIO_USERSPACE, SCARG(uap, link), td);
984263bc
MD
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 {
dadab5e9 1238 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
984263bc 1239 LEASE_WRITE);
dadab5e9 1240 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
bc0c094e 1241 error = VOP_LINK(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
984263bc
MD
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/*
41c20dac
MD
1257 * symlink(char *path, char *link)
1258 *
984263bc
MD
1259 * Make a symbolic link.
1260 */
984263bc
MD
1261/* ARGSUSED */
1262int
41c20dac 1263symlink(struct symlink_args *uap)
984263bc 1264{
dadab5e9
MD
1265 struct thread *td = curthread;
1266 struct proc *p = td->td_proc;
984263bc
MD
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();
2b69e610
MD
1276 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ, UIO_USERSPACE,
1277 SCARG(uap, link), td);
984263bc
MD
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;
dadab5e9 1292 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
bc0c094e 1293 error = VOP_SYMLINK(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
984263bc
MD
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/*
41c20dac
MD
1306 * undelete_args(char *path)
1307 *
984263bc
MD
1308 * Delete a whiteout from the filesystem.
1309 */
1310/* ARGSUSED */
1311int
41c20dac 1312undelete(struct undelete_args *uap)
984263bc 1313{
dadab5e9
MD
1314 struct thread *td = curthread;
1315 struct proc *p = td->td_proc;
984263bc
MD
1316 int error;
1317 struct nameidata nd;
1318
1319 bwillwrite();
2b69e610 1320 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
dadab5e9 1321 SCARG(uap, path), td);
984263bc
MD
1322 error = namei(&nd);
1323 if (error)
1324 return (error);
1325
2b69e610 1326 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
984263bc
MD
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
dadab5e9 1337 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
bc0c094e 1338 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
984263bc
MD
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/*
41c20dac
MD
1347 * unlink_args(char *path)
1348 *
984263bc
MD
1349 * Delete a name from the filesystem.
1350 */
984263bc 1351int
41c20dac 1352unlink(struct unlink_args *uap)
984263bc 1353{
dadab5e9
MD
1354 struct thread *td = curthread;
1355 struct proc *p = td->td_proc;
41c20dac 1356 struct vnode *vp;
984263bc
MD
1357 int error;
1358 struct nameidata nd;
1359
1360 bwillwrite();
2b69e610 1361 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1362 if ((error = namei(&nd)) != 0)
1363 return (error);
1364 vp = nd.ni_vp;
dadab5e9
MD
1365 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1366 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
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) {
dadab5e9 1381 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
bc0c094e 1382 error = VOP_REMOVE(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
984263bc
MD
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/*
41c20dac
MD
1397 * lseek_args(int fd, int pad, off_t offset, int whence)
1398 *
984263bc
MD
1399 * Reposition read/write file offset.
1400 */
984263bc 1401int
41c20dac 1402lseek(struct lseek_args *uap)
984263bc 1403{
dadab5e9
MD
1404 struct thread *td = curthread;
1405 struct proc *p = td->td_proc;
41c20dac
MD
1406 struct filedesc *fdp = p->p_fd;
1407 struct file *fp;
984263bc
MD
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:
3b568787 1421 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
984263bc
MD
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 }
c7114eea 1432 uap->sysmsg_offset = fp->f_offset;
984263bc
MD
1433 return (0);
1434}
1435
1436#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1437/*
1438 * Reposition read/write file offset.
41c20dac
MD
1439 *
1440 * olseek_args(int fd, long offset, int whence)
984263bc 1441 */
984263bc 1442int
41c20dac 1443olseek(struct olseek_args *uap)
984263bc
MD
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);
41c20dac 1456 error = lseek(&nuap);
984263bc
MD
1457 return (error);
1458}
1459#endif /* COMPAT_43 */
1460
1461/*
41c20dac
MD
1462 * access_args(char *path, int flags)
1463 *
984263bc
MD
1464 * Check access permissions.
1465 */
984263bc 1466int
41c20dac 1467access(struct access_args *uap)
984263bc 1468{
dadab5e9
MD
1469 struct thread *td = curthread;
1470 struct proc *p = td->td_proc;
984263bc 1471 struct ucred *cred, *tmpcred;
41c20dac 1472 struct vnode *vp;
984263bc
MD
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);
41c20dac
MD
1483 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1484 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
984263bc 1485 p->p_ucred = tmpcred;
2b69e610
MD
1486 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1487 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
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)
dadab5e9 1502 error = VOP_ACCESS(vp, flags, tmpcred, td);
984263bc
MD
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/*
41c20dac
MD
1514 * ostat_args(char *path, struct ostat *ub)
1515 *
984263bc
MD
1516 * Get file status; this version follows links.
1517 */
984263bc
MD
1518/* ARGSUSED */
1519int
41c20dac 1520ostat(struct ostat_args *uap)
984263bc 1521{
dadab5e9 1522 struct thread *td = curthread;
984263bc
MD
1523 struct stat sb;
1524 struct ostat osb;
1525 int error;
1526 struct nameidata nd;
1527
2b69e610
MD
1528 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1529 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1530 if ((error = namei(&nd)) != 0)
1531 return (error);
1532 NDFREE(&nd, NDF_ONLY_PNBUF);
dadab5e9 1533 error = vn_stat(nd.ni_vp, &sb, td);
984263bc
MD
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/*
41c20dac
MD
1543 * olstat_args(char *path, struct ostat *ub)
1544 *
984263bc
MD
1545 * Get file status; this version does not follow links.
1546 */
984263bc
MD
1547/* ARGSUSED */
1548int
41c20dac 1549olstat(struct olstat_args *uap)
984263bc 1550{
dadab5e9 1551 struct thread *td = curthread;
984263bc
MD
1552 struct vnode *vp;
1553 struct stat sb;
1554 struct ostat osb;
1555 int error;
1556 struct nameidata nd;
1557
6fc0e645 1558 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
2b69e610 1559 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1560 if ((error = namei(&nd)) != 0)
1561 return (error);
1562 vp = nd.ni_vp;
dadab5e9 1563 error = vn_stat(vp, &sb, td);
984263bc
MD
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{
984263bc
MD
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/*
41c20dac
MD
1603 * stat_args(char *path, struct stat *ub)
1604 *
984263bc
MD
1605 * Get file status; this version follows links.
1606 */
984263bc
MD
1607/* ARGSUSED */
1608int
41c20dac 1609stat(struct stat_args *uap)
984263bc 1610{
dadab5e9 1611 struct thread *td = curthread;
984263bc
MD
1612 struct stat sb;
1613 int error;
1614 struct nameidata nd;
1615
2b69e610
MD
1616 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1617 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1618 if ((error = namei(&nd)) != 0)
1619 return (error);
dadab5e9 1620 error = vn_stat(nd.ni_vp, &sb, td);
984263bc
MD
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/*
41c20dac
MD
1630 * lstat_args(char *path, struct stat *ub)
1631 *
984263bc
MD
1632 * Get file status; this version does not follow links.
1633 */
984263bc
MD
1634/* ARGSUSED */
1635int
41c20dac 1636lstat(struct lstat_args *uap)
984263bc 1637{
dadab5e9 1638 struct thread *td = curthread;
984263bc
MD
1639 int error;
1640 struct vnode *vp;
1641 struct stat sb;
1642 struct nameidata nd;
1643
6fc0e645 1644 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
2b69e610 1645 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1646 if ((error = namei(&nd)) != 0)
1647 return (error);
1648 vp = nd.ni_vp;
dadab5e9 1649 error = vn_stat(vp, &sb, td);
984263bc
MD
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
41c20dac
MD
1682/*
1683 * nstat_args(char *path, struct nstat *ub)
1684 */
984263bc
MD
1685/* ARGSUSED */
1686int
41c20dac 1687nstat(struct nstat_args *uap)
984263bc 1688{
dadab5e9 1689 struct thread *td = curthread;
984263bc
MD
1690 struct stat sb;
1691 struct nstat nsb;
1692 int error;
1693 struct nameidata nd;
1694
2b69e610
MD
1695 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1696 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1697 if ((error = namei(&nd)) != 0)
1698 return (error);
1699 NDFREE(&nd, NDF_ONLY_PNBUF);
dadab5e9 1700 error = vn_stat(nd.ni_vp, &sb, td);
984263bc
MD
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/*
41c20dac
MD
1710 * lstat_args(char *path, struct stat *ub)
1711 *
984263bc
MD
1712 * Get file status; this version does not follow links.
1713 */
984263bc
MD
1714/* ARGSUSED */
1715int
41c20dac 1716nlstat(struct nlstat_args *uap)
984263bc 1717{
dadab5e9 1718 struct thread *td = curthread;
984263bc
MD
1719 int error;
1720 struct vnode *vp;
1721 struct stat sb;
1722 struct nstat nsb;
1723 struct nameidata nd;
1724
6fc0e645 1725 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
2b69e610 1726 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1727 if ((error = namei(&nd)) != 0)
1728 return (error);
1729 vp = nd.ni_vp;
1730 NDFREE(&nd, NDF_ONLY_PNBUF);
dadab5e9 1731 error = vn_stat(vp, &sb, td);
984263bc
MD
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/*
41c20dac
MD
1741 * pathconf_Args(char *path, int name)
1742 *
984263bc
MD
1743 * Get configurable pathname variables.
1744 */
984263bc
MD
1745/* ARGSUSED */
1746int
41c20dac 1747pathconf(struct pathconf_args *uap)
984263bc 1748{
dadab5e9 1749 struct thread *td = curthread;
984263bc
MD
1750 int error;
1751 struct nameidata nd;
1752
2b69e610
MD
1753 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1754 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1755 if ((error = namei(&nd)) != 0)
1756 return (error);
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
c7114eea 1758 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
984263bc
MD
1759 vput(nd.ni_vp);
1760 return (error);
1761}
1762
1763/*
41c20dac
MD
1764 * readlink_args(char *path, char *buf, int count)
1765 *
984263bc
MD
1766 * Return target name of a symbolic link.
1767 */
984263bc
MD
1768/* ARGSUSED */
1769int
41c20dac 1770readlink(struct readlink_args *uap)
984263bc 1771{
dadab5e9
MD
1772 struct thread *td = curthread;
1773 struct proc *p = td->td_proc;
41c20dac 1774 struct vnode *vp;
984263bc
MD
1775 struct iovec aiov;
1776 struct uio auio;
1777 int error;
1778 struct nameidata nd;
1779
6fc0e645 1780 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
2b69e610 1781 UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
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;
dadab5e9 1796 auio.uio_td = td;
984263bc
MD
1797 auio.uio_resid = SCARG(uap, count);
1798 error = VOP_READLINK(vp, &auio, p->p_ucred);
1799 }
1800 vput(vp);
c7114eea 1801 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
984263bc
MD
1802 return (error);
1803}
1804
1805static int
41c20dac 1806setfflags(struct vnode *vp, int flags)
984263bc 1807{
dadab5e9
MD
1808 struct thread *td = curthread;
1809 struct proc *p = td->td_proc;
984263bc
MD
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) &&
dadab5e9 1820 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
984263bc
MD
1821 return (error);
1822
dadab5e9
MD
1823 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1824 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
1825 VATTR_NULL(&vattr);
1826 vattr.va_flags = flags;
dadab5e9
MD
1827 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1828 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1829 return (error);
1830}
1831
1832/*
41c20dac
MD
1833 * chflags(char *path, int flags)
1834 *
984263bc
MD
1835 * Change flags of a file given a path name.
1836 */
984263bc
MD
1837/* ARGSUSED */
1838int
41c20dac 1839chflags(struct chflags_args *uap)
984263bc 1840{
dadab5e9 1841 struct thread *td = curthread;
984263bc
MD
1842 int error;
1843 struct nameidata nd;
1844
2b69e610
MD
1845 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1846 SCARG(uap, path), td);
984263bc
MD
1847 if ((error = namei(&nd)) != 0)
1848 return (error);
1849 NDFREE(&nd, NDF_ONLY_PNBUF);
41c20dac 1850 error = setfflags(nd.ni_vp, SCARG(uap, flags));
984263bc
MD
1851 vrele(nd.ni_vp);
1852 return error;
1853}
1854
1855/*
41c20dac
MD
1856 * fchflags_args(int fd, int flags)
1857 *
984263bc
MD
1858 * Change flags of a file given a file descriptor.
1859 */
984263bc
MD
1860/* ARGSUSED */
1861int
41c20dac 1862fchflags(struct fchflags_args *uap)
984263bc 1863{
dadab5e9
MD
1864 struct thread *td = curthread;
1865 struct proc *p = td->td_proc;
984263bc
MD
1866 struct file *fp;
1867 int error;
1868
1869 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1870 return (error);
41c20dac 1871 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
984263bc
MD
1872}
1873
1874static int
41c20dac 1875setfmode(struct vnode *vp, int mode)
984263bc 1876{
dadab5e9
MD
1877 struct thread *td = curthread;
1878 struct proc *p = td->td_proc;
984263bc
MD
1879 int error;
1880 struct vattr vattr;
1881
dadab5e9
MD
1882 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1883 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
1884 VATTR_NULL(&vattr);
1885 vattr.va_mode = mode & ALLPERMS;
dadab5e9
MD
1886 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1887 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1888 return error;
1889}
1890
1891/*
41c20dac
MD
1892 * chmod_args(char *path, int mode)
1893 *
984263bc
MD
1894 * Change mode of a file given path name.
1895 */
984263bc
MD
1896/* ARGSUSED */
1897int
41c20dac 1898chmod(struct chmod_args *uap)
984263bc 1899{
dadab5e9 1900 struct thread *td = curthread;
984263bc
MD
1901 int error;
1902 struct nameidata nd;
1903
2b69e610
MD
1904 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1905 SCARG(uap, path), td);
984263bc
MD
1906 if ((error = namei(&nd)) != 0)
1907 return (error);
1908 NDFREE(&nd, NDF_ONLY_PNBUF);
41c20dac 1909 error = setfmode(nd.ni_vp, SCARG(uap, mode));
984263bc
MD
1910 vrele(nd.ni_vp);
1911 return error;
1912}
1913
1914/*
41c20dac
MD
1915 * lchmod_args(char *path, int mode)
1916 *
984263bc
MD
1917 * Change mode of a file given path name (don't follow links.)
1918 */
984263bc
MD
1919/* ARGSUSED */
1920int
41c20dac 1921lchmod(struct lchmod_args *uap)
984263bc 1922{
dadab5e9 1923 struct thread *td = curthread;
984263bc
MD
1924 int error;
1925 struct nameidata nd;
1926
6fc0e645 1927 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
1928 if ((error = namei(&nd)) != 0)
1929 return (error);
1930 NDFREE(&nd, NDF_ONLY_PNBUF);
41c20dac 1931 error = setfmode(nd.ni_vp, SCARG(uap, mode));
984263bc
MD
1932 vrele(nd.ni_vp);
1933 return error;
1934}
1935
1936/*
41c20dac
MD
1937 * fchmod_args(int fd, int mode)
1938 *
984263bc
MD
1939 * Change mode of a file given a file descriptor.
1940 */
984263bc
MD
1941/* ARGSUSED */
1942int
41c20dac 1943fchmod(struct fchmod_args *uap)
984263bc 1944{
dadab5e9
MD
1945 struct thread *td = curthread;
1946 struct proc *p = td->td_proc;
984263bc
MD
1947 struct file *fp;
1948 int error;
1949
1950 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1951 return (error);
41c20dac 1952 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
984263bc
MD
1953}
1954
1955static int
41c20dac 1956setfown(struct vnode *vp, uid_t uid, gid_t gid)
984263bc 1957{
dadab5e9
MD
1958 struct thread *td = curthread;
1959 struct proc *p = td->td_proc;
984263bc
MD
1960 int error;
1961 struct vattr vattr;
1962
dadab5e9
MD
1963 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1964 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
1965 VATTR_NULL(&vattr);
1966 vattr.va_uid = uid;
1967 vattr.va_gid = gid;
dadab5e9
MD
1968 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1969 VOP_UNLOCK(vp, 0, td);
984263bc
MD
1970 return error;
1971}
1972
1973/*
41c20dac
MD
1974 * chown(char *path, int uid, int gid)
1975 *
984263bc
MD
1976 * Set ownership given a path name.
1977 */
984263bc
MD
1978/* ARGSUSED */
1979int
41c20dac 1980chown(struct chown_args *uap)
984263bc 1981{
dadab5e9 1982 struct thread *td = curthread;
984263bc
MD
1983 int error;
1984 struct nameidata nd;
1985
2b69e610
MD
1986 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1987 SCARG(uap, path), td);
984263bc
MD
1988 if ((error = namei(&nd)) != 0)
1989 return (error);
1990 NDFREE(&nd, NDF_ONLY_PNBUF);
41c20dac 1991 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
984263bc
MD
1992 vrele(nd.ni_vp);
1993 return (error);
1994}
1995
1996/*
41c20dac
MD
1997 * lchown_args(char *path, int uid, int gid)
1998 *
984263bc
MD
1999 * Set ownership given a path name, do not cross symlinks.
2000 */
984263bc
MD
2001/* ARGSUSED */
2002int
41c20dac 2003lchown(struct lchown_args *uap)
984263bc 2004{
dadab5e9 2005 struct thread *td = curthread;
984263bc
MD
2006 int error;
2007 struct nameidata nd;
2008
6fc0e645 2009 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
2010 if ((error = namei(&nd)) != 0)
2011 return (error);
2012 NDFREE(&nd, NDF_ONLY_PNBUF);
41c20dac 2013 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
984263bc
MD
2014 vrele(nd.ni_vp);
2015 return (error);
2016}
2017
2018/*
41c20dac
MD
2019 * fchown_args(int fd, int uid, int gid)
2020 *
984263bc
MD
2021 * Set ownership given a file descriptor.
2022 */
984263bc
MD
2023/* ARGSUSED */
2024int
41c20dac 2025fchown(struct fchown_args *uap)
984263bc 2026{
dadab5e9
MD
2027 struct thread *td = curthread;
2028 struct proc *p = td->td_proc;
984263bc
MD
2029 struct file *fp;
2030 int error;
2031
2032 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2033 return (error);
41c20dac 2034 return setfown((struct vnode *)fp->f_data,
984263bc
MD
2035 SCARG(uap, uid), SCARG(uap, gid));
2036}
2037
2038static int
41c20dac 2039getutimes(const struct timeval *usrtvp, struct timespec *tsp)
984263bc
MD
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
41c20dac 2058setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
984263bc 2059{
dadab5e9
MD
2060 struct thread *td = curthread;
2061 struct proc *p = td->td_proc;
984263bc
MD
2062 int error;
2063 struct vattr vattr;
2064
dadab5e9
MD
2065 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2066 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
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;
dadab5e9
MD
2072 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2073 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2074 return error;
2075}
2076
2077/*
41c20dac
MD
2078 * utimes_args(char *path, struct timeval *tptr)
2079 *
984263bc
MD
2080 * Set the access and modification times of a file.
2081 */
984263bc
MD
2082/* ARGSUSED */
2083int
41c20dac 2084utimes(struct utimes_args *uap)
984263bc 2085{
dadab5e9 2086 struct thread *td = curthread;
984263bc
MD
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);
2b69e610 2095 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
2096 if ((error = namei(&nd)) != 0)
2097 return (error);
2098 NDFREE(&nd, NDF_ONLY_PNBUF);
41c20dac 2099 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
984263bc
MD
2100 vrele(nd.ni_vp);
2101 return (error);
2102}
2103
2104/*
41c20dac
MD
2105 * lutimes_args(char *path, struct timeval *tptr)
2106 *
984263bc
MD
2107 * Set the access and modification times of a file.
2108 */
984263bc
MD
2109/* ARGSUSED */
2110int
41c20dac 2111lutimes(struct lutimes_args *uap)
984263bc 2112{
dadab5e9 2113 struct thread *td = curthread;
984263bc
MD
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);
6fc0e645 2122 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
2123 if ((error = namei(&nd)) != 0)
2124 return (error);
2125 NDFREE(&nd, NDF_ONLY_PNBUF);
41c20dac 2126 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
984263bc
MD
2127 vrele(nd.ni_vp);
2128 return (error);
2129}
2130
2131/*
41c20dac
MD
2132 * futimes_args(int fd, struct timeval *tptr)
2133 *
984263bc
MD
2134 * Set the access and modification times of a file.
2135 */
984263bc
MD
2136/* ARGSUSED */
2137int
41c20dac 2138futimes(struct futimes_args *uap)
984263bc 2139{
dadab5e9
MD
2140 struct thread *td = curthread;
2141 struct proc *p = td->td_proc;
984263bc
MD
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);
41c20dac 2152 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
984263bc
MD
2153}
2154
2155/*
41c20dac
MD
2156 * truncate(char *path, int pad, off_t length)
2157 *
984263bc
MD
2158 * Truncate a file given its path name.
2159 */
984263bc
MD
2160/* ARGSUSED */
2161int
41c20dac 2162truncate(struct truncate_args *uap)
984263bc 2163{
dadab5e9
MD
2164 struct thread *td = curthread;
2165 struct proc *p = td->td_proc;
41c20dac 2166 struct vnode *vp;
984263bc
MD
2167 struct vattr vattr;
2168 int error;
2169 struct nameidata nd;
2170
2171 if (uap->length < 0)
2172 return(EINVAL);
2b69e610 2173 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
2174 if ((error = namei(&nd)) != 0)
2175 return (error);
2176 vp = nd.ni_vp;
2177 NDFREE(&nd, NDF_ONLY_PNBUF);
dadab5e9
MD
2178 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2179 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
2180 if (vp->v_type == VDIR)
2181 error = EISDIR;
2182 else if ((error = vn_writechk(vp)) == 0 &&
dadab5e9 2183 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
984263bc
MD
2184 VATTR_NULL(&vattr);
2185 vattr.va_size = SCARG(uap, length);
dadab5e9 2186 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
984263bc
MD
2187 }
2188 vput(vp);
2189 return (error);
2190}
2191
984263bc 2192int
8f6f8622 2193kern_ftruncate(int fd, off_t length)
984263bc 2194{
dadab5e9
MD
2195 struct thread *td = curthread;
2196 struct proc *p = td->td_proc;
984263bc
MD
2197 struct vattr vattr;
2198 struct vnode *vp;
2199 struct file *fp;
2200 int error;
2201
8f6f8622 2202 if (length < 0)
984263bc 2203 return(EINVAL);
8f6f8622 2204 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
2205 return (error);
2206 if ((fp->f_flag & FWRITE) == 0)
2207 return (EINVAL);
2208 vp = (struct vnode *)fp->f_data;
dadab5e9
MD
2209 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2210 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
2211 if (vp->v_type == VDIR)
2212 error = EISDIR;
2213 else if ((error = vn_writechk(vp)) == 0) {
2214 VATTR_NULL(&vattr);
8f6f8622 2215 vattr.va_size = length;
dadab5e9 2216 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
984263bc 2217 }
dadab5e9 2218 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2219 return (error);
2220}
2221
984263bc 2222/*
8f6f8622 2223 * ftruncate_args(int fd, int pad, off_t length)
41c20dac 2224 *
8f6f8622 2225 * Truncate a file given a file descriptor.
984263bc 2226 */
984263bc
MD
2227/* ARGSUSED */
2228int
8f6f8622 2229ftruncate(struct ftruncate_args *uap)
984263bc 2230{
8f6f8622 2231 int error;
984263bc 2232
8f6f8622
DRJ
2233 error = kern_ftruncate(uap->fd, uap->length);
2234
2235 return (error);
984263bc
MD
2236}
2237
8f6f8622 2238#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
984263bc 2239/*
8f6f8622 2240 * otruncate_args(char *path, long length)
41c20dac 2241 *
8f6f8622 2242 * Truncate a file given its path name.
984263bc 2243 */
984263bc
MD
2244/* ARGSUSED */
2245int
8f6f8622 2246otruncate(struct otruncate_args *uap)
984263bc 2247{
8f6f8622
DRJ
2248 struct truncate_args /* {
2249 syscallarg(char *) path;
984263bc
MD
2250 syscallarg(int) pad;
2251 syscallarg(off_t) length;
2252 } */ nuap;
2253
8f6f8622 2254 SCARG(&nuap, path) = SCARG(uap, path);
984263bc 2255 SCARG(&nuap, length) = SCARG(uap, length);
8f6f8622 2256 return (truncate(&nuap));
984263bc
MD
2257}
2258#endif /* COMPAT_43 || COMPAT_SUNOS */
2259
2260/*
41c20dac
MD
2261 * fsync(int fd)
2262 *
984263bc
MD
2263 * Sync an open file.
2264 */
984263bc
MD
2265/* ARGSUSED */
2266int
41c20dac 2267fsync(struct fsync_args *uap)
984263bc 2268{
dadab5e9
MD
2269 struct thread *td = curthread;
2270 struct proc *p = td->td_proc;
41c20dac 2271 struct vnode *vp;
984263bc
MD
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;
dadab5e9 2279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
2280 if (VOP_GETVOBJECT(vp, &obj) == 0)
2281 vm_object_page_clean(obj, 0, 0, 0);
3b568787 2282 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
984263bc
MD
2283 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2284 bioops.io_fsync)
2285 error = (*bioops.io_fsync)(vp);
dadab5e9 2286 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2287 return (error);
2288}
2289
2290/*
41c20dac
MD
2291 * rename_args(char *from, char *to)
2292 *
984263bc
MD
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 */
984263bc
MD
2296/* ARGSUSED */
2297int
41c20dac 2298rename(struct rename_args *uap)
984263bc 2299{
dadab5e9
MD
2300 struct thread *td = curthread;
2301 struct proc *p = td->td_proc;
41c20dac 2302 struct vnode *tvp, *fvp, *tdvp;
984263bc
MD
2303 struct nameidata fromnd, tond;
2304 int error;
2305
2306 bwillwrite();
2b69e610
MD
2307 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2308 UIO_USERSPACE, SCARG(uap, from), td);
984263bc
MD
2309 if ((error = namei(&fromnd)) != 0)
2310 return (error);
2311 fvp = fromnd.ni_vp;
2b69e610
MD
2312 NDINIT(&tond, NAMEI_RENAME,
2313 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2314 CNP_SAVESTART | CNP_NOOBJ,
dadab5e9 2315 UIO_USERSPACE, SCARG(uap, to), td);
984263bc 2316 if (fromnd.ni_vp->v_type == VDIR)
2b69e610 2317 tond.ni_cnd.cn_flags |= CNP_WILLBEDIR;
984263bc
MD
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) {
dadab5e9 2348 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
984263bc 2349 if (fromnd.ni_dvp != tdvp) {
dadab5e9 2350 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
984263bc
MD
2351 }
2352 if (tvp) {
dadab5e9 2353 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
984263bc 2354 }
bc0c094e
MD
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);
984263bc
MD
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/*
41c20dac
MD
2385 * mkdir_args(char *path, int mode)
2386 *
984263bc
MD
2387 * Make a directory file.
2388 */
984263bc
MD
2389/* ARGSUSED */
2390int
41c20dac 2391mkdir(struct mkdir_args *uap)
984263bc 2392{
dadab5e9
MD
2393 struct thread *td = curthread;
2394 struct proc *p = td->td_proc;
41c20dac 2395 struct vnode *vp;
984263bc
MD
2396 struct vattr vattr;
2397 int error;
2398 struct nameidata nd;
2399
2400 bwillwrite();
2b69e610
MD
2401 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
2402 SCARG(uap, path), td);
2403 nd.ni_cnd.cn_flags |= CNP_WILLBEDIR;
984263bc
MD
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;
dadab5e9 2419 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
bc0c094e 2420 error = VOP_MKDIR(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr);
984263bc
MD
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/*
41c20dac
MD
2431 * rmdir_args(char *path)
2432 *
984263bc
MD
2433 * Remove a directory file.
2434 */
984263bc
MD
2435/* ARGSUSED */
2436int
41c20dac 2437rmdir(struct rmdir_args *uap)
984263bc 2438{
dadab5e9
MD
2439 struct thread *td = curthread;
2440 struct proc *p = td->td_proc;
41c20dac 2441 struct vnode *vp;
984263bc
MD
2442 int error;
2443 struct nameidata nd;
2444
2445 bwillwrite();
2b69e610 2446 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9 2447 SCARG(uap, path), td);
984263bc
MD
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 {
dadab5e9
MD
2468 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2469 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
bc0c094e 2470 error = VOP_RMDIR(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
984263bc
MD
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/*
41c20dac
MD
2487 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2488 *
984263bc
MD
2489 * Read a block of directory entries in a file system independent format.
2490 */
984263bc 2491int
41c20dac 2492ogetdirentries(struct ogetdirentries_args *uap)
984263bc 2493{
dadab5e9
MD
2494 struct thread *td = curthread;
2495 struct proc *p = td->td_proc;
984263bc
MD
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;
dadab5e9 2522 auio.uio_td = td;
984263bc 2523 auio.uio_resid = SCARG(uap, count);
dadab5e9 2524 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
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 }
dadab5e9 2577 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2578 if (error)
2579 return (error);
2580 if (SCARG(uap, count) == auio.uio_resid) {
2581 if (union_dircheckp) {
dadab5e9 2582 error = union_dircheckp(td, &vp, fp);
984263bc
MD
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));
c7114eea 2601 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
984263bc
MD
2602 return (error);
2603}
2604#endif /* COMPAT_43 */
2605
2606/*
41c20dac
MD
2607 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2608 *
984263bc
MD
2609 * Read a block of directory entries in a file system independent format.
2610 */
984263bc 2611int
41c20dac 2612getdirentries(struct getdirentries_args *uap)
984263bc 2613{
dadab5e9
MD
2614 struct thread *td = curthread;
2615 struct proc *p = td->td_proc;
984263bc
MD
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;
dadab5e9 2637 auio.uio_td = td;
984263bc 2638 auio.uio_resid = SCARG(uap, count);
dadab5e9
MD
2639 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2640 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
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;
dadab5e9 2644 VOP_UNLOCK(vp, 0, td);
984263bc
MD
2645 if (error)
2646 return (error);
2647 if (SCARG(uap, count) == auio.uio_resid) {
2648 if (union_dircheckp) {
dadab5e9 2649 error = union_dircheckp(td, &vp, fp);
984263bc
MD
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 }
c7114eea 2670 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
984263bc
MD
2671 return (error);
2672}
41c20dac
MD
2673
2674/*
2675 * getdents_args(int fd, char *buf, size_t count)
2676 */
984263bc 2677int
41c20dac 2678getdents(struct getdents_args *uap)
984263bc
MD
2679{
2680 struct getdirentries_args ap;
41c20dac 2681
984263bc
MD
2682 ap.fd = uap->fd;
2683 ap.buf = uap->buf;
2684 ap.count = uap->count;
2685 ap.basep = NULL;
41c20dac 2686 return getdirentries(&ap);
984263bc
MD
2687}
2688
2689/*
41c20dac
MD
2690 * umask(int newmask)
2691 *
984263bc
MD
2692 * Set the mode mask for creation of filesystem nodes.
2693 *
2694 * MP SAFE
2695 */
984263bc 2696int
41c20dac 2697umask(struct umask_args *uap)
984263bc 2698{
dadab5e9
MD
2699 struct thread *td = curthread;
2700 struct proc *p = td->td_proc;
41c20dac 2701 struct filedesc *fdp;
984263bc
MD
2702
2703 fdp = p->p_fd;
c7114eea 2704 uap->sysmsg_result = fdp->fd_cmask;
984263bc
MD
2705 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2706 return (0);
2707}
2708
2709/*
41c20dac
MD
2710 * revoke(char *path)
2711 *
984263bc
MD
2712 * Void all references to file by ripping underlying filesystem
2713 * away from vnode.
2714 */
984263bc
MD
2715/* ARGSUSED */
2716int
41c20dac 2717revoke(struct revoke_args *uap)
984263bc 2718{
dadab5e9
MD
2719 struct thread *td = curthread;
2720 struct proc *p = td->td_proc;
41c20dac 2721 struct vnode *vp;
984263bc
MD
2722 struct vattr vattr;
2723 int error;
2724 struct nameidata nd;
2725
2b69e610 2726 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
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 }
3b568787 2735 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
984263bc
MD
2736 goto out;
2737 if (p->p_ucred->cr_uid != vattr.va_uid &&
dadab5e9 2738 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
984263bc
MD
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
41c20dac 2751getvnode(struct filedesc *fdp, int fd, struct file **fpp)
984263bc
MD
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/*
41c20dac
MD
2764 * getfh_args(char *fname, fhandle_t *fhp)
2765 *
984263bc
MD
2766 * Get (NFS) file handle
2767 */
984263bc 2768int
41c20dac 2769getfh(struct getfh_args *uap)
984263bc 2770{
dadab5e9 2771 struct thread *td = curthread;
984263bc
MD
2772 struct nameidata nd;
2773 fhandle_t fh;
41c20dac 2774 struct vnode *vp;
984263bc
MD
2775 int error;
2776
2777 /*
2778 * Must be super user
2779 */
dadab5e9 2780 error = suser(td);
984263bc
MD
2781 if (error)
2782 return (error);
2b69e610 2783 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
984263bc
MD
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/*
41c20dac
MD
2800 * fhopen_args(const struct fhandle *u_fhp, int flags)
2801 *
984263bc
MD
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 */
984263bc 2808int
41c20dac 2809fhopen(struct fhopen_args *uap)
984263bc 2810{
dadab5e9
MD
2811 struct thread *td = curthread;
2812 struct proc *p = td->td_proc;
984263bc
MD
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;
41c20dac 2820 struct filedesc *fdp = p->p_fd;
984263bc
MD
2821 int fmode, mode, error, type;
2822 struct file *nfp;
2823 int indx;
2824
2825 /*
2826 * Must be super user
2827 */
dadab5e9 2828 error = suser(td);
984263bc
MD
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) {
dadab5e9 2879 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
984263bc
MD
2880 if (error)
2881 goto bad;
2882 }
2883 if (fmode & O_TRUNC) {
dadab5e9
MD
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 */
984263bc
MD
2887 VATTR_NULL(vap);
2888 vap->va_size = 0;
dadab5e9 2889 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
984263bc
MD
2890 if (error)
2891 goto bad;
2892 }
dadab5e9 2893 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
984263bc
MD
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) {
3b568787 2900 if ((error = vfs_object_create(vp, td)) != 0)
984263bc
MD
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;
dadab5e9 2937 VOP_UNLOCK(vp, 0, td);
984263bc
MD
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;
dadab5e9 2946 fdrop(fp, td);
984263bc
MD
2947 }
2948
2949 /*
2950 * release our private reference.
2951 */
dadab5e9 2952 fdrop(fp, td);
984263bc
MD
2953 return (error);
2954 }
dadab5e9 2955 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
2956 fp->f_flag |= FHASLOCK;
2957 }
2958 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3b568787 2959 vfs_object_create(vp, td);
984263bc 2960
dadab5e9
MD
2961 VOP_UNLOCK(vp, 0, td);
2962 fdrop(fp, td);
c7114eea 2963 uap->sysmsg_result = indx;
984263bc
MD
2964 return (0);
2965
2966bad:
2967 vput(vp);
2968 return (error);
2969}
2970
41c20dac
MD
2971/*
2972 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2973 */
984263bc 2974int
41c20dac 2975fhstat(struct fhstat_args *uap)
984263bc 2976{
dadab5e9 2977 struct thread *td = curthread;
984263bc
MD
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 */
dadab5e9 2987 error = suser(td);
984263bc
MD
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);
dadab5e9 2999 error = vn_stat(vp, &sb, td);
984263bc
MD
3000 vput(vp);
3001 if (error)
3002 return (error);
3003 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3004 return (error);
3005}
3006
41c20dac
MD
3007/*
3008 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3009 */
984263bc 3010int
41c20dac 3011fhstatfs(struct fhstatfs_args *uap)
984263bc 3012{
dadab5e9 3013 struct thread *td = curthread;
984263bc
MD
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 */
dadab5e9 3024 if ((error = suser(td)))
984263bc
MD
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);
dadab5e9 3037 if ((error = VFS_STATFS(mp, sp, td)) != 0)
984263bc
MD
3038 return (error);
3039 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
dadab5e9 3040 if (suser(td)) {
984263bc
MD
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
41c20dac 3060extattrctl(struct extattrctl_args *uap)
984263bc 3061{
dadab5e9 3062 struct thread *td = curthread;
984263bc
MD
3063 struct nameidata nd;
3064 struct mount *mp;
3065 int error;
3066
2b69e610 3067 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
984263bc
MD
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),
dadab5e9 3073 SCARG(uap, arg), td));
984263bc
MD
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
41c20dac 3083extattr_set_file(struct extattr_set_file_args *uap)
984263bc 3084{
dadab5e9
MD
3085 struct thread *td = curthread;
3086 struct proc *p = td->td_proc;
984263bc
MD
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);
2b69e610 3097 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9 3098 SCARG(uap, path), td);
984263bc
MD
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;
dadab5e9 3115 auio.uio_td = td;
984263bc
MD
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;
dadab5e9 3129 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
984263bc 3130 cnt -= auio.uio_resid;
c7114eea 3131 uap->sysmsg_result = cnt;
984263bc
MD
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
41c20dac 3146extattr_get_file(struct extattr_get_file_args *uap)
984263bc 3147{
dadab5e9
MD
3148 struct thread *td = curthread;
3149 struct proc *p = td->td_proc;
984263bc
MD
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);
2b69e610 3160 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9 3161 SCARG(uap, path), td);
984263bc
MD
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;
dadab5e9 3180 auio.uio_td = td;
984263bc
MD
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;
dadab5e9 3194 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
984263bc 3195 cnt -= auio.uio_resid;
c7114eea 3196 uap->sysmsg_result = cnt;
984263bc
MD
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
41c20dac 3209extattr_delete_file(struct extattr_delete_file_args *uap)
984263bc 3210{
dadab5e9
MD
3211 struct thread *td = curthread;
3212 struct proc *p = td->td_proc;
984263bc
MD
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);
2b69e610 3220 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
dadab5e9 3221 SCARG(uap, path), td);
984263bc
MD
3222 if ((error = namei(&nd)) != 0)
3223 return(error);
dadab5e9 3224 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
984263bc
MD
3225 NDFREE(&nd, 0);
3226 return(error);
3227}