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