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