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