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