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