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