priv: Use PRIV_VFS_CHFLAGS_DEV
[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 }
276 error = linker_load_file(fstypename, &lf);
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
984263bc 783/*
d3c546e6
MD
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
803/*
984263bc
MD
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);
895c1f85 911 if ((error = priv_check(td, PRIV_ROOT)) != 0)
949ecb9b
MD
912 return (error);
913
914 /*
915 * Argument length checks
916 */
39b13188 917 if (uap->ctllen < 0 || uap->ctllen > 1024)
949ecb9b 918 return (EINVAL);
39b13188 919 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
949ecb9b
MD
920 return (EINVAL);
921 if (uap->path == NULL)
922 return (EINVAL);
923
924 /*
925 * Allocate the necessary buffers and copyin data
926 */
70aac194 927 path = objcache_get(namei_oc, M_WAITOK);
949ecb9b
MD
928 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
929 if (error)
930 goto done;
931
932 if (uap->ctllen) {
efda3bd0 933 ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
949ecb9b
MD
934 error = copyin(uap->ctl, ctl, uap->ctllen);
935 if (error)
936 goto done;
937 }
938 if (uap->buflen)
efda3bd0 939 buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
949ecb9b
MD
940
941 /*
2281065e
MD
942 * Validate the descriptor
943 */
ecda6326
MD
944 if (uap->fd >= 0) {
945 fp = holdfp(p->p_fd, uap->fd, -1);
946 if (fp == NULL) {
947 error = EBADF;
948 goto done;
949 }
950 } else {
951 fp = NULL;
2281065e 952 }
2281065e
MD
953
954 /*
949ecb9b
MD
955 * Execute the internal kernel function and clean up.
956 */
2281065e
MD
957 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
958 if (fp)
9f87144f 959 fdrop(fp);
949ecb9b
MD
960 if (error == 0 && uap->sysmsg_result > 0)
961 error = copyout(buf, uap->buf, uap->sysmsg_result);
962done:
963 if (path)
70aac194 964 objcache_put(namei_oc, path);
949ecb9b 965 if (ctl)
efda3bd0 966 kfree(ctl, M_TEMP);
949ecb9b 967 if (buf)
efda3bd0 968 kfree(buf, M_TEMP);
949ecb9b
MD
969 return (error);
970}
971
972/*
973 * Execute a mount control operation by resolving the path to a mount point
974 * and calling vop_mountctl().
67863d04
MD
975 *
976 * Use the mount point from the nch instead of the vnode so nullfs mounts
977 * can properly spike the VOP.
949ecb9b
MD
978 */
979int
2281065e
MD
980kern_mountctl(const char *path, int op, struct file *fp,
981 const void *ctl, int ctllen,
949ecb9b
MD
982 void *buf, int buflen, int *res)
983{
949ecb9b
MD
984 struct vnode *vp;
985 struct mount *mp;
986 struct nlookupdata nd;
987 int error;
988
989 *res = 0;
990 vp = NULL;
991 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
992 if (error == 0)
993 error = nlookup(&nd);
994 if (error == 0)
28623bf9 995 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
67863d04 996 mp = nd.nl_nch.mount;
949ecb9b
MD
997 nlookup_done(&nd);
998 if (error)
999 return (error);
1000
949ecb9b
MD
1001 /*
1002 * Must be the root of the filesystem
1003 */
67863d04 1004 if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
949ecb9b
MD
1005 vput(vp);
1006 return (EINVAL);
1007 }
2281065e 1008 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
949ecb9b
MD
1009 buf, buflen, res);
1010 vput(vp);
1011 return (error);
1012}
1013
984263bc 1014int
fad57d0e 1015kern_statfs(struct nlookupdata *nd, struct statfs *buf)
984263bc 1016{
dadab5e9 1017 struct thread *td = curthread;
75ffff0d 1018 struct proc *p = td->td_proc;
41c20dac
MD
1019 struct mount *mp;
1020 struct statfs *sp;
75ffff0d 1021 char *fullpath, *freepath;
984263bc 1022 int error;
984263bc 1023
fad57d0e 1024 if ((error = nlookup(nd)) != 0)
984263bc 1025 return (error);
28623bf9 1026 mp = nd->nl_nch.mount;
984263bc 1027 sp = &mp->mnt_stat;
acde96db 1028 if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0)
984263bc 1029 return (error);
75ffff0d 1030
d3c546e6 1031 error = mount_path(p, mp, &fullpath, &freepath);
75ffff0d
JS
1032 if (error)
1033 return(error);
1034 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1035 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
efda3bd0 1036 kfree(freepath, M_TEMP);
75ffff0d 1037
984263bc 1038 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
9697c509
DRJ
1039 bcopy(sp, buf, sizeof(*buf));
1040 /* Only root should have access to the fsid's. */
895c1f85 1041 if (priv_check(td, PRIV_ROOT))
9697c509
DRJ
1042 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1043 return (0);
984263bc
MD
1044}
1045
1046/*
9697c509 1047 * statfs_args(char *path, struct statfs *buf)
41c20dac 1048 *
984263bc
MD
1049 * Get filesystem statistics.
1050 */
984263bc 1051int
753fd850 1052sys_statfs(struct statfs_args *uap)
9697c509 1053{
fad57d0e 1054 struct nlookupdata nd;
9697c509
DRJ
1055 struct statfs buf;
1056 int error;
1057
fad57d0e
MD
1058 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1059 if (error == 0)
1060 error = kern_statfs(&nd, &buf);
1061 nlookup_done(&nd);
9697c509
DRJ
1062 if (error == 0)
1063 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1064 return (error);
1065}
1066
1067int
1068kern_fstatfs(int fd, struct statfs *buf)
984263bc 1069{
dadab5e9
MD
1070 struct thread *td = curthread;
1071 struct proc *p = td->td_proc;
984263bc
MD
1072 struct file *fp;
1073 struct mount *mp;
1fd87d54 1074 struct statfs *sp;
75ffff0d 1075 char *fullpath, *freepath;
984263bc 1076 int error;
984263bc 1077
dadab5e9 1078 KKASSERT(p);
5b287bba 1079 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
984263bc
MD
1080 return (error);
1081 mp = ((struct vnode *)fp->f_data)->v_mount;
5b287bba
MD
1082 if (mp == NULL) {
1083 error = EBADF;
1084 goto done;
1085 }
1086 if (fp->f_cred == NULL) {
1087 error = EINVAL;
1088 goto done;
1089 }
984263bc 1090 sp = &mp->mnt_stat;
5b287bba
MD
1091 if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0)
1092 goto done;
75ffff0d 1093
d3c546e6 1094 if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
5b287bba 1095 goto done;
75ffff0d
JS
1096 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1097 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
efda3bd0 1098 kfree(freepath, M_TEMP);
75ffff0d 1099
984263bc 1100 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
9697c509 1101 bcopy(sp, buf, sizeof(*buf));
75ffff0d 1102
9697c509 1103 /* Only root should have access to the fsid's. */
895c1f85 1104 if (priv_check(td, PRIV_ROOT))
9697c509 1105 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
5b287bba
MD
1106 error = 0;
1107done:
1108 fdrop(fp);
1109 return (error);
9697c509
DRJ
1110}
1111
1112/*
1113 * fstatfs_args(int fd, struct statfs *buf)
1114 *
1115 * Get filesystem statistics.
1116 */
1117int
753fd850 1118sys_fstatfs(struct fstatfs_args *uap)
9697c509
DRJ
1119{
1120 struct statfs buf;
1121 int error;
1122
1123 error = kern_fstatfs(uap->fd, &buf);
1124
1125 if (error == 0)
1126 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1127 return (error);
984263bc
MD
1128}
1129
d9fad06e
MD
1130int
1131kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1132{
1133 struct mount *mp;
1134 struct statvfs *sp;
1135 int error;
1136
1137 if ((error = nlookup(nd)) != 0)
1138 return (error);
1139 mp = nd->nl_nch.mount;
1140 sp = &mp->mnt_vstat;
1141 if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1142 return (error);
1143
1144 sp->f_flag = 0;
1145 if (mp->mnt_flag & MNT_RDONLY)
1146 sp->f_flag |= ST_RDONLY;
1147 if (mp->mnt_flag & MNT_NOSUID)
1148 sp->f_flag |= ST_NOSUID;
1149 bcopy(sp, buf, sizeof(*buf));
1150 return (0);
1151}
1152
1153/*
1154 * statfs_args(char *path, struct statfs *buf)
1155 *
1156 * Get filesystem statistics.
1157 */
1158int
1159sys_statvfs(struct statvfs_args *uap)
1160{
1161 struct nlookupdata nd;
1162 struct statvfs buf;
1163 int error;
1164
1165 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1166 if (error == 0)
1167 error = kern_statvfs(&nd, &buf);
1168 nlookup_done(&nd);
1169 if (error == 0)
1170 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1171 return (error);
1172}
1173
1174int
1175kern_fstatvfs(int fd, struct statvfs *buf)
1176{
1177 struct thread *td = curthread;
1178 struct proc *p = td->td_proc;
1179 struct file *fp;
1180 struct mount *mp;
1181 struct statvfs *sp;
1182 int error;
1183
1184 KKASSERT(p);
1185 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
1186 return (error);
1187 mp = ((struct vnode *)fp->f_data)->v_mount;
1188 if (mp == NULL) {
1189 error = EBADF;
1190 goto done;
1191 }
1192 if (fp->f_cred == NULL) {
1193 error = EINVAL;
1194 goto done;
1195 }
1196 sp = &mp->mnt_vstat;
1197 if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1198 goto done;
1199
1200 sp->f_flag = 0;
1201 if (mp->mnt_flag & MNT_RDONLY)
1202 sp->f_flag |= ST_RDONLY;
1203 if (mp->mnt_flag & MNT_NOSUID)
1204 sp->f_flag |= ST_NOSUID;
1205
1206 bcopy(sp, buf, sizeof(*buf));
1207 error = 0;
1208done:
1209 fdrop(fp);
1210 return (error);
1211}
1212
1213/*
1214 * fstatfs_args(int fd, struct statfs *buf)
1215 *
1216 * Get filesystem statistics.
1217 */
1218int
1219sys_fstatvfs(struct fstatvfs_args *uap)
1220{
1221 struct statvfs buf;
1222 int error;
1223
1224 error = kern_fstatvfs(uap->fd, &buf);
1225
1226 if (error == 0)
1227 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1228 return (error);
1229}
1230
984263bc 1231/*
41c20dac
MD
1232 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1233 *
984263bc
MD
1234 * Get statistics on all filesystems.
1235 */
861905fb
MD
1236
1237struct getfsstat_info {
1238 struct statfs *sfsp;
1239 long count;
1240 long maxcount;
1241 int error;
1242 int flags;
861905fb
MD
1243 struct proc *p;
1244};
1245
1246static int getfsstat_callback(struct mount *, void *);
1247
41c20dac 1248/* ARGSUSED */
984263bc 1249int
753fd850 1250sys_getfsstat(struct getfsstat_args *uap)
984263bc 1251{
dadab5e9 1252 struct thread *td = curthread;
75ffff0d 1253 struct proc *p = td->td_proc;
861905fb 1254 struct getfsstat_info info;
75ffff0d 1255
861905fb 1256 bzero(&info, sizeof(info));
861905fb
MD
1257
1258 info.maxcount = uap->bufsize / sizeof(struct statfs);
1259 info.sfsp = uap->buf;
1260 info.count = 0;
1261 info.flags = uap->flags;
861905fb
MD
1262 info.p = p;
1263
1264 mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1265 if (info.sfsp && info.count > info.maxcount)
1266 uap->sysmsg_result = info.maxcount;
75ffff0d 1267 else
861905fb
MD
1268 uap->sysmsg_result = info.count;
1269 return (info.error);
1270}
1271
1272static int
1273getfsstat_callback(struct mount *mp, void *data)
1274{
1275 struct getfsstat_info *info = data;
1276 struct statfs *sp;
1277 char *freepath;
1278 char *fullpath;
1279 int error;
1280
1281 if (info->sfsp && info->count < info->maxcount) {
28623bf9 1282 if (info->p && !chroot_visible_mnt(mp, info->p))
861905fb
MD
1283 return(0);
1284 sp = &mp->mnt_stat;
1285
1286 /*
1287 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1288 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1289 * overrides MNT_WAIT.
1290 */
1291 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1292 (info->flags & MNT_WAIT)) &&
acde96db 1293 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
861905fb
MD
1294 return(0);
1295 }
1296 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1297
d3c546e6 1298 error = mount_path(info->p, mp, &fullpath, &freepath);
861905fb
MD
1299 if (error) {
1300 info->error = error;
1301 return(-1);
984263bc 1302 }
861905fb
MD
1303 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1304 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
efda3bd0 1305 kfree(freepath, M_TEMP);
861905fb
MD
1306
1307 error = copyout(sp, info->sfsp, sizeof(*sp));
1308 if (error) {
1309 info->error = error;
1310 return (-1);
984263bc 1311 }
861905fb 1312 ++info->sfsp;
984263bc 1313 }
861905fb
MD
1314 info->count++;
1315 return(0);
984263bc
MD
1316}
1317
1318/*
00fe9d48
MD
1319 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1320 long bufsize, int flags)
1321 *
1322 * Get statistics on all filesystems.
1323 */
1324
1325struct getvfsstat_info {
1326 struct statfs *sfsp;
1327 struct statvfs *vsfsp;
1328 long count;
1329 long maxcount;
1330 int error;
1331 int flags;
1332 struct proc *p;
1333};
1334
1335static int getvfsstat_callback(struct mount *, void *);
1336
1337/* ARGSUSED */
1338int
1339sys_getvfsstat(struct getvfsstat_args *uap)
1340{
1341 struct thread *td = curthread;
1342 struct proc *p = td->td_proc;
1343 struct getvfsstat_info info;
1344
1345 bzero(&info, sizeof(info));
1346
1347 info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1348 info.sfsp = uap->buf;
1349 info.vsfsp = uap->vbuf;
1350 info.count = 0;
1351 info.flags = uap->flags;
1352 info.p = p;
1353
1354 mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1355 if (info.vsfsp && info.count > info.maxcount)
1356 uap->sysmsg_result = info.maxcount;
1357 else
1358 uap->sysmsg_result = info.count;
1359 return (info.error);
1360}
1361
1362static int
1363getvfsstat_callback(struct mount *mp, void *data)
1364{
1365 struct getvfsstat_info *info = data;
1366 struct statfs *sp;
1367 struct statvfs *vsp;
1368 char *freepath;
1369 char *fullpath;
1370 int error;
1371
1372 if (info->vsfsp && info->count < info->maxcount) {
1373 if (info->p && !chroot_visible_mnt(mp, info->p))
1374 return(0);
1375 sp = &mp->mnt_stat;
1376 vsp = &mp->mnt_vstat;
1377
1378 /*
1379 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1380 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1381 * overrides MNT_WAIT.
1382 */
1383 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1384 (info->flags & MNT_WAIT)) &&
1385 (error = VFS_STATFS(mp, sp, info->p->p_ucred))) {
1386 return(0);
1387 }
1388 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1389
1390 if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1391 (info->flags & MNT_WAIT)) &&
1392 (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) {
1393 return(0);
1394 }
1395 vsp->f_flag = 0;
1396 if (mp->mnt_flag & MNT_RDONLY)
1397 vsp->f_flag |= ST_RDONLY;
1398 if (mp->mnt_flag & MNT_NOSUID)
1399 vsp->f_flag |= ST_NOSUID;
1400
1401 error = mount_path(info->p, mp, &fullpath, &freepath);
1402 if (error) {
1403 info->error = error;
1404 return(-1);
1405 }
1406 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1407 strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1408 kfree(freepath, M_TEMP);
1409
1410 error = copyout(sp, info->sfsp, sizeof(*sp));
1411 if (error == 0)
1412 error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1413 if (error) {
1414 info->error = error;
1415 return (-1);
1416 }
1417 ++info->sfsp;
1418 ++info->vsfsp;
1419 }
1420 info->count++;
1421 return(0);
1422}
1423
1424
1425/*
41c20dac
MD
1426 * fchdir_args(int fd)
1427 *
984263bc
MD
1428 * Change current working directory to a given file descriptor.
1429 */
984263bc
MD
1430/* ARGSUSED */
1431int
753fd850 1432sys_fchdir(struct fchdir_args *uap)
984263bc 1433{
dadab5e9
MD
1434 struct thread *td = curthread;
1435 struct proc *p = td->td_proc;
41c20dac 1436 struct filedesc *fdp = p->p_fd;
21739618 1437 struct vnode *vp, *ovp;
984263bc
MD
1438 struct mount *mp;
1439 struct file *fp;
28623bf9 1440 struct nchandle nch, onch, tnch;
984263bc
MD
1441 int error;
1442
5b287bba 1443 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
984263bc
MD
1444 return (error);
1445 vp = (struct vnode *)fp->f_data;
597aea93 1446 vref(vp);
ca466bae 1447 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
28623bf9 1448 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
984263bc
MD
1449 error = ENOTDIR;
1450 else
87de5057 1451 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
984263bc
MD
1452 if (error) {
1453 vput(vp);
5b287bba 1454 fdrop(fp);
984263bc
MD
1455 return (error);
1456 }
28623bf9 1457 cache_copy(&fp->f_nchandle, &nch);
1d505369
MD
1458
1459 /*
1460 * If the ncp has become a mount point, traverse through
1461 * the mount point.
1462 */
1463
28623bf9
MD
1464 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1465 (mp = cache_findmount(&nch)) != NULL
1d505369 1466 ) {
28623bf9 1467 error = nlookup_mp(mp, &tnch);
21739618 1468 if (error == 0) {
28623bf9 1469 cache_unlock(&tnch); /* leave ref intact */
21739618 1470 vput(vp);
28623bf9 1471 vp = tnch.ncp->nc_vp;
87de5057 1472 error = vget(vp, LK_SHARED);
21739618 1473 KKASSERT(error == 0);
28623bf9
MD
1474 cache_drop(&nch);
1475 nch = tnch;
21739618
MD
1476 }
1477 }
1478 if (error == 0) {
1479 ovp = fdp->fd_cdir;
28623bf9 1480 onch = fdp->fd_ncdir;
a11aaa81 1481 vn_unlock(vp); /* leave ref intact */
21739618 1482 fdp->fd_cdir = vp;
28623bf9
MD
1483 fdp->fd_ncdir = nch;
1484 cache_drop(&onch);
21739618
MD
1485 vrele(ovp);
1486 } else {
28623bf9 1487 cache_drop(&nch);
21739618
MD
1488 vput(vp);
1489 }
5b287bba 1490 fdrop(fp);
21739618 1491 return (error);
984263bc
MD
1492}
1493
9697c509 1494int
21739618 1495kern_chdir(struct nlookupdata *nd)
9697c509
DRJ
1496{
1497 struct thread *td = curthread;
1498 struct proc *p = td->td_proc;
1499 struct filedesc *fdp = p->p_fd;
21739618 1500 struct vnode *vp, *ovp;
28623bf9 1501 struct nchandle onch;
9697c509
DRJ
1502 int error;
1503
21739618 1504 if ((error = nlookup(nd)) != 0)
9697c509 1505 return (error);
28623bf9 1506 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
21739618 1507 return (ENOENT);
87de5057 1508 if ((error = vget(vp, LK_SHARED)) != 0)
21739618
MD
1509 return (error);
1510
1511 error = checkvp_chdir(vp, td);
a11aaa81 1512 vn_unlock(vp);
21739618
MD
1513 if (error == 0) {
1514 ovp = fdp->fd_cdir;
28623bf9
MD
1515 onch = fdp->fd_ncdir;
1516 cache_unlock(&nd->nl_nch); /* leave reference intact */
1517 fdp->fd_ncdir = nd->nl_nch;
21739618 1518 fdp->fd_cdir = vp;
28623bf9 1519 cache_drop(&onch);
21739618 1520 vrele(ovp);
28623bf9 1521 cache_zero(&nd->nl_nch);
21739618
MD
1522 } else {
1523 vrele(vp);
e24b948e 1524 }
e24b948e 1525 return (error);
9697c509
DRJ
1526}
1527
984263bc 1528/*
41c20dac
MD
1529 * chdir_args(char *path)
1530 *
984263bc
MD
1531 * Change current working directory (``.'').
1532 */
984263bc 1533int
753fd850 1534sys_chdir(struct chdir_args *uap)
984263bc 1535{
21739618 1536 struct nlookupdata nd;
9697c509 1537 int error;
984263bc 1538
21739618 1539 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e 1540 if (error == 0)
21739618 1541 error = kern_chdir(&nd);
fad57d0e 1542 nlookup_done(&nd);
9697c509 1543 return (error);
984263bc
MD
1544}
1545
1546/*
1547 * Helper function for raised chroot(2) security function: Refuse if
1548 * any filedescriptors are open directories.
1549 */
1550static int
c972a82f 1551chroot_refuse_vdir_fds(struct filedesc *fdp)
984263bc
MD
1552{
1553 struct vnode *vp;
1554 struct file *fp;
1555 int error;
1556 int fd;
1557
1558 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
5b287bba 1559 if ((error = holdvnode(fdp, fd, &fp)) != 0)
984263bc
MD
1560 continue;
1561 vp = (struct vnode *)fp->f_data;
5b287bba
MD
1562 if (vp->v_type != VDIR) {
1563 fdrop(fp);
984263bc 1564 continue;
5b287bba
MD
1565 }
1566 fdrop(fp);
984263bc
MD
1567 return(EPERM);
1568 }
1569 return (0);
1570}
1571
1572/*
1573 * This sysctl determines if we will allow a process to chroot(2) if it
1574 * has a directory open:
1575 * 0: disallowed for all processes.
1576 * 1: allowed for processes that were not already chroot(2)'ed.
1577 * 2: allowed for all processes.
1578 */
1579
1580static int chroot_allow_open_directories = 1;
1581
1582SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1583 &chroot_allow_open_directories, 0, "");
1584
1585/*
21739618
MD
1586 * chroot to the specified namecache entry. We obtain the vp from the
1587 * namecache data. The passed ncp must be locked and referenced and will
1588 * remain locked and referenced on return.
e24b948e 1589 */
53dd6631 1590int
28623bf9 1591kern_chroot(struct nchandle *nch)
e24b948e
MD
1592{
1593 struct thread *td = curthread;
1594 struct proc *p = td->td_proc;
1595 struct filedesc *fdp = p->p_fd;
21739618 1596 struct vnode *vp;
e24b948e
MD
1597 int error;
1598
1599 /*
ca3cd02d 1600 * Only privileged user can chroot
e24b948e 1601 */
3a591c90
MN
1602 error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0);
1603 if (error)
e24b948e
MD
1604 return (error);
1605
1606 /*
1607 * Disallow open directory descriptors (fchdir() breakouts).
1608 */
1609 if (chroot_allow_open_directories == 0 ||
1610 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1611 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1612 return (error);
1613 }
28623bf9 1614 if ((vp = nch->ncp->nc_vp) == NULL)
21739618
MD
1615 return (ENOENT);
1616
87de5057 1617 if ((error = vget(vp, LK_SHARED)) != 0)
21739618 1618 return (error);
e24b948e
MD
1619
1620 /*
1621 * Check the validity of vp as a directory to change to and
1622 * associate it with rdir/jdir.
1623 */
21739618 1624 error = checkvp_chdir(vp, td);
a11aaa81 1625 vn_unlock(vp); /* leave reference intact */
21739618 1626 if (error == 0) {
e24b948e 1627 vrele(fdp->fd_rdir);
21739618 1628 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
28623bf9
MD
1629 cache_drop(&fdp->fd_nrdir);
1630 cache_copy(nch, &fdp->fd_nrdir);
e24b948e
MD
1631 if (fdp->fd_jdir == NULL) {
1632 fdp->fd_jdir = vp;
597aea93 1633 vref(fdp->fd_jdir);
28623bf9 1634 cache_copy(nch, &fdp->fd_njdir);
e24b948e 1635 }
21739618
MD
1636 } else {
1637 vrele(vp);
e24b948e
MD
1638 }
1639 return (error);
1640}
1641
1642/*
41c20dac
MD
1643 * chroot_args(char *path)
1644 *
984263bc
MD
1645 * Change notion of root (``/'') directory.
1646 */
984263bc
MD
1647/* ARGSUSED */
1648int
753fd850 1649sys_chroot(struct chroot_args *uap)
984263bc 1650{
dadab5e9 1651 struct thread *td = curthread;
21739618 1652 struct nlookupdata nd;
e24b948e 1653 int error;
984263bc 1654
e24b948e 1655 KKASSERT(td->td_proc);
21739618 1656 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
53dd6631
JS
1657 if (error) {
1658 nlookup_done(&nd);
1659 return(error);
1660 }
3a907475 1661 nd.nl_flags |= NLC_EXEC;
53dd6631
JS
1662 error = nlookup(&nd);
1663 if (error == 0)
28623bf9 1664 error = kern_chroot(&nd.nl_nch);
fad57d0e 1665 nlookup_done(&nd);
53dd6631 1666 return(error);
984263bc
MD
1667}
1668
1669/*
e24b948e
MD
1670 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1671 * determine whether it is legal to chdir to the vnode. The vnode's state
1672 * is not changed by this call.
984263bc 1673 */
e24b948e
MD
1674int
1675checkvp_chdir(struct vnode *vp, struct thread *td)
984263bc 1676{
984263bc
MD
1677 int error;
1678
984263bc
MD
1679 if (vp->v_type != VDIR)
1680 error = ENOTDIR;
1681 else
87de5057 1682 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
984263bc
MD
1683 return (error);
1684}
1685
984263bc 1686int
fad57d0e 1687kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
984263bc 1688{
dadab5e9
MD
1689 struct thread *td = curthread;
1690 struct proc *p = td->td_proc;
fbfe4e7d 1691 struct lwp *lp = td->td_lwp;
41c20dac 1692 struct filedesc *fdp = p->p_fd;
9697c509 1693 int cmode, flags;
984263bc 1694 struct file *nfp;
fad57d0e
MD
1695 struct file *fp;
1696 struct vnode *vp;
984263bc
MD
1697 int type, indx, error;
1698 struct flock lf;
984263bc 1699
984263bc
MD
1700 if ((oflags & O_ACCMODE) == O_ACCMODE)
1701 return (EINVAL);
1702 flags = FFLAGS(oflags);
fad57d0e 1703 error = falloc(p, &nfp, NULL);
984263bc
MD
1704 if (error)
1705 return (error);
1706 fp = nfp;
3a907475 1707 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
fad57d0e 1708
984263bc 1709 /*
fad57d0e
MD
1710 * XXX p_dupfd is a real mess. It allows a device to return a
1711 * file descriptor to be duplicated rather then doing the open
1712 * itself.
984263bc 1713 */
fbfe4e7d 1714 lp->lwp_dupfd = -1;
21739618
MD
1715
1716 /*
fad57d0e
MD
1717 * Call vn_open() to do the lookup and assign the vnode to the
1718 * file pointer. vn_open() does not change the ref count on fp
1719 * and the vnode, on success, will be inherited by the file pointer
1720 * and unlocked.
21739618 1721 */
fad57d0e
MD
1722 nd->nl_flags |= NLC_LOCKVP;
1723 error = vn_open(nd, fp, flags, cmode);
1724 nlookup_done(nd);
984263bc
MD
1725 if (error) {
1726 /*
984263bc
MD
1727 * handle special fdopen() case. bleh. dupfdopen() is
1728 * responsible for dropping the old contents of ofiles[indx]
1729 * if it succeeds.
fad57d0e 1730 *
259b8ea0
MD
1731 * Note that fsetfd() will add a ref to fp which represents
1732 * the fd_files[] assignment. We must still drop our
1733 * reference.
984263bc 1734 */
fbfe4e7d 1735 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
259b8ea0
MD
1736 if (fdalloc(p, 0, &indx) == 0) {
1737 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
fad57d0e
MD
1738 if (error == 0) {
1739 *res = indx;
9f87144f 1740 fdrop(fp); /* our ref */
fad57d0e
MD
1741 return (0);
1742 }
259b8ea0 1743 fsetfd(p, NULL, indx);
fad57d0e 1744 }
984263bc 1745 }
9f87144f 1746 fdrop(fp); /* our ref */
984263bc
MD
1747 if (error == ERESTART)
1748 error = EINTR;
1749 return (error);
1750 }
fad57d0e
MD
1751
1752 /*
1753 * ref the vnode for ourselves so it can't be ripped out from under
1754 * is. XXX need an ND flag to request that the vnode be returned
1755 * anyway.
259b8ea0
MD
1756 *
1757 * Reserve a file descriptor but do not assign it until the open
1758 * succeeds.
fad57d0e
MD
1759 */
1760 vp = (struct vnode *)fp->f_data;
1761 vref(vp);
259b8ea0 1762 if ((error = fdalloc(p, 0, &indx)) != 0) {
9f87144f 1763 fdrop(fp);
fad57d0e
MD
1764 vrele(vp);
1765 return (error);
1766 }
1767
1768 /*
1769 * If no error occurs the vp will have been assigned to the file
1770 * pointer.
1771 */
fbfe4e7d 1772 lp->lwp_dupfd = 0;
984263bc 1773
984263bc
MD
1774 if (flags & (O_EXLOCK | O_SHLOCK)) {
1775 lf.l_whence = SEEK_SET;
1776 lf.l_start = 0;
1777 lf.l_len = 0;
1778 if (flags & O_EXLOCK)
1779 lf.l_type = F_WRLCK;
1780 else
1781 lf.l_type = F_RDLCK;
71c18fe3
MD
1782 if (flags & FNONBLOCK)
1783 type = 0;
1784 else
1785 type = F_WAIT;
fad57d0e 1786
984263bc
MD
1787 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1788 /*
259b8ea0
MD
1789 * lock request failed. Clean up the reserved
1790 * descriptor.
984263bc 1791 */
fad57d0e 1792 vrele(vp);
259b8ea0 1793 fsetfd(p, NULL, indx);
9f87144f 1794 fdrop(fp);
984263bc
MD
1795 return (error);
1796 }
984263bc
MD
1797 fp->f_flag |= FHASLOCK;
1798 }
7540ab49
MD
1799#if 0
1800 /*
1801 * Assert that all regular file vnodes were created with a object.
1802 */
1803 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1804 ("open: regular file has no backing object after vn_open"));
1805#endif
984263bc 1806
fad57d0e 1807 vrele(vp);
21739618
MD
1808
1809 /*
984263bc
MD
1810 * release our private reference, leaving the one associated with the
1811 * descriptor table intact.
1812 */
259b8ea0 1813 fsetfd(p, fp, indx);
9f87144f 1814 fdrop(fp);
9697c509 1815 *res = indx;
984263bc
MD
1816 return (0);
1817}
1818
984263bc 1819/*
9697c509 1820 * open_args(char *path, int flags, int mode)
41c20dac 1821 *
9697c509
DRJ
1822 * Check permissions, allocate an open file structure,
1823 * and call the device open routine if any.
984263bc 1824 */
984263bc 1825int
753fd850 1826sys_open(struct open_args *uap)
984263bc 1827{
fad57d0e 1828 struct nlookupdata nd;
9697c509
DRJ
1829 int error;
1830
85dbab7f 1831 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
fad57d0e
MD
1832 if (error == 0) {
1833 error = kern_open(&nd, uap->flags,
1834 uap->mode, &uap->sysmsg_result);
1835 }
1836 nlookup_done(&nd);
9697c509 1837 return (error);
984263bc 1838}
984263bc 1839
984263bc 1840int
0e9b9130 1841kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
984263bc 1842{
dadab5e9
MD
1843 struct thread *td = curthread;
1844 struct proc *p = td->td_proc;
41c20dac 1845 struct vnode *vp;
984263bc
MD
1846 struct vattr vattr;
1847 int error;
1848 int whiteout = 0;
984263bc 1849
dadab5e9
MD
1850 KKASSERT(p);
1851
fad57d0e
MD
1852 VATTR_NULL(&vattr);
1853 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
0e9b9130
MD
1854 vattr.va_rmajor = rmajor;
1855 vattr.va_rminor = rminor;
fad57d0e
MD
1856
1857 switch (mode & S_IFMT) {
1858 case S_IFMT: /* used by badsect to flag bad sectors */
3a591c90 1859 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0);
fad57d0e
MD
1860 vattr.va_type = VBAD;
1861 break;
1862 case S_IFCHR:
44a36778 1863 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
fad57d0e
MD
1864 vattr.va_type = VCHR;
1865 break;
1866 case S_IFBLK:
44a36778 1867 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
fad57d0e
MD
1868 vattr.va_type = VBLK;
1869 break;
1870 case S_IFWHT:
3a591c90 1871 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0);
fad57d0e
MD
1872 whiteout = 1;
1873 break;
44a36778 1874 case S_IFDIR: /* special directories support for HAMMER */
3a591c90 1875 error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0);
bed9e372
MD
1876 vattr.va_type = VDIR;
1877 break;
fad57d0e
MD
1878 default:
1879 error = EINVAL;
1880 break;
984263bc 1881 }
44a36778
MN
1882
1883 if (error)
1884 return (error);
1885
1886 bwillinode(1);
1887 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
1888 if ((error = nlookup(nd)) != 0)
1889 return (error);
1890 if (nd->nl_nch.ncp->nc_vp)
1891 return (EEXIST);
1892 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
1893 return (error);
1894
1895 if (whiteout) {
1896 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1897 nd->nl_cred, NAMEI_CREATE);
1898 } else {
1899 vp = NULL;
1900 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1901 &vp, nd->nl_cred, &vattr);
1902 if (error == 0)
1903 vput(vp);
984263bc 1904 }
9697c509
DRJ
1905 return (error);
1906}
1907
1908/*
1909 * mknod_args(char *path, int mode, int dev)
1910 *
1911 * Create a special file.
1912 */
1913int
753fd850 1914sys_mknod(struct mknod_args *uap)
9697c509 1915{
fad57d0e 1916 struct nlookupdata nd;
9697c509
DRJ
1917 int error;
1918
fad57d0e 1919 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
0e9b9130
MD
1920 if (error == 0) {
1921 error = kern_mknod(&nd, uap->mode,
1922 umajor(uap->dev), uminor(uap->dev));
1923 }
fad57d0e 1924 nlookup_done(&nd);
984263bc
MD
1925 return (error);
1926}
1927
984263bc 1928int
fad57d0e 1929kern_mkfifo(struct nlookupdata *nd, int mode)
984263bc 1930{
dadab5e9
MD
1931 struct thread *td = curthread;
1932 struct proc *p = td->td_proc;
984263bc 1933 struct vattr vattr;
fad57d0e 1934 struct vnode *vp;
984263bc 1935 int error;
984263bc 1936
c4df9635 1937 bwillinode(1);
fad57d0e 1938
5312fa43 1939 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e 1940 if ((error = nlookup(nd)) != 0)
984263bc 1941 return (error);
28623bf9 1942 if (nd->nl_nch.ncp->nc_vp)
984263bc 1943 return (EEXIST);
28623bf9 1944 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 1945 return (error);
fad57d0e 1946
984263bc
MD
1947 VATTR_NULL(&vattr);
1948 vattr.va_type = VFIFO;
136178b3 1949 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
fad57d0e 1950 vp = NULL;
5312fa43 1951 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
984263bc 1952 if (error == 0)
fad57d0e 1953 vput(vp);
136178b3
DRJ
1954 return (error);
1955}
1956
1957/*
1958 * mkfifo_args(char *path, int mode)
1959 *
1960 * Create a named pipe.
1961 */
1962int
753fd850 1963sys_mkfifo(struct mkfifo_args *uap)
136178b3 1964{
fad57d0e 1965 struct nlookupdata nd;
136178b3
DRJ
1966 int error;
1967
fad57d0e
MD
1968 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1969 if (error == 0)
1970 error = kern_mkfifo(&nd, uap->mode);
1971 nlookup_done(&nd);
984263bc
MD
1972 return (error);
1973}
1974
8c40f81e 1975static int hardlink_check_uid = 0;
43a0f7ae 1976SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
8c40f81e
MD
1977 &hardlink_check_uid, 0,
1978 "Unprivileged processes cannot create hard links to files owned by other "
1979 "users");
1980static int hardlink_check_gid = 0;
43a0f7ae 1981SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
8c40f81e
MD
1982 &hardlink_check_gid, 0,
1983 "Unprivileged processes cannot create hard links to files owned by other "
1984 "groups");
1985
1986static int
1987can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1988{
1989 struct vattr va;
1990 int error;
1991
1992 /*
1993 * Shortcut if disabled
1994 */
1995 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1996 return (0);
1997
1998 /*
f6df0641 1999 * Privileged user can always hardlink
8c40f81e 2000 */
f6df0641 2001 if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
8c40f81e
MD
2002 return (0);
2003
2004 /*
2005 * Otherwise only if the originating file is owned by the
2006 * same user or group. Note that any group is allowed if
2007 * the file is owned by the caller.
2008 */
87de5057 2009 error = VOP_GETATTR(vp, &va);
8c40f81e
MD
2010 if (error != 0)
2011 return (error);
2012
2013 if (hardlink_check_uid) {
2014 if (cred->cr_uid != va.va_uid)
2015 return (EPERM);
2016 }
2017
2018 if (hardlink_check_gid) {
2019 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2020 return (EPERM);
2021 }
2022
2023 return (0);
2024}
2025
984263bc 2026int
fad57d0e 2027kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
984263bc 2028{
dadab5e9 2029 struct thread *td = curthread;
41c20dac 2030 struct vnode *vp;
984263bc
MD
2031 int error;
2032
fad57d0e
MD
2033 /*
2034 * Lookup the source and obtained a locked vnode.
2035 *
3a907475
MD
2036 * You may only hardlink a file which you have write permission
2037 * on or which you own.
2038 *
fad57d0e
MD
2039 * XXX relookup on vget failure / race ?
2040 */
c4df9635 2041 bwillinode(1);
3a907475 2042 nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
fad57d0e 2043 if ((error = nlookup(nd)) != 0)
984263bc 2044 return (error);
28623bf9 2045 vp = nd->nl_nch.ncp->nc_vp;
fad57d0e 2046 KKASSERT(vp != NULL);
984263bc 2047 if (vp->v_type == VDIR)
fad57d0e 2048 return (EPERM); /* POSIX */
28623bf9 2049 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2050 return (error);
87de5057 2051 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
fad57d0e
MD
2052 return (error);
2053
2054 /*
2055 * Unlock the source so we can lookup the target without deadlocking
2056 * (XXX vp is locked already, possible other deadlock?). The target
2057 * must not exist.
2058 */
2059 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2060 nd->nl_flags &= ~NLC_NCPISLOCKED;
28623bf9 2061 cache_unlock(&nd->nl_nch);
fad57d0e 2062
5312fa43 2063 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e
MD
2064 if ((error = nlookup(linknd)) != 0) {
2065 vput(vp);
2066 return (error);
984263bc 2067 }
28623bf9 2068 if (linknd->nl_nch.ncp->nc_vp) {
fad57d0e
MD
2069 vput(vp);
2070 return (EEXIST);
2071 }
2072
2073 /*
2074 * Finally run the new API VOP.
2075 */
8c40f81e 2076 error = can_hardlink(vp, td, td->td_proc->p_ucred);
5312fa43
MD
2077 if (error == 0) {
2078 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2079 vp, linknd->nl_cred);
2080 }
fad57d0e 2081 vput(vp);
984263bc
MD
2082 return (error);
2083}
2084
2085/*
9697c509 2086 * link_args(char *path, char *link)
41c20dac 2087 *
9697c509 2088 * Make a hard file link.
984263bc 2089 */
984263bc 2090int
753fd850 2091sys_link(struct link_args *uap)
9697c509 2092{
fad57d0e 2093 struct nlookupdata nd, linknd;
9697c509
DRJ
2094 int error;
2095
fad57d0e
MD
2096 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2097 if (error == 0) {
2098 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2099 if (error == 0)
2100 error = kern_link(&nd, &linknd);
2101 nlookup_done(&linknd);
2102 }
2103 nlookup_done(&nd);
9697c509
DRJ
2104 return (error);
2105}
2106
2107int
fad57d0e 2108kern_symlink(struct nlookupdata *nd, char *path, int mode)
984263bc
MD
2109{
2110 struct vattr vattr;
fad57d0e 2111 struct vnode *vp;
dff430ab 2112 struct vnode *dvp;
984263bc 2113 int error;
984263bc 2114
c4df9635 2115 bwillinode(1);
5312fa43 2116 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e 2117 if ((error = nlookup(nd)) != 0)
9697c509 2118 return (error);
28623bf9 2119 if (nd->nl_nch.ncp->nc_vp)
9697c509 2120 return (EEXIST);
28623bf9 2121 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2122 return (error);
5312fa43 2123 dvp = nd->nl_dvp;
984263bc 2124 VATTR_NULL(&vattr);
fad57d0e 2125 vattr.va_mode = mode;
dff430ab 2126 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
984263bc 2127 if (error == 0)
fad57d0e 2128 vput(vp);
9697c509
DRJ
2129 return (error);
2130}
2131
2132/*
2133 * symlink(char *path, char *link)
2134 *
2135 * Make a symbolic link.
2136 */
2137int
753fd850 2138sys_symlink(struct symlink_args *uap)
9697c509
DRJ
2139{
2140 struct thread *td = curthread;
fad57d0e 2141 struct nlookupdata nd;
9697c509
DRJ
2142 char *path;
2143 int error;
fad57d0e 2144 int mode;
9697c509 2145
70aac194 2146 path = objcache_get(namei_oc, M_WAITOK);
9697c509 2147 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
e24b948e 2148 if (error == 0) {
fad57d0e
MD
2149 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2150 if (error == 0) {
2151 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2152 error = kern_symlink(&nd, path, mode);
2153 }
2154 nlookup_done(&nd);
e24b948e 2155 }
70aac194 2156 objcache_put(namei_oc, path);
984263bc
MD
2157 return (error);
2158}
2159
2160/*
41c20dac
MD
2161 * undelete_args(char *path)
2162 *
984263bc
MD
2163 * Delete a whiteout from the filesystem.
2164 */
2165/* ARGSUSED */
2166int
753fd850 2167sys_undelete(struct undelete_args *uap)
984263bc 2168{
fad57d0e 2169 struct nlookupdata nd;
984263bc 2170 int error;
984263bc 2171
ab2eb4eb 2172 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
c4df9635 2173 bwillinode(1);
5312fa43 2174 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
fad57d0e
MD
2175 if (error == 0)
2176 error = nlookup(&nd);
b8997912 2177 if (error == 0)
28623bf9 2178 error = ncp_writechk(&nd.nl_nch);
dff430ab 2179 if (error == 0) {
5312fa43
MD
2180 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2181 NAMEI_DELETE);
dff430ab 2182 }
fad57d0e 2183 nlookup_done(&nd);
984263bc
MD
2184 return (error);
2185}
2186
984263bc 2187int
fad57d0e 2188kern_unlink(struct nlookupdata *nd)
984263bc 2189{
984263bc 2190 int error;
984263bc 2191
c4df9635 2192 bwillinode(1);
5312fa43 2193 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
fad57d0e 2194 if ((error = nlookup(nd)) != 0)
984263bc 2195 return (error);
28623bf9 2196 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2197 return (error);
5312fa43 2198 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
984263bc
MD
2199 return (error);
2200}
2201
2202/*
9697c509 2203 * unlink_args(char *path)
41c20dac 2204 *
9697c509 2205 * Delete a name from the filesystem.
984263bc 2206 */
984263bc 2207int
753fd850 2208sys_unlink(struct unlink_args *uap)
9697c509 2209{
fad57d0e 2210 struct nlookupdata nd;
9697c509
DRJ
2211 int error;
2212
fad57d0e
MD
2213 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2214 if (error == 0)
2215 error = kern_unlink(&nd);
2216 nlookup_done(&nd);
9697c509
DRJ
2217 return (error);
2218}
2219
2220int
5a3fe67d 2221kern_lseek(int fd, off_t offset, int whence, off_t *res)
984263bc 2222{
dadab5e9
MD
2223 struct thread *td = curthread;
2224 struct proc *p = td->td_proc;
41c20dac 2225 struct file *fp;
116edfbf 2226 struct vnode *vp;
984263bc 2227 struct vattr vattr;
116edfbf 2228 off_t new_offset;
984263bc
MD
2229 int error;
2230
228b401d
MD
2231 fp = holdfp(p->p_fd, fd, -1);
2232 if (fp == NULL)
984263bc 2233 return (EBADF);
228b401d
MD
2234 if (fp->f_type != DTYPE_VNODE) {
2235 error = ESPIPE;
2236 goto done;
2237 }
116edfbf 2238 vp = (struct vnode *)fp->f_data;
228b401d 2239
9697c509 2240 switch (whence) {
984263bc 2241 case L_INCR:
116edfbf 2242 new_offset = fp->f_offset + offset;
228b401d 2243 error = 0;
984263bc
MD
2244 break;
2245 case L_XTND:
116edfbf
MD
2246 error = VOP_GETATTR(vp, &vattr);
2247 new_offset = offset + vattr.va_size;
984263bc
MD
2248 break;
2249 case L_SET:
116edfbf 2250 new_offset = offset;
228b401d 2251 error = 0;
984263bc
MD
2252 break;
2253 default:
116edfbf 2254 new_offset = 0;
228b401d
MD
2255 error = EINVAL;
2256 break;
984263bc 2257 }
116edfbf
MD
2258
2259 /*
2260 * Validate the seek position. Negative offsets are not allowed
58675cb0
MD
2261 * for regular files or directories.
2262 *
2263 * Normally we would also not want to allow negative offsets for
2264 * character and block-special devices. However kvm addresses
2265 * on 64 bit architectures might appear to be negative and must
2266 * be allowed.
116edfbf
MD
2267 */
2268 if (error == 0) {
2269 if (new_offset < 0 &&
58675cb0 2270 (vp->v_type == VREG || vp->v_type == VDIR)) {
116edfbf 2271 error = EINVAL;
b06b3b87
MD
2272 } else {
2273 fp->f_offset = new_offset;
116edfbf 2274 }
116edfbf 2275 }
9697c509 2276 *res = fp->f_offset;
228b401d
MD
2277done:
2278 fdrop(fp);
2279 return (error);
984263bc
MD
2280}
2281
984263bc 2282/*
9697c509 2283 * lseek_args(int fd, int pad, off_t offset, int whence)
41c20dac 2284 *
9697c509 2285 * Reposition read/write file offset.
984263bc 2286 */
984263bc 2287int
753fd850 2288sys_lseek(struct lseek_args *uap)
984263bc 2289{
984263bc
MD
2290 int error;
2291
9697c509 2292 error = kern_lseek(uap->fd, uap->offset, uap->whence,
116edfbf 2293 &uap->sysmsg_offset);
9697c509 2294
984263bc
MD
2295 return (error);
2296}
984263bc 2297
984263bc 2298int
fad57d0e 2299kern_access(struct nlookupdata *nd, int aflags)
984263bc 2300{
41c20dac 2301 struct vnode *vp;
984263bc 2302 int error, flags;
984263bc 2303
fad57d0e
MD
2304 if ((error = nlookup(nd)) != 0)
2305 return (error);
806dcf9a 2306retry:
28623bf9 2307 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 2308 if (error)
fad57d0e 2309 return (error);
984263bc
MD
2310
2311 /* Flags == 0 means only check for existence. */
9697c509 2312 if (aflags) {
984263bc 2313 flags = 0;
9697c509 2314 if (aflags & R_OK)
984263bc 2315 flags |= VREAD;
9697c509 2316 if (aflags & W_OK)
984263bc 2317 flags |= VWRITE;
9697c509 2318 if (aflags & X_OK)
984263bc 2319 flags |= VEXEC;
468bb1f9 2320 if ((flags & VWRITE) == 0 ||
28623bf9 2321 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
87de5057 2322 error = VOP_ACCESS(vp, flags, nd->nl_cred);
806dcf9a
MD
2323
2324 /*
2325 * If the file handle is stale we have to re-resolve the
2326 * entry. This is a hack at the moment.
2327 */
2328 if (error == ESTALE) {
5e1a76ea 2329 vput(vp);
28623bf9
MD
2330 cache_setunresolved(&nd->nl_nch);
2331 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
806dcf9a 2332 if (error == 0) {
806dcf9a
MD
2333 vp = NULL;
2334 goto retry;
2335 }
5e1a76ea 2336 return(error);
806dcf9a 2337 }
984263bc 2338 }
984263bc 2339 vput(vp);
984263bc
MD
2340 return (error);
2341}
2342
984263bc 2343/*
9697c509 2344 * access_args(char *path, int flags)
41c20dac 2345 *
9697c509 2346 * Check access permissions.
984263bc 2347 */
984263bc 2348int
753fd850 2349sys_access(struct access_args *uap)
984263bc 2350{
fad57d0e 2351 struct nlookupdata nd;
9697c509 2352 int error;
984263bc 2353
fad57d0e
MD
2354 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2355 if (error == 0)
2356 error = kern_access(&nd, uap->flags);
2357 nlookup_done(&nd);
984263bc
MD
2358 return (error);
2359}
2360
984263bc 2361int
21739618 2362kern_stat(struct nlookupdata *nd, struct stat *st)
984263bc 2363{
984263bc 2364 int error;
21739618
MD
2365 struct vnode *vp;
2366 thread_t td;
984263bc 2367
21739618 2368 if ((error = nlookup(nd)) != 0)
984263bc 2369 return (error);
806dcf9a 2370again:
28623bf9 2371 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
21739618
MD
2372 return (ENOENT);
2373
2374 td = curthread;
87de5057 2375 if ((error = vget(vp, LK_SHARED)) != 0)
21739618 2376 return (error);
87de5057 2377 error = vn_stat(vp, st, nd->nl_cred);
806dcf9a
MD
2378
2379 /*
2380 * If the file handle is stale we have to re-resolve the entry. This
2381 * is a hack at the moment.
2382 */
2383 if (error == ESTALE) {
5dc91765 2384 vput(vp);
28623bf9
MD
2385 cache_setunresolved(&nd->nl_nch);
2386 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
5dc91765 2387 if (error == 0)
806dcf9a 2388 goto again;
5dc91765
MD
2389 } else {
2390 vput(vp);
806dcf9a 2391 }
984263bc
MD
2392 return (error);
2393}
2394
2395/*
41c20dac
MD
2396 * stat_args(char *path, struct stat *ub)
2397 *
984263bc
MD
2398 * Get file status; this version follows links.
2399 */
984263bc 2400int
753fd850 2401sys_stat(struct stat_args *uap)
984263bc 2402{
21739618 2403 struct nlookupdata nd;
9697c509
DRJ
2404 struct stat st;
2405 int error;
984263bc 2406
21739618
MD
2407 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2408 if (error == 0) {
2409 error = kern_stat(&nd, &st);
2410 if (error == 0)
2411 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 2412 }
fad57d0e 2413 nlookup_done(&nd);
984263bc
MD
2414 return (error);
2415}
2416
2417/*
41c20dac
MD
2418 * lstat_args(char *path, struct stat *ub)
2419 *
984263bc
MD
2420 * Get file status; this version does not follow links.
2421 */
984263bc 2422int
753fd850 2423sys_lstat(struct lstat_args *uap)
984263bc 2424{
21739618 2425 struct nlookupdata nd;
9697c509
DRJ
2426 struct stat st;
2427 int error;
984263bc 2428
21739618
MD
2429 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2430 if (error == 0) {
2431 error = kern_stat(&nd, &st);
2432 if (error == 0)
2433 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 2434 }
fad57d0e 2435 nlookup_done(&nd);
984263bc
MD
2436 return (error);
2437}
2438
984263bc 2439/*
41c20dac
MD
2440 * pathconf_Args(char *path, int name)
2441 *
984263bc
MD
2442 * Get configurable pathname variables.
2443 */
984263bc
MD
2444/* ARGSUSED */
2445int
753fd850 2446sys_pathconf(struct pathconf_args *uap)
984263bc 2447{
fad57d0e
MD
2448 struct nlookupdata nd;
2449 struct vnode *vp;
984263bc 2450 int error;
984263bc 2451
fad57d0e 2452 vp = NULL;
ab2eb4eb 2453 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2454 if (error == 0)
2455 error = nlookup(&nd);
2456 if (error == 0)
28623bf9 2457 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
fad57d0e
MD
2458 nlookup_done(&nd);
2459 if (error == 0) {
a385a915 2460 error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg);
fad57d0e
MD
2461 vput(vp);
2462 }
984263bc
MD
2463 return (error);
2464}
2465
bbab27f6
DRJ
2466/*
2467 * XXX: daver
2468 * kern_readlink isn't properly split yet. There is a copyin burried
2469 * in VOP_READLINK().
2470 */
984263bc 2471int
fad57d0e 2472kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
984263bc 2473{
dadab5e9
MD
2474 struct thread *td = curthread;
2475 struct proc *p = td->td_proc;
41c20dac 2476 struct vnode *vp;
984263bc
MD
2477 struct iovec aiov;
2478 struct uio auio;
2479 int error;
984263bc 2480
fad57d0e
MD
2481 if ((error = nlookup(nd)) != 0)
2482 return (error);
28623bf9 2483 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 2484 if (error)
984263bc 2485 return (error);
fad57d0e 2486 if (vp->v_type != VLNK) {
984263bc 2487 error = EINVAL;
fad57d0e 2488 } else {
9697c509
DRJ
2489 aiov.iov_base = buf;
2490 aiov.iov_len = count;
984263bc
MD
2491 auio.uio_iov = &aiov;
2492 auio.uio_iovcnt = 1;
2493 auio.uio_offset = 0;
2494 auio.uio_rw = UIO_READ;
2495 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 2496 auio.uio_td = td;
9697c509 2497 auio.uio_resid = count;
984263bc
MD
2498 error = VOP_READLINK(vp, &auio, p->p_ucred);
2499 }
2500 vput(vp);
9697c509
DRJ
2501 *res = count - auio.uio_resid;
2502 return (error);
2503}
2504
2505/*
2506 * readlink_args(char *path, char *buf, int count)
2507 *
2508 * Return target name of a symbolic link.
2509 */
2510int
753fd850 2511sys_readlink(struct readlink_args *uap)
9697c509 2512{
fad57d0e 2513 struct nlookupdata nd;
9697c509
DRJ
2514 int error;
2515
fad57d0e
MD
2516 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2517 if (error == 0) {
2518 error = kern_readlink(&nd, uap->buf, uap->count,
2519 &uap->sysmsg_result);
2520 }
2521 nlookup_done(&nd);
984263bc
MD
2522 return (error);
2523}
2524
2525static int
41c20dac 2526setfflags(struct vnode *vp, int flags)
984263bc 2527{
dadab5e9
MD
2528 struct thread *td = curthread;
2529 struct proc *p = td->td_proc;
984263bc
MD
2530 int error;
2531 struct vattr vattr;
2532
2533 /*
2534 * Prevent non-root users from setting flags on devices. When
2535 * a device is reused, users can retain ownership of the device
2536 * if they are allowed to set flags and programs assume that
2537 * chown can't fail when done as root.
2538 */
2539 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
6dc79895 2540 ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
984263bc
MD
2541 return (error);
2542
186fccd6
MD
2543 /*
2544 * note: vget is required for any operation that might mod the vnode
2545 * so VINACTIVE is properly cleared.
2546 */
87de5057 2547 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2548 VATTR_NULL(&vattr);
2549 vattr.va_flags = flags;
87de5057 2550 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2551 vput(vp);
2552 }
984263bc
MD
2553 return (error);
2554}
2555
2556/*
41c20dac
MD
2557 * chflags(char *path, int flags)
2558 *
984263bc
MD
2559 * Change flags of a file given a path name.
2560 */
984263bc
MD
2561/* ARGSUSED */
2562int
753fd850 2563sys_chflags(struct chflags_args *uap)
984263bc 2564{
fad57d0e
MD
2565 struct nlookupdata nd;
2566 struct vnode *vp;
984263bc 2567 int error;
984263bc 2568
fad57d0e 2569 vp = NULL;
ab2eb4eb 2570 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2571 if (error == 0)
2572 error = nlookup(&nd);
2573 if (error == 0)
28623bf9 2574 error = ncp_writechk(&nd.nl_nch);
72310cfb 2575 if (error == 0)
28623bf9 2576 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
fad57d0e
MD
2577 nlookup_done(&nd);
2578 if (error == 0) {
ab2eb4eb 2579 error = setfflags(vp, uap->flags);
fad57d0e
MD
2580 vrele(vp);
2581 }
2582 return (error);
984263bc
MD
2583}
2584
2585/*
91fdc001
PA
2586 * lchflags(char *path, int flags)
2587 *
2588 * Change flags of a file given a path name, but don't follow symlinks.
2589 */
2590/* ARGSUSED */
2591int
2592sys_lchflags(struct lchflags_args *uap)
2593{
2594 struct nlookupdata nd;
2595 struct vnode *vp;
2596 int error;
2597
2598 vp = NULL;
2599 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
91fdc001
PA
2600 if (error == 0)
2601 error = nlookup(&nd);
2602 if (error == 0)
2603 error = ncp_writechk(&nd.nl_nch);
2604 if (error == 0)
2605 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
2606 nlookup_done(&nd);
2607 if (error == 0) {
2608 error = setfflags(vp, uap->flags);
2609 vrele(vp);
2610 }
2611 return (error);
2612}
2613
2614/*
41c20dac
MD
2615 * fchflags_args(int fd, int flags)
2616 *
984263bc
MD
2617 * Change flags of a file given a file descriptor.
2618 */
984263bc
MD
2619/* ARGSUSED */
2620int
753fd850 2621sys_fchflags(struct fchflags_args *uap)
984263bc 2622{
dadab5e9
MD
2623 struct thread *td = curthread;
2624 struct proc *p = td->td_proc;
984263bc
MD
2625 struct file *fp;
2626 int error;
2627
5b287bba 2628 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2629 return (error);
28623bf9
MD
2630 if (fp->f_nchandle.ncp)
2631 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2632 if (error == 0)
2633 error = setfflags((struct vnode *) fp->f_data, uap->flags);
5b287bba
MD
2634 fdrop(fp);
2635 return (error);
984263bc
MD
2636}
2637
2638static int
41c20dac 2639setfmode(struct vnode *vp, int mode)
984263bc 2640{
dadab5e9
MD
2641 struct thread *td = curthread;
2642 struct proc *p = td->td_proc;
984263bc
MD
2643 int error;
2644 struct vattr vattr;
2645
186fccd6
MD
2646 /*
2647 * note: vget is required for any operation that might mod the vnode
2648 * so VINACTIVE is properly cleared.
2649 */
87de5057 2650 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2651 VATTR_NULL(&vattr);
2652 vattr.va_mode = mode & ALLPERMS;
87de5057 2653 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2654 vput(vp);
2655 }
984263bc
MD
2656 return error;
2657}
2658
9697c509 2659int
fad57d0e 2660kern_chmod(struct nlookupdata *nd, int mode)
9697c509 2661{
fad57d0e 2662 struct vnode *vp;
9697c509
DRJ
2663 int error;
2664
fad57d0e 2665 if ((error = nlookup(nd)) != 0)
9697c509 2666 return (error);
28623bf9 2667 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e 2668 return (error);
28623bf9 2669 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
72310cfb 2670 error = setfmode(vp, mode);
fad57d0e
MD
2671 vrele(vp);
2672 return (error);
9697c509
DRJ
2673}
2674
984263bc 2675/*
41c20dac
MD
2676 * chmod_args(char *path, int mode)
2677 *
984263bc
MD
2678 * Change mode of a file given path name.
2679 */
984263bc
MD
2680/* ARGSUSED */
2681int
753fd850 2682sys_chmod(struct chmod_args *uap)
984263bc 2683{
fad57d0e 2684 struct nlookupdata nd;
9697c509 2685 int error;
984263bc 2686
fad57d0e
MD
2687 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2688 if (error == 0)
2689 error = kern_chmod(&nd, uap->mode);
2690 nlookup_done(&nd);
9697c509 2691 return (error);
984263bc
MD
2692}
2693
2694/*
41c20dac
MD
2695 * lchmod_args(char *path, int mode)
2696 *
984263bc
MD
2697 * Change mode of a file given path name (don't follow links.)
2698 */
984263bc
MD
2699/* ARGSUSED */
2700int
753fd850 2701sys_lchmod(struct lchmod_args *uap)
984263bc 2702{
fad57d0e 2703 struct nlookupdata nd;
984263bc 2704 int error;
984263bc 2705
fad57d0e
MD
2706 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2707 if (error == 0)
2708 error = kern_chmod(&nd, uap->mode);
2709 nlookup_done(&nd);
2710 return (error);
984263bc
MD
2711}
2712
2713/*
41c20dac
MD
2714 * fchmod_args(int fd, int mode)
2715 *
984263bc
MD
2716 * Change mode of a file given a file descriptor.
2717 */
984263bc
MD
2718/* ARGSUSED */
2719int
753fd850 2720sys_fchmod(struct fchmod_args *uap)
984263bc 2721{
dadab5e9
MD
2722 struct thread *td = curthread;
2723 struct proc *p = td->td_proc;
984263bc
MD
2724 struct file *fp;
2725 int error;
2726
5b287bba 2727 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2728 return (error);
28623bf9
MD
2729 if (fp->f_nchandle.ncp)
2730 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2731 if (error == 0)
2732 error = setfmode((struct vnode *)fp->f_data, uap->mode);
5b287bba
MD
2733 fdrop(fp);
2734 return (error);
984263bc
MD
2735}
2736
2737static int
41c20dac 2738setfown(struct vnode *vp, uid_t uid, gid_t gid)
984263bc 2739{
dadab5e9
MD
2740 struct thread *td = curthread;
2741 struct proc *p = td->td_proc;
984263bc
MD
2742 int error;
2743 struct vattr vattr;
2744
186fccd6
MD
2745 /*
2746 * note: vget is required for any operation that might mod the vnode
2747 * so VINACTIVE is properly cleared.
2748 */
87de5057 2749 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2750 VATTR_NULL(&vattr);
2751 vattr.va_uid = uid;
2752 vattr.va_gid = gid;
87de5057 2753 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2754 vput(vp);
2755 }
984263bc
MD
2756 return error;
2757}
2758
9697c509 2759int
fad57d0e 2760kern_chown(struct nlookupdata *nd, int uid, int gid)
9697c509 2761{
fad57d0e 2762 struct vnode *vp;
9697c509
DRJ
2763 int error;
2764
fad57d0e 2765 if ((error = nlookup(nd)) != 0)
9697c509 2766 return (error);
28623bf9 2767 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e 2768 return (error);
28623bf9 2769 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
72310cfb 2770 error = setfown(vp, uid, gid);
fad57d0e 2771 vrele(vp);
9697c509
DRJ
2772 return (error);
2773}
2774
984263bc 2775/*
41c20dac
MD
2776 * chown(char *path, int uid, int gid)
2777 *
984263bc
MD
2778 * Set ownership given a path name.
2779 */
984263bc 2780int
753fd850 2781sys_chown(struct chown_args *uap)
984263bc 2782{
fad57d0e 2783 struct nlookupdata nd;
9697c509
DRJ
2784 int error;
2785
fad57d0e
MD
2786 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2787 if (error == 0)
2788 error = kern_chown(&nd, uap->uid, uap->gid);
2789 nlookup_done(&nd);
984263bc
MD
2790 return (error);
2791}
2792
2793/*
41c20dac
MD
2794 * lchown_args(char *path, int uid, int gid)
2795 *
984263bc
MD
2796 * Set ownership given a path name, do not cross symlinks.
2797 */
984263bc 2798int
753fd850 2799sys_lchown(struct lchown_args *uap)
984263bc 2800{
fad57d0e 2801 struct nlookupdata nd;
984263bc 2802 int error;
9697c509 2803
fad57d0e
MD
2804 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2805 if (error == 0)
2806 error = kern_chown(&nd, uap->uid, uap->gid);
2807 nlookup_done(&nd);
984263bc
MD
2808 return (error);
2809}
2810
2811/*
41c20dac
MD
2812 * fchown_args(int fd, int uid, int gid)
2813 *
984263bc
MD
2814 * Set ownership given a file descriptor.
2815 */
984263bc
MD
2816/* ARGSUSED */
2817int
753fd850 2818sys_fchown(struct fchown_args *uap)
984263bc 2819{
dadab5e9
MD
2820 struct thread *td = curthread;
2821 struct proc *p = td->td_proc;
984263bc
MD
2822 struct file *fp;
2823 int error;
2824
5b287bba 2825 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2826 return (error);
28623bf9
MD
2827 if (fp->f_nchandle.ncp)
2828 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2829 if (error == 0)
2830 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
5b287bba
MD
2831 fdrop(fp);
2832 return (error);
984263bc
MD
2833}
2834
2835static int
9697c509 2836getutimes(const struct timeval *tvp, struct timespec *tsp)
984263bc
MD
2837{
2838 struct timeval tv[2];
984263bc 2839
9697c509 2840 if (tvp == NULL) {
984263bc
MD
2841 microtime(&tv[0]);
2842 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2843 tsp[1] = tsp[0];
2844 } else {
9697c509
DRJ
2845 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2846 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
984263bc
MD
2847 }
2848 return 0;
2849}
2850
2851static int
3a907475
MD
2852setutimes(struct vnode *vp, struct vattr *vattr,
2853 const struct timespec *ts, int nullflag)
984263bc 2854{
dadab5e9
MD
2855 struct thread *td = curthread;
2856 struct proc *p = td->td_proc;
984263bc 2857 int error;
984263bc 2858
3a907475
MD
2859 VATTR_NULL(vattr);
2860 vattr->va_atime = ts[0];
2861 vattr->va_mtime = ts[1];
2862 if (nullflag)
2863 vattr->va_vaflags |= VA_UTIMES_NULL;
2864 error = VOP_SETATTR(vp, vattr, p->p_ucred);
2865
984263bc
MD
2866 return error;
2867}
2868
9697c509 2869int
fad57d0e 2870kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
9697c509
DRJ
2871{
2872 struct timespec ts[2];
fad57d0e 2873 struct vnode *vp;
3a907475 2874 struct vattr vattr;
9697c509
DRJ
2875 int error;
2876
fad57d0e 2877 if ((error = getutimes(tptr, ts)) != 0)
9697c509 2878 return (error);
3a907475
MD
2879
2880 /*
2881 * NOTE: utimes() succeeds for the owner even if the file
2882 * is not user-writable.
2883 */
2884 nd->nl_flags |= NLC_OWN | NLC_WRITE;
2885
fad57d0e 2886 if ((error = nlookup(nd)) != 0)
9697c509 2887 return (error);
28623bf9 2888 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2889 return (error);
28623bf9 2890 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e 2891 return (error);
ee89633d
MD
2892
2893 /*
3a907475
MD
2894 * note: vget is required for any operation that might mod the vnode
2895 * so VINACTIVE is properly cleared.
ee89633d 2896 */
3a907475
MD
2897 if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
2898 error = vget(vp, LK_EXCLUSIVE);
2899 if (error == 0) {
2900 error = setutimes(vp, &vattr, ts, (tptr == NULL));
2901 vput(vp);
2902 }
918101d3 2903 }
fad57d0e 2904 vrele(vp);
9697c509
DRJ
2905 return (error);
2906}
2907
984263bc 2908/*
41c20dac
MD
2909 * utimes_args(char *path, struct timeval *tptr)
2910 *
984263bc
MD
2911 * Set the access and modification times of a file.
2912 */
984263bc 2913int
753fd850 2914sys_utimes(struct utimes_args *uap)
984263bc 2915{
9697c509 2916 struct timeval tv[2];
fad57d0e 2917 struct nlookupdata nd;
9697c509
DRJ
2918 int error;
2919
2920 if (uap->tptr) {
2921 error = copyin(uap->tptr, tv, sizeof(tv));
2922 if (error)
2923 return (error);
2924 }
fad57d0e
MD
2925 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2926 if (error == 0)
2927 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2928 nlookup_done(&nd);
984263bc
MD
2929 return (error);
2930}
2931
2932/*
41c20dac
MD
2933 * lutimes_args(char *path, struct timeval *tptr)
2934 *
984263bc
MD
2935 * Set the access and modification times of a file.
2936 */
984263bc 2937int
753fd850 2938sys_lutimes(struct lutimes_args *uap)
984263bc 2939{
9697c509 2940 struct timeval tv[2];
fad57d0e 2941 struct nlookupdata nd;
9697c509
DRJ
2942 int error;
2943
2944 if (uap->tptr) {
2945 error = copyin(uap->tptr, tv, sizeof(tv));
2946 if (error)
2947 return (error);
2948 }
fad57d0e
MD
2949 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2950 if (error == 0)
2951 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2952 nlookup_done(&nd);
984263bc
MD
2953 return (error);
2954}
2955
3a907475
MD
2956/*
2957 * Set utimes on a file descriptor. The creds used to open the
2958 * file are used to determine whether the operation is allowed
2959 * or not.
2960 */
984263bc 2961int
9697c509 2962kern_futimes(int fd, struct timeval *tptr)
984263bc 2963{
dadab5e9
MD
2964 struct thread *td = curthread;
2965 struct proc *p = td->td_proc;
984263bc
MD
2966 struct timespec ts[2];
2967 struct file *fp;
3a907475
MD
2968 struct vnode *vp;
2969 struct vattr vattr;
984263bc
MD
2970 int error;
2971
9697c509
DRJ
2972 error = getutimes(tptr, ts);
2973 if (error)
984263bc 2974 return (error);
5b287bba 2975 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
984263bc 2976 return (error);
28623bf9
MD
2977 if (fp->f_nchandle.ncp)
2978 error = ncp_writechk(&fp->f_nchandle);
3a907475
MD
2979 if (error == 0) {
2980 vp = fp->f_data;
2981 error = vget(vp, LK_EXCLUSIVE);
2982 if (error == 0) {
2983 error = VOP_GETATTR(vp, &vattr);
2984 if (error == 0) {
2985 error = naccess_va(&vattr, NLC_OWN | NLC_WRITE,
2986 fp->f_cred);
2987 }
2988 if (error == 0) {
2989 error = setutimes(vp, &vattr, ts,
2990 (tptr == NULL));
2991 }
2992 vput(vp);
2993 }
2994 }
5b287bba 2995 fdrop(fp);
9697c509 2996 return (error);
984263bc
MD
2997}
2998
2999/*
9697c509 3000 * futimes_args(int fd, struct timeval *tptr)
41c20dac 3001 *
9697c509 3002 * Set the access and modification times of a file.
984263bc 3003 */
984263bc 3004int
753fd850 3005sys_futimes(struct futimes_args *uap)
9697c509
DRJ
3006{
3007 struct timeval tv[2];
3008 int error;
3009
3010 if (uap->tptr) {
3011 error = copyin(uap->tptr, tv, sizeof(tv));
3012 if (error)
3013 return (error);
3014 }
3015
b651c485 3016 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
9697c509
DRJ
3017
3018 return (error);
3019}
3020
3021int
fad57d0e 3022kern_truncate(struct nlookupdata *nd, off_t length)
984263bc 3023{
41c20dac 3024 struct vnode *vp;
984263bc
MD
3025 struct vattr vattr;
3026 int error;
984263bc 3027
9697c509 3028 if (length < 0)
984263bc 3029 return(EINVAL);
3a907475 3030 nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
fad57d0e 3031 if ((error = nlookup(nd)) != 0)
984263bc 3032 return (error);
28623bf9 3033 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 3034 return (error);
28623bf9 3035 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e 3036 return (error);
ca466bae 3037 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
fad57d0e
MD
3038 vrele(vp);
3039 return (error);
3040 }
3041 if (vp->v_type == VDIR) {
984263bc 3042 error = EISDIR;
3a907475 3043 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) {
984263bc 3044 VATTR_NULL(&vattr);
9697c509 3045 vattr.va_size = length;
87de5057 3046 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
984263bc
MD
3047 }
3048 vput(vp);
3049 return (error);
3050}
3051
9697c509
DRJ
3052/*
3053 * truncate(char *path, int pad, off_t length)
3054 *
3055 * Truncate a file given its path name.
3056 */
3057int
753fd850 3058sys_truncate(struct truncate_args *uap)
9697c509 3059{
fad57d0e 3060 struct nlookupdata nd;
9697c509
DRJ
3061 int error;
3062
fad57d0e
MD
3063 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3064 if (error == 0)
3065 error = kern_truncate(&nd, uap->length);
3066 nlookup_done(&nd);
9697c509
DRJ
3067 return error;
3068}
3069
984263bc 3070int
8f6f8622 3071kern_ftruncate(int fd, off_t length)
984263bc 3072{
dadab5e9
MD
3073 struct thread *td = curthread;
3074 struct proc *p = td->td_proc;
984263bc
MD
3075 struct vattr vattr;
3076 struct vnode *vp;
3077 struct file *fp;
3078 int error;
3079
8f6f8622 3080 if (length < 0)
984263bc 3081 return(EINVAL);
5b287bba 3082 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
984263bc 3083 return (error);
28623bf9
MD
3084 if (fp->f_nchandle.ncp) {
3085 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
3086 if (error)
3087 goto done;
3088 }
5b287bba
MD
3089 if ((fp->f_flag & FWRITE) == 0) {
3090 error = EINVAL;
3091 goto done;
3092 }
3a907475
MD
3093 if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */
3094 error = EINVAL;
3095 goto done;
3096 }
984263bc 3097 vp = (struct vnode *)fp->f_data;
ca466bae 3098 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5b287bba 3099 if (vp->v_type == VDIR) {
984263bc 3100 error = EISDIR;
468bb1f9 3101 } else if ((error = vn_writechk(vp, NULL)) == 0) {
984263bc 3102 VATTR_NULL(&vattr);
8f6f8622 3103 vattr.va_size = length;
87de5057 3104 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
984263bc 3105 }
a11aaa81 3106 vn_unlock(vp);
5b287bba
MD
3107done:
3108 fdrop(fp);
984263bc
MD
3109 return (error);
3110}
3111
984263bc 3112/*
8f6f8622 3113 * ftruncate_args(int fd, int pad, off_t length)
41c20dac 3114 *
8f6f8622 3115 * Truncate a file given a file descriptor.
984263bc 3116 */
984263bc 3117int
753fd850 3118sys_ftruncate(struct ftruncate_args *uap)
984263bc 3119{
8f6f8622 3120 int error;
984263bc 3121
8f6f8622
DRJ
3122 error = kern_ftruncate(uap->fd, uap->length);
3123
3124 return (error);
984263bc
MD
3125}
3126
984263bc 3127/*
41c20dac
MD
3128 * fsync(int fd)
3129 *
984263bc
MD
3130 * Sync an open file.
3131 */
984263bc
MD
3132/* ARGSUSED */
3133int
753fd850 3134sys_fsync(struct fsync_args *uap)
984263bc 3135{
dadab5e9
MD
3136 struct thread *td = curthread;
3137 struct proc *p = td->td_proc;
41c20dac 3138 struct vnode *vp;
984263bc
MD
3139 struct file *fp;
3140 vm_object_t obj;
3141 int error;
3142
5b287bba 3143 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc
MD
3144 return (error);
3145 vp = (struct vnode *)fp->f_data;
ca466bae 3146 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7540ab49 3147 if ((obj = vp->v_object) != NULL)
984263bc 3148 vm_object_page_clean(obj, 0, 0, 0);
408357d8
MD
3149 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
3150 error = buf_fsync(vp);
a11aaa81 3151 vn_unlock(vp);
5b287bba 3152 fdrop(fp);
984263bc
MD
3153 return (error);
3154}
3155
984263bc 3156int
fad57d0e 3157kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
984263bc 3158{
28623bf9
MD
3159 struct nchandle fnchd;
3160 struct nchandle tnchd;
fad57d0e 3161 struct namecache *ncp;
dff430ab
MD
3162 struct vnode *fdvp;
3163 struct vnode *tdvp;
fad57d0e 3164 struct mount *mp;
984263bc
MD
3165 int error;
3166
c4df9635 3167 bwillinode(1);
3a907475 3168 fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
fad57d0e 3169 if ((error = nlookup(fromnd)) != 0)
984263bc 3170 return (error);
28623bf9 3171 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
fad57d0e 3172 return (ENOENT);
28623bf9
MD
3173 fnchd.mount = fromnd->nl_nch.mount;
3174 cache_hold(&fnchd);
fad57d0e
MD
3175
3176 /*
28623bf9 3177 * unlock the source nch so we can lookup the target nch without
fad57d0e
MD
3178 * deadlocking. The target may or may not exist so we do not check
3179 * for a target vp like kern_mkdir() and other creation functions do.
3180 *
3181 * The source and target directories are ref'd and rechecked after
3182 * everything is relocked to determine if the source or target file
3183 * has been renamed.
3184 */
3185 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
3186 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
28623bf9 3187 cache_unlock(&fromnd->nl_nch);
fad57d0e 3188
3a907475 3189 tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
fad57d0e 3190 if ((error = nlookup(tond)) != 0) {
28623bf9 3191 cache_drop(&fnchd);
fad57d0e
MD
3192 return (error);
3193 }
28623bf9
MD
3194 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3195 cache_drop(&fnchd);
fad57d0e
MD
3196 return (ENOENT);
3197 }
28623bf9
MD
3198 tnchd.mount = tond->nl_nch.mount;
3199 cache_hold(&tnchd);
fad57d0e
MD
3200
3201 /*
3202 * If the source and target are the same there is nothing to do
3203 */
28623bf9
MD
3204 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3205 cache_drop(&fnchd);
3206 cache_drop(&tnchd);
fad57d0e
MD
3207 return (0);
3208 }
3209
3210 /*
1d505369
MD
3211 * Mount points cannot be renamed or overwritten
3212 */
28623bf9
MD
3213 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3214 NCF_ISMOUNTPT
1d505369 3215 ) {
28623bf9
MD
3216 cache_drop(&fnchd);
3217 cache_drop(&tnchd);
1d505369
MD
3218 return (EINVAL);
3219 }
3220
3221 /*
aa40af33
MD
3222 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3223 * may have become invalid while it was unlocked, nc_vp and nc_mount
3224 * could be NULL.
fad57d0e 3225 */
28623bf9
MD
3226 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3227 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3228 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3229 cache_lock(&fromnd->nl_nch);