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