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