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