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