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