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