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