* Implement new system calls in the kernel: statvfs(), fstatvfs(),
[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 $
d9fad06e 40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.128 2008/06/01 19:27:35 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
1319/*
41c20dac
MD
1320 * fchdir_args(int fd)
1321 *
984263bc
MD
1322 * Change current working directory to a given file descriptor.
1323 */
984263bc
MD
1324/* ARGSUSED */
1325int
753fd850 1326sys_fchdir(struct fchdir_args *uap)
984263bc 1327{
dadab5e9
MD
1328 struct thread *td = curthread;
1329 struct proc *p = td->td_proc;
41c20dac 1330 struct filedesc *fdp = p->p_fd;
21739618 1331 struct vnode *vp, *ovp;
984263bc
MD
1332 struct mount *mp;
1333 struct file *fp;
28623bf9 1334 struct nchandle nch, onch, tnch;
984263bc
MD
1335 int error;
1336
5b287bba 1337 if ((error = holdvnode(fdp, uap->fd, &fp)) != 0)
984263bc
MD
1338 return (error);
1339 vp = (struct vnode *)fp->f_data;
597aea93 1340 vref(vp);
ca466bae 1341 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
28623bf9 1342 if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL)
984263bc
MD
1343 error = ENOTDIR;
1344 else
87de5057 1345 error = VOP_ACCESS(vp, VEXEC, p->p_ucred);
984263bc
MD
1346 if (error) {
1347 vput(vp);
5b287bba 1348 fdrop(fp);
984263bc
MD
1349 return (error);
1350 }
28623bf9 1351 cache_copy(&fp->f_nchandle, &nch);
1d505369
MD
1352
1353 /*
1354 * If the ncp has become a mount point, traverse through
1355 * the mount point.
1356 */
1357
28623bf9
MD
1358 while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1359 (mp = cache_findmount(&nch)) != NULL
1d505369 1360 ) {
28623bf9 1361 error = nlookup_mp(mp, &tnch);
21739618 1362 if (error == 0) {
28623bf9 1363 cache_unlock(&tnch); /* leave ref intact */
21739618 1364 vput(vp);
28623bf9 1365 vp = tnch.ncp->nc_vp;
87de5057 1366 error = vget(vp, LK_SHARED);
21739618 1367 KKASSERT(error == 0);
28623bf9
MD
1368 cache_drop(&nch);
1369 nch = tnch;
21739618
MD
1370 }
1371 }
1372 if (error == 0) {
1373 ovp = fdp->fd_cdir;
28623bf9 1374 onch = fdp->fd_ncdir;
a11aaa81 1375 vn_unlock(vp); /* leave ref intact */
21739618 1376 fdp->fd_cdir = vp;
28623bf9
MD
1377 fdp->fd_ncdir = nch;
1378 cache_drop(&onch);
21739618
MD
1379 vrele(ovp);
1380 } else {
28623bf9 1381 cache_drop(&nch);
21739618
MD
1382 vput(vp);
1383 }
5b287bba 1384 fdrop(fp);
21739618 1385 return (error);
984263bc
MD
1386}
1387
9697c509 1388int
21739618 1389kern_chdir(struct nlookupdata *nd)
9697c509
DRJ
1390{
1391 struct thread *td = curthread;
1392 struct proc *p = td->td_proc;
1393 struct filedesc *fdp = p->p_fd;
21739618 1394 struct vnode *vp, *ovp;
28623bf9 1395 struct nchandle onch;
9697c509
DRJ
1396 int error;
1397
21739618 1398 if ((error = nlookup(nd)) != 0)
9697c509 1399 return (error);
28623bf9 1400 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
21739618 1401 return (ENOENT);
87de5057 1402 if ((error = vget(vp, LK_SHARED)) != 0)
21739618
MD
1403 return (error);
1404
1405 error = checkvp_chdir(vp, td);
a11aaa81 1406 vn_unlock(vp);
21739618
MD
1407 if (error == 0) {
1408 ovp = fdp->fd_cdir;
28623bf9
MD
1409 onch = fdp->fd_ncdir;
1410 cache_unlock(&nd->nl_nch); /* leave reference intact */
1411 fdp->fd_ncdir = nd->nl_nch;
21739618 1412 fdp->fd_cdir = vp;
28623bf9 1413 cache_drop(&onch);
21739618 1414 vrele(ovp);
28623bf9 1415 cache_zero(&nd->nl_nch);
21739618
MD
1416 } else {
1417 vrele(vp);
e24b948e 1418 }
e24b948e 1419 return (error);
9697c509
DRJ
1420}
1421
984263bc 1422/*
41c20dac
MD
1423 * chdir_args(char *path)
1424 *
984263bc
MD
1425 * Change current working directory (``.'').
1426 */
984263bc 1427int
753fd850 1428sys_chdir(struct chdir_args *uap)
984263bc 1429{
21739618 1430 struct nlookupdata nd;
9697c509 1431 int error;
984263bc 1432
21739618 1433 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e 1434 if (error == 0)
21739618 1435 error = kern_chdir(&nd);
fad57d0e 1436 nlookup_done(&nd);
9697c509 1437 return (error);
984263bc
MD
1438}
1439
1440/*
1441 * Helper function for raised chroot(2) security function: Refuse if
1442 * any filedescriptors are open directories.
1443 */
1444static int
c972a82f 1445chroot_refuse_vdir_fds(struct filedesc *fdp)
984263bc
MD
1446{
1447 struct vnode *vp;
1448 struct file *fp;
1449 int error;
1450 int fd;
1451
1452 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
5b287bba 1453 if ((error = holdvnode(fdp, fd, &fp)) != 0)
984263bc
MD
1454 continue;
1455 vp = (struct vnode *)fp->f_data;
5b287bba
MD
1456 if (vp->v_type != VDIR) {
1457 fdrop(fp);
984263bc 1458 continue;
5b287bba
MD
1459 }
1460 fdrop(fp);
984263bc
MD
1461 return(EPERM);
1462 }
1463 return (0);
1464}
1465
1466/*
1467 * This sysctl determines if we will allow a process to chroot(2) if it
1468 * has a directory open:
1469 * 0: disallowed for all processes.
1470 * 1: allowed for processes that were not already chroot(2)'ed.
1471 * 2: allowed for all processes.
1472 */
1473
1474static int chroot_allow_open_directories = 1;
1475
1476SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1477 &chroot_allow_open_directories, 0, "");
1478
e24b948e 1479/*
21739618
MD
1480 * chroot to the specified namecache entry. We obtain the vp from the
1481 * namecache data. The passed ncp must be locked and referenced and will
1482 * remain locked and referenced on return.
e24b948e 1483 */
53dd6631 1484int
28623bf9 1485kern_chroot(struct nchandle *nch)
e24b948e
MD
1486{
1487 struct thread *td = curthread;
1488 struct proc *p = td->td_proc;
1489 struct filedesc *fdp = p->p_fd;
21739618 1490 struct vnode *vp;
e24b948e
MD
1491 int error;
1492
1493 /*
1494 * Only root can chroot
1495 */
1496 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1497 return (error);
1498
1499 /*
1500 * Disallow open directory descriptors (fchdir() breakouts).
1501 */
1502 if (chroot_allow_open_directories == 0 ||
1503 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1504 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1505 return (error);
1506 }
28623bf9 1507 if ((vp = nch->ncp->nc_vp) == NULL)
21739618
MD
1508 return (ENOENT);
1509
87de5057 1510 if ((error = vget(vp, LK_SHARED)) != 0)
21739618 1511 return (error);
e24b948e
MD
1512
1513 /*
1514 * Check the validity of vp as a directory to change to and
1515 * associate it with rdir/jdir.
1516 */
21739618 1517 error = checkvp_chdir(vp, td);
a11aaa81 1518 vn_unlock(vp); /* leave reference intact */
21739618 1519 if (error == 0) {
e24b948e 1520 vrele(fdp->fd_rdir);
21739618 1521 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
28623bf9
MD
1522 cache_drop(&fdp->fd_nrdir);
1523 cache_copy(nch, &fdp->fd_nrdir);
e24b948e
MD
1524 if (fdp->fd_jdir == NULL) {
1525 fdp->fd_jdir = vp;
597aea93 1526 vref(fdp->fd_jdir);
28623bf9 1527 cache_copy(nch, &fdp->fd_njdir);
e24b948e 1528 }
21739618
MD
1529 } else {
1530 vrele(vp);
e24b948e
MD
1531 }
1532 return (error);
1533}
1534
984263bc 1535/*
41c20dac
MD
1536 * chroot_args(char *path)
1537 *
984263bc
MD
1538 * Change notion of root (``/'') directory.
1539 */
984263bc
MD
1540/* ARGSUSED */
1541int
753fd850 1542sys_chroot(struct chroot_args *uap)
984263bc 1543{
dadab5e9 1544 struct thread *td = curthread;
21739618 1545 struct nlookupdata nd;
e24b948e 1546 int error;
984263bc 1547
e24b948e 1548 KKASSERT(td->td_proc);
21739618 1549 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
53dd6631
JS
1550 if (error) {
1551 nlookup_done(&nd);
1552 return(error);
1553 }
1554 error = nlookup(&nd);
1555 if (error == 0)
28623bf9 1556 error = kern_chroot(&nd.nl_nch);
fad57d0e 1557 nlookup_done(&nd);
53dd6631 1558 return(error);
984263bc
MD
1559}
1560
1561/*
e24b948e
MD
1562 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1563 * determine whether it is legal to chdir to the vnode. The vnode's state
1564 * is not changed by this call.
984263bc 1565 */
e24b948e
MD
1566int
1567checkvp_chdir(struct vnode *vp, struct thread *td)
984263bc 1568{
984263bc
MD
1569 int error;
1570
984263bc
MD
1571 if (vp->v_type != VDIR)
1572 error = ENOTDIR;
1573 else
87de5057 1574 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred);
984263bc
MD
1575 return (error);
1576}
1577
984263bc 1578int
fad57d0e 1579kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
984263bc 1580{
dadab5e9
MD
1581 struct thread *td = curthread;
1582 struct proc *p = td->td_proc;
fbfe4e7d 1583 struct lwp *lp = td->td_lwp;
41c20dac 1584 struct filedesc *fdp = p->p_fd;
9697c509 1585 int cmode, flags;
984263bc 1586 struct file *nfp;
fad57d0e
MD
1587 struct file *fp;
1588 struct vnode *vp;
984263bc
MD
1589 int type, indx, error;
1590 struct flock lf;
984263bc 1591
984263bc
MD
1592 if ((oflags & O_ACCMODE) == O_ACCMODE)
1593 return (EINVAL);
1594 flags = FFLAGS(oflags);
fad57d0e 1595 error = falloc(p, &nfp, NULL);
984263bc
MD
1596 if (error)
1597 return (error);
1598 fp = nfp;
9697c509 1599 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
fad57d0e 1600
984263bc 1601 /*
fad57d0e
MD
1602 * XXX p_dupfd is a real mess. It allows a device to return a
1603 * file descriptor to be duplicated rather then doing the open
1604 * itself.
984263bc 1605 */
fbfe4e7d 1606 lp->lwp_dupfd = -1;
21739618
MD
1607
1608 /*
fad57d0e
MD
1609 * Call vn_open() to do the lookup and assign the vnode to the
1610 * file pointer. vn_open() does not change the ref count on fp
1611 * and the vnode, on success, will be inherited by the file pointer
1612 * and unlocked.
21739618 1613 */
fad57d0e
MD
1614 nd->nl_flags |= NLC_LOCKVP;
1615 error = vn_open(nd, fp, flags, cmode);
1616 nlookup_done(nd);
984263bc 1617 if (error) {
984263bc
MD
1618 /*
1619 * handle special fdopen() case. bleh. dupfdopen() is
1620 * responsible for dropping the old contents of ofiles[indx]
1621 * if it succeeds.
fad57d0e 1622 *
259b8ea0
MD
1623 * Note that fsetfd() will add a ref to fp which represents
1624 * the fd_files[] assignment. We must still drop our
1625 * reference.
984263bc 1626 */
fbfe4e7d 1627 if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) {
259b8ea0
MD
1628 if (fdalloc(p, 0, &indx) == 0) {
1629 error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error);
fad57d0e
MD
1630 if (error == 0) {
1631 *res = indx;
9f87144f 1632 fdrop(fp); /* our ref */
fad57d0e
MD
1633 return (0);
1634 }
259b8ea0 1635 fsetfd(p, NULL, indx);
fad57d0e 1636 }
984263bc 1637 }
9f87144f 1638 fdrop(fp); /* our ref */
984263bc
MD
1639 if (error == ERESTART)
1640 error = EINTR;
1641 return (error);
1642 }
fad57d0e
MD
1643
1644 /*
1645 * ref the vnode for ourselves so it can't be ripped out from under
1646 * is. XXX need an ND flag to request that the vnode be returned
1647 * anyway.
259b8ea0
MD
1648 *
1649 * Reserve a file descriptor but do not assign it until the open
1650 * succeeds.
fad57d0e
MD
1651 */
1652 vp = (struct vnode *)fp->f_data;
1653 vref(vp);
259b8ea0 1654 if ((error = fdalloc(p, 0, &indx)) != 0) {
9f87144f 1655 fdrop(fp);
fad57d0e
MD
1656 vrele(vp);
1657 return (error);
1658 }
1659
1660 /*
1661 * If no error occurs the vp will have been assigned to the file
1662 * pointer.
1663 */
fbfe4e7d 1664 lp->lwp_dupfd = 0;
984263bc 1665
984263bc
MD
1666 if (flags & (O_EXLOCK | O_SHLOCK)) {
1667 lf.l_whence = SEEK_SET;
1668 lf.l_start = 0;
1669 lf.l_len = 0;
1670 if (flags & O_EXLOCK)
1671 lf.l_type = F_WRLCK;
1672 else
1673 lf.l_type = F_RDLCK;
71c18fe3
MD
1674 if (flags & FNONBLOCK)
1675 type = 0;
1676 else
1677 type = F_WAIT;
fad57d0e 1678
984263bc
MD
1679 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1680 /*
259b8ea0
MD
1681 * lock request failed. Clean up the reserved
1682 * descriptor.
984263bc 1683 */
fad57d0e 1684 vrele(vp);
259b8ea0 1685 fsetfd(p, NULL, indx);
9f87144f 1686 fdrop(fp);
984263bc
MD
1687 return (error);
1688 }
984263bc
MD
1689 fp->f_flag |= FHASLOCK;
1690 }
7540ab49
MD
1691#if 0
1692 /*
1693 * Assert that all regular file vnodes were created with a object.
1694 */
1695 KASSERT(vp->v_type != VREG || vp->v_object != NULL,
1696 ("open: regular file has no backing object after vn_open"));
1697#endif
984263bc 1698
fad57d0e 1699 vrele(vp);
21739618 1700
984263bc
MD
1701 /*
1702 * release our private reference, leaving the one associated with the
1703 * descriptor table intact.
1704 */
259b8ea0 1705 fsetfd(p, fp, indx);
9f87144f 1706 fdrop(fp);
9697c509 1707 *res = indx;
984263bc
MD
1708 return (0);
1709}
1710
984263bc 1711/*
9697c509 1712 * open_args(char *path, int flags, int mode)
41c20dac 1713 *
9697c509
DRJ
1714 * Check permissions, allocate an open file structure,
1715 * and call the device open routine if any.
984263bc 1716 */
984263bc 1717int
753fd850 1718sys_open(struct open_args *uap)
984263bc 1719{
fad57d0e 1720 struct nlookupdata nd;
9697c509
DRJ
1721 int error;
1722
fad57d0e
MD
1723 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1724 if (error == 0) {
1725 error = kern_open(&nd, uap->flags,
1726 uap->mode, &uap->sysmsg_result);
1727 }
1728 nlookup_done(&nd);
9697c509 1729 return (error);
984263bc 1730}
984263bc 1731
984263bc 1732int
0e9b9130 1733kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
984263bc 1734{
dadab5e9
MD
1735 struct thread *td = curthread;
1736 struct proc *p = td->td_proc;
41c20dac 1737 struct vnode *vp;
984263bc
MD
1738 struct vattr vattr;
1739 int error;
1740 int whiteout = 0;
984263bc 1741
dadab5e9
MD
1742 KKASSERT(p);
1743
9697c509 1744 switch (mode & S_IFMT) {
984263bc
MD
1745 case S_IFCHR:
1746 case S_IFBLK:
dadab5e9 1747 error = suser(td);
984263bc
MD
1748 break;
1749 default:
dadab5e9 1750 error = suser_cred(p->p_ucred, PRISON_ROOT);
984263bc
MD
1751 break;
1752 }
1753 if (error)
1754 return (error);
fad57d0e 1755
984263bc 1756 bwillwrite();
5312fa43 1757 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e 1758 if ((error = nlookup(nd)) != 0)
984263bc 1759 return (error);
28623bf9 1760 if (nd->nl_nch.ncp->nc_vp)
fad57d0e 1761 return (EEXIST);
28623bf9 1762 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 1763 return (error);
984263bc 1764
fad57d0e
MD
1765 VATTR_NULL(&vattr);
1766 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
0e9b9130
MD
1767 vattr.va_rmajor = rmajor;
1768 vattr.va_rminor = rminor;
fad57d0e
MD
1769 whiteout = 0;
1770
1771 switch (mode & S_IFMT) {
1772 case S_IFMT: /* used by badsect to flag bad sectors */
1773 vattr.va_type = VBAD;
1774 break;
1775 case S_IFCHR:
1776 vattr.va_type = VCHR;
1777 break;
1778 case S_IFBLK:
1779 vattr.va_type = VBLK;
1780 break;
1781 case S_IFWHT:
1782 whiteout = 1;
1783 break;
1784 default:
1785 error = EINVAL;
1786 break;
984263bc 1787 }
9697c509 1788 if (error == 0) {
fad57d0e 1789 if (whiteout) {
5312fa43
MD
1790 error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
1791 nd->nl_cred, NAMEI_CREATE);
fad57d0e
MD
1792 } else {
1793 vp = NULL;
5312fa43
MD
1794 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
1795 &vp, nd->nl_cred, &vattr);
984263bc 1796 if (error == 0)
fad57d0e 1797 vput(vp);
984263bc 1798 }
984263bc 1799 }
9697c509
DRJ
1800 return (error);
1801}
1802
1803/*
1804 * mknod_args(char *path, int mode, int dev)
1805 *
1806 * Create a special file.
1807 */
1808int
753fd850 1809sys_mknod(struct mknod_args *uap)
9697c509 1810{
fad57d0e 1811 struct nlookupdata nd;
9697c509
DRJ
1812 int error;
1813
fad57d0e 1814 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
0e9b9130
MD
1815 if (error == 0) {
1816 error = kern_mknod(&nd, uap->mode,
1817 umajor(uap->dev), uminor(uap->dev));
1818 }
fad57d0e 1819 nlookup_done(&nd);
984263bc
MD
1820 return (error);
1821}
1822
984263bc 1823int
fad57d0e 1824kern_mkfifo(struct nlookupdata *nd, int mode)
984263bc 1825{
dadab5e9
MD
1826 struct thread *td = curthread;
1827 struct proc *p = td->td_proc;
984263bc 1828 struct vattr vattr;
fad57d0e 1829 struct vnode *vp;
984263bc 1830 int error;
984263bc
MD
1831
1832 bwillwrite();
fad57d0e 1833
5312fa43 1834 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e 1835 if ((error = nlookup(nd)) != 0)
984263bc 1836 return (error);
28623bf9 1837 if (nd->nl_nch.ncp->nc_vp)
984263bc 1838 return (EEXIST);
28623bf9 1839 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 1840 return (error);
fad57d0e 1841
984263bc
MD
1842 VATTR_NULL(&vattr);
1843 vattr.va_type = VFIFO;
136178b3 1844 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
fad57d0e 1845 vp = NULL;
5312fa43 1846 error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
984263bc 1847 if (error == 0)
fad57d0e 1848 vput(vp);
136178b3
DRJ
1849 return (error);
1850}
1851
1852/*
1853 * mkfifo_args(char *path, int mode)
1854 *
1855 * Create a named pipe.
1856 */
1857int
753fd850 1858sys_mkfifo(struct mkfifo_args *uap)
136178b3 1859{
fad57d0e 1860 struct nlookupdata nd;
136178b3
DRJ
1861 int error;
1862
fad57d0e
MD
1863 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1864 if (error == 0)
1865 error = kern_mkfifo(&nd, uap->mode);
1866 nlookup_done(&nd);
984263bc
MD
1867 return (error);
1868}
1869
8c40f81e 1870static int hardlink_check_uid = 0;
43a0f7ae 1871SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
8c40f81e
MD
1872 &hardlink_check_uid, 0,
1873 "Unprivileged processes cannot create hard links to files owned by other "
1874 "users");
1875static int hardlink_check_gid = 0;
43a0f7ae 1876SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
8c40f81e
MD
1877 &hardlink_check_gid, 0,
1878 "Unprivileged processes cannot create hard links to files owned by other "
1879 "groups");
1880
1881static int
1882can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
1883{
1884 struct vattr va;
1885 int error;
1886
1887 /*
1888 * Shortcut if disabled
1889 */
1890 if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
1891 return (0);
1892
1893 /*
1894 * root cred can always hardlink
1895 */
1896 if (suser_cred(cred, PRISON_ROOT) == 0)
1897 return (0);
1898
1899 /*
1900 * Otherwise only if the originating file is owned by the
1901 * same user or group. Note that any group is allowed if
1902 * the file is owned by the caller.
1903 */
87de5057 1904 error = VOP_GETATTR(vp, &va);
8c40f81e
MD
1905 if (error != 0)
1906 return (error);
1907
1908 if (hardlink_check_uid) {
1909 if (cred->cr_uid != va.va_uid)
1910 return (EPERM);
1911 }
1912
1913 if (hardlink_check_gid) {
1914 if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
1915 return (EPERM);
1916 }
1917
1918 return (0);
1919}
1920
984263bc 1921int
fad57d0e 1922kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
984263bc 1923{
dadab5e9 1924 struct thread *td = curthread;
41c20dac 1925 struct vnode *vp;
984263bc
MD
1926 int error;
1927
fad57d0e
MD
1928 /*
1929 * Lookup the source and obtained a locked vnode.
1930 *
1931 * XXX relookup on vget failure / race ?
1932 */
984263bc 1933 bwillwrite();
fad57d0e 1934 if ((error = nlookup(nd)) != 0)
984263bc 1935 return (error);
28623bf9 1936 vp = nd->nl_nch.ncp->nc_vp;
fad57d0e 1937 KKASSERT(vp != NULL);
984263bc 1938 if (vp->v_type == VDIR)
fad57d0e 1939 return (EPERM); /* POSIX */
28623bf9 1940 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 1941 return (error);
87de5057 1942 if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
fad57d0e
MD
1943 return (error);
1944
1945 /*
1946 * Unlock the source so we can lookup the target without deadlocking
1947 * (XXX vp is locked already, possible other deadlock?). The target
1948 * must not exist.
1949 */
1950 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1951 nd->nl_flags &= ~NLC_NCPISLOCKED;
28623bf9 1952 cache_unlock(&nd->nl_nch);
fad57d0e 1953
5312fa43 1954 linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e
MD
1955 if ((error = nlookup(linknd)) != 0) {
1956 vput(vp);
1957 return (error);
984263bc 1958 }
28623bf9 1959 if (linknd->nl_nch.ncp->nc_vp) {
fad57d0e
MD
1960 vput(vp);
1961 return (EEXIST);
1962 }
1963
1964 /*
1965 * Finally run the new API VOP.
1966 */
8c40f81e 1967 error = can_hardlink(vp, td, td->td_proc->p_ucred);
5312fa43
MD
1968 if (error == 0) {
1969 error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
1970 vp, linknd->nl_cred);
1971 }
fad57d0e 1972 vput(vp);
984263bc
MD
1973 return (error);
1974}
1975
1976/*
9697c509 1977 * link_args(char *path, char *link)
41c20dac 1978 *
9697c509 1979 * Make a hard file link.
984263bc 1980 */
984263bc 1981int
753fd850 1982sys_link(struct link_args *uap)
9697c509 1983{
fad57d0e 1984 struct nlookupdata nd, linknd;
9697c509
DRJ
1985 int error;
1986
fad57d0e
MD
1987 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1988 if (error == 0) {
1989 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1990 if (error == 0)
1991 error = kern_link(&nd, &linknd);
1992 nlookup_done(&linknd);
1993 }
1994 nlookup_done(&nd);
9697c509
DRJ
1995 return (error);
1996}
1997
1998int
fad57d0e 1999kern_symlink(struct nlookupdata *nd, char *path, int mode)
984263bc
MD
2000{
2001 struct vattr vattr;
fad57d0e 2002 struct vnode *vp;
dff430ab 2003 struct vnode *dvp;
984263bc 2004 int error;
984263bc 2005
984263bc 2006 bwillwrite();
5312fa43 2007 nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e 2008 if ((error = nlookup(nd)) != 0)
9697c509 2009 return (error);
28623bf9 2010 if (nd->nl_nch.ncp->nc_vp)
9697c509 2011 return (EEXIST);
28623bf9 2012 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2013 return (error);
5312fa43 2014 dvp = nd->nl_dvp;
984263bc 2015 VATTR_NULL(&vattr);
fad57d0e 2016 vattr.va_mode = mode;
dff430ab 2017 error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
984263bc 2018 if (error == 0)
fad57d0e 2019 vput(vp);
9697c509
DRJ
2020 return (error);
2021}
2022
2023/*
2024 * symlink(char *path, char *link)
2025 *
2026 * Make a symbolic link.
2027 */
2028int
753fd850 2029sys_symlink(struct symlink_args *uap)
9697c509
DRJ
2030{
2031 struct thread *td = curthread;
fad57d0e 2032 struct nlookupdata nd;
9697c509
DRJ
2033 char *path;
2034 int error;
fad57d0e 2035 int mode;
9697c509 2036
70aac194 2037 path = objcache_get(namei_oc, M_WAITOK);
9697c509 2038 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
e24b948e 2039 if (error == 0) {
fad57d0e
MD
2040 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2041 if (error == 0) {
2042 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2043 error = kern_symlink(&nd, path, mode);
2044 }
2045 nlookup_done(&nd);
e24b948e 2046 }
70aac194 2047 objcache_put(namei_oc, path);
984263bc
MD
2048 return (error);
2049}
2050
2051/*
41c20dac
MD
2052 * undelete_args(char *path)
2053 *
984263bc
MD
2054 * Delete a whiteout from the filesystem.
2055 */
2056/* ARGSUSED */
2057int
753fd850 2058sys_undelete(struct undelete_args *uap)
984263bc 2059{
fad57d0e 2060 struct nlookupdata nd;
984263bc 2061 int error;
984263bc 2062
ab2eb4eb 2063 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
984263bc 2064 bwillwrite();
5312fa43 2065 nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
fad57d0e
MD
2066 if (error == 0)
2067 error = nlookup(&nd);
72310cfb 2068 if (error == 0)
28623bf9 2069 error = ncp_writechk(&nd.nl_nch);
dff430ab 2070 if (error == 0) {
5312fa43
MD
2071 error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2072 NAMEI_DELETE);
dff430ab 2073 }
fad57d0e 2074 nlookup_done(&nd);
984263bc
MD
2075 return (error);
2076}
2077
984263bc 2078int
fad57d0e 2079kern_unlink(struct nlookupdata *nd)
984263bc 2080{
984263bc 2081 int error;
984263bc
MD
2082
2083 bwillwrite();
5312fa43 2084 nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
fad57d0e 2085 if ((error = nlookup(nd)) != 0)
984263bc 2086 return (error);
28623bf9 2087 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2088 return (error);
5312fa43 2089 error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
984263bc
MD
2090 return (error);
2091}
2092
2093/*
9697c509 2094 * unlink_args(char *path)
41c20dac 2095 *
9697c509 2096 * Delete a name from the filesystem.
984263bc 2097 */
984263bc 2098int
753fd850 2099sys_unlink(struct unlink_args *uap)
9697c509 2100{
fad57d0e 2101 struct nlookupdata nd;
9697c509
DRJ
2102 int error;
2103
fad57d0e
MD
2104 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2105 if (error == 0)
2106 error = kern_unlink(&nd);
2107 nlookup_done(&nd);
9697c509
DRJ
2108 return (error);
2109}
2110
2111int
5a3fe67d 2112kern_lseek(int fd, off_t offset, int whence, off_t *res)
984263bc 2113{
dadab5e9
MD
2114 struct thread *td = curthread;
2115 struct proc *p = td->td_proc;
41c20dac 2116 struct file *fp;
984263bc
MD
2117 struct vattr vattr;
2118 int error;
2119
228b401d
MD
2120 fp = holdfp(p->p_fd, fd, -1);
2121 if (fp == NULL)
984263bc 2122 return (EBADF);
228b401d
MD
2123 if (fp->f_type != DTYPE_VNODE) {
2124 error = ESPIPE;
2125 goto done;
2126 }
2127
9697c509 2128 switch (whence) {
984263bc 2129 case L_INCR:
9697c509 2130 fp->f_offset += offset;
228b401d 2131 error = 0;
984263bc
MD
2132 break;
2133 case L_XTND:
228b401d
MD
2134 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr);
2135 if (error == 0)
2136 fp->f_offset = offset + vattr.va_size;
984263bc
MD
2137 break;
2138 case L_SET:
9697c509 2139 fp->f_offset = offset;
228b401d 2140 error = 0;
984263bc
MD
2141 break;
2142 default:
228b401d
MD
2143 error = EINVAL;
2144 break;
984263bc 2145 }
9697c509 2146 *res = fp->f_offset;
228b401d
MD
2147done:
2148 fdrop(fp);
2149 return (error);
984263bc
MD
2150}
2151
984263bc 2152/*
9697c509 2153 * lseek_args(int fd, int pad, off_t offset, int whence)
41c20dac 2154 *
9697c509 2155 * Reposition read/write file offset.
984263bc 2156 */
984263bc 2157int
753fd850 2158sys_lseek(struct lseek_args *uap)
984263bc 2159{
984263bc
MD
2160 int error;
2161
9697c509 2162 error = kern_lseek(uap->fd, uap->offset, uap->whence,
5a3fe67d 2163 &uap->sysmsg_offset);
9697c509 2164
984263bc
MD
2165 return (error);
2166}
984263bc 2167
984263bc 2168int
fad57d0e 2169kern_access(struct nlookupdata *nd, int aflags)
984263bc 2170{
41c20dac 2171 struct vnode *vp;
984263bc 2172 int error, flags;
984263bc 2173
fad57d0e
MD
2174 if ((error = nlookup(nd)) != 0)
2175 return (error);
806dcf9a 2176retry:
28623bf9 2177 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 2178 if (error)
fad57d0e 2179 return (error);
984263bc
MD
2180
2181 /* Flags == 0 means only check for existence. */
9697c509 2182 if (aflags) {
984263bc 2183 flags = 0;
9697c509 2184 if (aflags & R_OK)
984263bc 2185 flags |= VREAD;
9697c509 2186 if (aflags & W_OK)
984263bc 2187 flags |= VWRITE;
9697c509 2188 if (aflags & X_OK)
984263bc 2189 flags |= VEXEC;
468bb1f9 2190 if ((flags & VWRITE) == 0 ||
28623bf9 2191 (error = vn_writechk(vp, &nd->nl_nch)) == 0)
87de5057 2192 error = VOP_ACCESS(vp, flags, nd->nl_cred);
806dcf9a
MD
2193
2194 /*
2195 * If the file handle is stale we have to re-resolve the
2196 * entry. This is a hack at the moment.
2197 */
2198 if (error == ESTALE) {
5e1a76ea 2199 vput(vp);
28623bf9
MD
2200 cache_setunresolved(&nd->nl_nch);
2201 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
806dcf9a 2202 if (error == 0) {
806dcf9a
MD
2203 vp = NULL;
2204 goto retry;
2205 }
5e1a76ea 2206 return(error);
806dcf9a 2207 }
984263bc 2208 }
984263bc 2209 vput(vp);
984263bc
MD
2210 return (error);
2211}
2212
984263bc 2213/*
9697c509 2214 * access_args(char *path, int flags)
41c20dac 2215 *
9697c509 2216 * Check access permissions.
984263bc 2217 */
984263bc 2218int
753fd850 2219sys_access(struct access_args *uap)
984263bc 2220{
fad57d0e 2221 struct nlookupdata nd;
9697c509 2222 int error;
984263bc 2223
fad57d0e
MD
2224 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2225 if (error == 0)
2226 error = kern_access(&nd, uap->flags);
2227 nlookup_done(&nd);
984263bc
MD
2228 return (error);
2229}
2230
984263bc 2231int
21739618 2232kern_stat(struct nlookupdata *nd, struct stat *st)
984263bc 2233{
984263bc 2234 int error;
21739618
MD
2235 struct vnode *vp;
2236 thread_t td;
984263bc 2237
21739618 2238 if ((error = nlookup(nd)) != 0)
984263bc 2239 return (error);
806dcf9a 2240again:
28623bf9 2241 if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
21739618
MD
2242 return (ENOENT);
2243
2244 td = curthread;
87de5057 2245 if ((error = vget(vp, LK_SHARED)) != 0)
21739618 2246 return (error);
87de5057 2247 error = vn_stat(vp, st, nd->nl_cred);
806dcf9a
MD
2248
2249 /*
2250 * If the file handle is stale we have to re-resolve the entry. This
2251 * is a hack at the moment.
2252 */
2253 if (error == ESTALE) {
5dc91765 2254 vput(vp);
28623bf9
MD
2255 cache_setunresolved(&nd->nl_nch);
2256 error = cache_resolve(&nd->nl_nch, nd->nl_cred);
5dc91765 2257 if (error == 0)
806dcf9a 2258 goto again;
5dc91765
MD
2259 } else {
2260 vput(vp);
806dcf9a 2261 }
984263bc
MD
2262 return (error);
2263}
2264
984263bc 2265/*
41c20dac
MD
2266 * stat_args(char *path, struct stat *ub)
2267 *
984263bc
MD
2268 * Get file status; this version follows links.
2269 */
984263bc 2270int
753fd850 2271sys_stat(struct stat_args *uap)
984263bc 2272{
21739618 2273 struct nlookupdata nd;
9697c509
DRJ
2274 struct stat st;
2275 int error;
984263bc 2276
21739618
MD
2277 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2278 if (error == 0) {
2279 error = kern_stat(&nd, &st);
2280 if (error == 0)
2281 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 2282 }
fad57d0e 2283 nlookup_done(&nd);
984263bc
MD
2284 return (error);
2285}
2286
2287/*
41c20dac
MD
2288 * lstat_args(char *path, struct stat *ub)
2289 *
984263bc
MD
2290 * Get file status; this version does not follow links.
2291 */
984263bc 2292int
753fd850 2293sys_lstat(struct lstat_args *uap)
984263bc 2294{
21739618 2295 struct nlookupdata nd;
9697c509
DRJ
2296 struct stat st;
2297 int error;
984263bc 2298
21739618
MD
2299 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2300 if (error == 0) {
2301 error = kern_stat(&nd, &st);
2302 if (error == 0)
2303 error = copyout(&st, uap->ub, sizeof(*uap->ub));
21739618 2304 }
fad57d0e 2305 nlookup_done(&nd);
984263bc
MD
2306 return (error);
2307}
2308
984263bc 2309/*
41c20dac
MD
2310 * pathconf_Args(char *path, int name)
2311 *
984263bc
MD
2312 * Get configurable pathname variables.
2313 */
984263bc
MD
2314/* ARGSUSED */
2315int
753fd850 2316sys_pathconf(struct pathconf_args *uap)
984263bc 2317{
fad57d0e
MD
2318 struct nlookupdata nd;
2319 struct vnode *vp;
984263bc 2320 int error;
984263bc 2321
fad57d0e 2322 vp = NULL;
ab2eb4eb 2323 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2324 if (error == 0)
2325 error = nlookup(&nd);
2326 if (error == 0)
28623bf9 2327 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
fad57d0e
MD
2328 nlookup_done(&nd);
2329 if (error == 0) {
ab2eb4eb 2330 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
fad57d0e
MD
2331 vput(vp);
2332 }
984263bc
MD
2333 return (error);
2334}
2335
bbab27f6
DRJ
2336/*
2337 * XXX: daver
2338 * kern_readlink isn't properly split yet. There is a copyin burried
2339 * in VOP_READLINK().
2340 */
984263bc 2341int
fad57d0e 2342kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
984263bc 2343{
dadab5e9
MD
2344 struct thread *td = curthread;
2345 struct proc *p = td->td_proc;
41c20dac 2346 struct vnode *vp;
984263bc
MD
2347 struct iovec aiov;
2348 struct uio auio;
2349 int error;
984263bc 2350
fad57d0e
MD
2351 if ((error = nlookup(nd)) != 0)
2352 return (error);
28623bf9 2353 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp);
9697c509 2354 if (error)
984263bc 2355 return (error);
fad57d0e 2356 if (vp->v_type != VLNK) {
984263bc 2357 error = EINVAL;
fad57d0e 2358 } else {
9697c509
DRJ
2359 aiov.iov_base = buf;
2360 aiov.iov_len = count;
984263bc
MD
2361 auio.uio_iov = &aiov;
2362 auio.uio_iovcnt = 1;
2363 auio.uio_offset = 0;
2364 auio.uio_rw = UIO_READ;
2365 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 2366 auio.uio_td = td;
9697c509 2367 auio.uio_resid = count;
984263bc
MD
2368 error = VOP_READLINK(vp, &auio, p->p_ucred);
2369 }
2370 vput(vp);
9697c509
DRJ
2371 *res = count - auio.uio_resid;
2372 return (error);
2373}
2374
2375/*
2376 * readlink_args(char *path, char *buf, int count)
2377 *
2378 * Return target name of a symbolic link.
2379 */
2380int
753fd850 2381sys_readlink(struct readlink_args *uap)
9697c509 2382{
fad57d0e 2383 struct nlookupdata nd;
9697c509
DRJ
2384 int error;
2385
fad57d0e
MD
2386 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2387 if (error == 0) {
2388 error = kern_readlink(&nd, uap->buf, uap->count,
2389 &uap->sysmsg_result);
2390 }
2391 nlookup_done(&nd);
984263bc
MD
2392 return (error);
2393}
2394
2395static int
41c20dac 2396setfflags(struct vnode *vp, int flags)
984263bc 2397{
dadab5e9
MD
2398 struct thread *td = curthread;
2399 struct proc *p = td->td_proc;
984263bc
MD
2400 int error;
2401 struct vattr vattr;
2402
2403 /*
2404 * Prevent non-root users from setting flags on devices. When
2405 * a device is reused, users can retain ownership of the device
2406 * if they are allowed to set flags and programs assume that
2407 * chown can't fail when done as root.
2408 */
2409 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
dadab5e9 2410 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
984263bc
MD
2411 return (error);
2412
186fccd6
MD
2413 /*
2414 * note: vget is required for any operation that might mod the vnode
2415 * so VINACTIVE is properly cleared.
2416 */
87de5057 2417 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2418 VATTR_NULL(&vattr);
2419 vattr.va_flags = flags;
87de5057 2420 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2421 vput(vp);
2422 }
984263bc
MD
2423 return (error);
2424}
2425
2426/*
41c20dac
MD
2427 * chflags(char *path, int flags)
2428 *
984263bc
MD
2429 * Change flags of a file given a path name.
2430 */
984263bc
MD
2431/* ARGSUSED */
2432int
753fd850 2433sys_chflags(struct chflags_args *uap)
984263bc 2434{
fad57d0e
MD
2435 struct nlookupdata nd;
2436 struct vnode *vp;
984263bc 2437 int error;
984263bc 2438
fad57d0e 2439 vp = NULL;
ab2eb4eb 2440 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
fad57d0e
MD
2441 /* XXX Add NLC flag indicating modifying operation? */
2442 if (error == 0)
2443 error = nlookup(&nd);
72310cfb 2444 if (error == 0)
28623bf9 2445 error = ncp_writechk(&nd.nl_nch);
fad57d0e 2446 if (error == 0)
28623bf9 2447 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
fad57d0e
MD
2448 nlookup_done(&nd);
2449 if (error == 0) {
ab2eb4eb 2450 error = setfflags(vp, uap->flags);
fad57d0e
MD
2451 vrele(vp);
2452 }
2453 return (error);
984263bc
MD
2454}
2455
2456/*
41c20dac
MD
2457 * fchflags_args(int fd, int flags)
2458 *
984263bc
MD
2459 * Change flags of a file given a file descriptor.
2460 */
984263bc
MD
2461/* ARGSUSED */
2462int
753fd850 2463sys_fchflags(struct fchflags_args *uap)
984263bc 2464{
dadab5e9
MD
2465 struct thread *td = curthread;
2466 struct proc *p = td->td_proc;
984263bc
MD
2467 struct file *fp;
2468 int error;
2469
5b287bba 2470 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2471 return (error);
28623bf9
MD
2472 if (fp->f_nchandle.ncp)
2473 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2474 if (error == 0)
2475 error = setfflags((struct vnode *) fp->f_data, uap->flags);
5b287bba
MD
2476 fdrop(fp);
2477 return (error);
984263bc
MD
2478}
2479
2480static int
41c20dac 2481setfmode(struct vnode *vp, int mode)
984263bc 2482{
dadab5e9
MD
2483 struct thread *td = curthread;
2484 struct proc *p = td->td_proc;
984263bc
MD
2485 int error;
2486 struct vattr vattr;
2487
186fccd6
MD
2488 /*
2489 * note: vget is required for any operation that might mod the vnode
2490 * so VINACTIVE is properly cleared.
2491 */
87de5057 2492 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2493 VATTR_NULL(&vattr);
2494 vattr.va_mode = mode & ALLPERMS;
87de5057 2495 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2496 vput(vp);
2497 }
984263bc
MD
2498 return error;
2499}
2500
9697c509 2501int
fad57d0e 2502kern_chmod(struct nlookupdata *nd, int mode)
9697c509 2503{
fad57d0e 2504 struct vnode *vp;
9697c509
DRJ
2505 int error;
2506
fad57d0e
MD
2507 /* XXX Add NLC flag indicating modifying operation? */
2508 if ((error = nlookup(nd)) != 0)
9697c509 2509 return (error);
28623bf9 2510 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e 2511 return (error);
28623bf9 2512 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
72310cfb 2513 error = setfmode(vp, mode);
fad57d0e
MD
2514 vrele(vp);
2515 return (error);
9697c509
DRJ
2516}
2517
984263bc 2518/*
41c20dac
MD
2519 * chmod_args(char *path, int mode)
2520 *
984263bc
MD
2521 * Change mode of a file given path name.
2522 */
984263bc
MD
2523/* ARGSUSED */
2524int
753fd850 2525sys_chmod(struct chmod_args *uap)
984263bc 2526{
fad57d0e 2527 struct nlookupdata nd;
9697c509 2528 int error;
984263bc 2529
fad57d0e
MD
2530 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2531 if (error == 0)
2532 error = kern_chmod(&nd, uap->mode);
2533 nlookup_done(&nd);
9697c509 2534 return (error);
984263bc
MD
2535}
2536
2537/*
41c20dac
MD
2538 * lchmod_args(char *path, int mode)
2539 *
984263bc
MD
2540 * Change mode of a file given path name (don't follow links.)
2541 */
984263bc
MD
2542/* ARGSUSED */
2543int
753fd850 2544sys_lchmod(struct lchmod_args *uap)
984263bc 2545{
fad57d0e 2546 struct nlookupdata nd;
984263bc 2547 int error;
984263bc 2548
fad57d0e
MD
2549 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2550 if (error == 0)
2551 error = kern_chmod(&nd, uap->mode);
2552 nlookup_done(&nd);
2553 return (error);
984263bc
MD
2554}
2555
2556/*
41c20dac
MD
2557 * fchmod_args(int fd, int mode)
2558 *
984263bc
MD
2559 * Change mode of a file given a file descriptor.
2560 */
984263bc
MD
2561/* ARGSUSED */
2562int
753fd850 2563sys_fchmod(struct fchmod_args *uap)
984263bc 2564{
dadab5e9
MD
2565 struct thread *td = curthread;
2566 struct proc *p = td->td_proc;
984263bc
MD
2567 struct file *fp;
2568 int error;
2569
5b287bba 2570 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2571 return (error);
28623bf9
MD
2572 if (fp->f_nchandle.ncp)
2573 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2574 if (error == 0)
2575 error = setfmode((struct vnode *)fp->f_data, uap->mode);
5b287bba
MD
2576 fdrop(fp);
2577 return (error);
984263bc
MD
2578}
2579
2580static int
41c20dac 2581setfown(struct vnode *vp, uid_t uid, gid_t gid)
984263bc 2582{
dadab5e9
MD
2583 struct thread *td = curthread;
2584 struct proc *p = td->td_proc;
984263bc
MD
2585 int error;
2586 struct vattr vattr;
2587
186fccd6
MD
2588 /*
2589 * note: vget is required for any operation that might mod the vnode
2590 * so VINACTIVE is properly cleared.
2591 */
87de5057 2592 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2593 VATTR_NULL(&vattr);
2594 vattr.va_uid = uid;
2595 vattr.va_gid = gid;
87de5057 2596 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2597 vput(vp);
2598 }
984263bc
MD
2599 return error;
2600}
2601
9697c509 2602int
fad57d0e 2603kern_chown(struct nlookupdata *nd, int uid, int gid)
9697c509 2604{
fad57d0e 2605 struct vnode *vp;
9697c509
DRJ
2606 int error;
2607
fad57d0e
MD
2608 /* XXX Add NLC flag indicating modifying operation? */
2609 if ((error = nlookup(nd)) != 0)
9697c509 2610 return (error);
28623bf9 2611 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e 2612 return (error);
28623bf9 2613 if ((error = ncp_writechk(&nd->nl_nch)) == 0)
72310cfb 2614 error = setfown(vp, uid, gid);
fad57d0e 2615 vrele(vp);
9697c509
DRJ
2616 return (error);
2617}
2618
984263bc 2619/*
41c20dac
MD
2620 * chown(char *path, int uid, int gid)
2621 *
984263bc
MD
2622 * Set ownership given a path name.
2623 */
984263bc 2624int
753fd850 2625sys_chown(struct chown_args *uap)
984263bc 2626{
fad57d0e 2627 struct nlookupdata nd;
9697c509
DRJ
2628 int error;
2629
fad57d0e
MD
2630 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2631 if (error == 0)
2632 error = kern_chown(&nd, uap->uid, uap->gid);
2633 nlookup_done(&nd);
984263bc
MD
2634 return (error);
2635}
2636
2637/*
41c20dac
MD
2638 * lchown_args(char *path, int uid, int gid)
2639 *
984263bc
MD
2640 * Set ownership given a path name, do not cross symlinks.
2641 */
984263bc 2642int
753fd850 2643sys_lchown(struct lchown_args *uap)
984263bc 2644{
fad57d0e 2645 struct nlookupdata nd;
984263bc 2646 int error;
9697c509 2647
fad57d0e
MD
2648 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2649 if (error == 0)
2650 error = kern_chown(&nd, uap->uid, uap->gid);
2651 nlookup_done(&nd);
984263bc
MD
2652 return (error);
2653}
2654
2655/*
41c20dac
MD
2656 * fchown_args(int fd, int uid, int gid)
2657 *
984263bc
MD
2658 * Set ownership given a file descriptor.
2659 */
984263bc
MD
2660/* ARGSUSED */
2661int
753fd850 2662sys_fchown(struct fchown_args *uap)
984263bc 2663{
dadab5e9
MD
2664 struct thread *td = curthread;
2665 struct proc *p = td->td_proc;
984263bc
MD
2666 struct file *fp;
2667 int error;
2668
5b287bba 2669 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc 2670 return (error);
28623bf9
MD
2671 if (fp->f_nchandle.ncp)
2672 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2673 if (error == 0)
2674 error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid);
5b287bba
MD
2675 fdrop(fp);
2676 return (error);
984263bc
MD
2677}
2678
2679static int
9697c509 2680getutimes(const struct timeval *tvp, struct timespec *tsp)
984263bc
MD
2681{
2682 struct timeval tv[2];
984263bc 2683
9697c509 2684 if (tvp == NULL) {
984263bc
MD
2685 microtime(&tv[0]);
2686 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2687 tsp[1] = tsp[0];
2688 } else {
9697c509
DRJ
2689 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2690 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
984263bc
MD
2691 }
2692 return 0;
2693}
2694
2695static int
41c20dac 2696setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
984263bc 2697{
dadab5e9
MD
2698 struct thread *td = curthread;
2699 struct proc *p = td->td_proc;
984263bc
MD
2700 int error;
2701 struct vattr vattr;
2702
186fccd6
MD
2703 /*
2704 * note: vget is required for any operation that might mod the vnode
2705 * so VINACTIVE is properly cleared.
2706 */
87de5057 2707 if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
186fccd6
MD
2708 VATTR_NULL(&vattr);
2709 vattr.va_atime = ts[0];
2710 vattr.va_mtime = ts[1];
2711 if (nullflag)
2712 vattr.va_vaflags |= VA_UTIMES_NULL;
87de5057 2713 error = VOP_SETATTR(vp, &vattr, p->p_ucred);
186fccd6
MD
2714 vput(vp);
2715 }
984263bc
MD
2716 return error;
2717}
2718
9697c509 2719int
fad57d0e 2720kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
9697c509
DRJ
2721{
2722 struct timespec ts[2];
fad57d0e 2723 struct vnode *vp;
9697c509
DRJ
2724 int error;
2725
fad57d0e 2726 if ((error = getutimes(tptr, ts)) != 0)
9697c509 2727 return (error);
fad57d0e
MD
2728 /* XXX Add NLC flag indicating modifying operation? */
2729 if ((error = nlookup(nd)) != 0)
9697c509 2730 return (error);
28623bf9 2731 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2732 return (error);
28623bf9 2733 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e
MD
2734 return (error);
2735 error = setutimes(vp, ts, tptr == NULL);
2736 vrele(vp);
9697c509
DRJ
2737 return (error);
2738}
2739
984263bc 2740/*
41c20dac
MD
2741 * utimes_args(char *path, struct timeval *tptr)
2742 *
984263bc
MD
2743 * Set the access and modification times of a file.
2744 */
984263bc 2745int
753fd850 2746sys_utimes(struct utimes_args *uap)
984263bc 2747{
9697c509 2748 struct timeval tv[2];
fad57d0e 2749 struct nlookupdata nd;
9697c509
DRJ
2750 int error;
2751
2752 if (uap->tptr) {
2753 error = copyin(uap->tptr, tv, sizeof(tv));
2754 if (error)
2755 return (error);
2756 }
fad57d0e
MD
2757 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2758 if (error == 0)
2759 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2760 nlookup_done(&nd);
984263bc
MD
2761 return (error);
2762}
2763
2764/*
41c20dac
MD
2765 * lutimes_args(char *path, struct timeval *tptr)
2766 *
984263bc
MD
2767 * Set the access and modification times of a file.
2768 */
984263bc 2769int
753fd850 2770sys_lutimes(struct lutimes_args *uap)
984263bc 2771{
9697c509 2772 struct timeval tv[2];
fad57d0e 2773 struct nlookupdata nd;
9697c509
DRJ
2774 int error;
2775
2776 if (uap->tptr) {
2777 error = copyin(uap->tptr, tv, sizeof(tv));
2778 if (error)
2779 return (error);
2780 }
fad57d0e
MD
2781 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2782 if (error == 0)
2783 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2784 nlookup_done(&nd);
984263bc
MD
2785 return (error);
2786}
2787
984263bc 2788int
9697c509 2789kern_futimes(int fd, struct timeval *tptr)
984263bc 2790{
dadab5e9
MD
2791 struct thread *td = curthread;
2792 struct proc *p = td->td_proc;
984263bc
MD
2793 struct timespec ts[2];
2794 struct file *fp;
984263bc
MD
2795 int error;
2796
9697c509
DRJ
2797 error = getutimes(tptr, ts);
2798 if (error)
984263bc 2799 return (error);
5b287bba 2800 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
984263bc 2801 return (error);
28623bf9
MD
2802 if (fp->f_nchandle.ncp)
2803 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2804 if (error == 0)
2805 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
5b287bba 2806 fdrop(fp);
9697c509 2807 return (error);
984263bc
MD
2808}
2809
2810/*
9697c509 2811 * futimes_args(int fd, struct timeval *tptr)
41c20dac 2812 *
9697c509 2813 * Set the access and modification times of a file.
984263bc 2814 */
984263bc 2815int
753fd850 2816sys_futimes(struct futimes_args *uap)
9697c509
DRJ
2817{
2818 struct timeval tv[2];
2819 int error;
2820
2821 if (uap->tptr) {
2822 error = copyin(uap->tptr, tv, sizeof(tv));
2823 if (error)
2824 return (error);
2825 }
2826
b651c485 2827 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
9697c509
DRJ
2828
2829 return (error);
2830}
2831
2832int
fad57d0e 2833kern_truncate(struct nlookupdata *nd, off_t length)
984263bc 2834{
41c20dac 2835 struct vnode *vp;
984263bc
MD
2836 struct vattr vattr;
2837 int error;
984263bc 2838
9697c509 2839 if (length < 0)
984263bc 2840 return(EINVAL);
fad57d0e
MD
2841 /* XXX Add NLC flag indicating modifying operation? */
2842 if ((error = nlookup(nd)) != 0)
984263bc 2843 return (error);
28623bf9 2844 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 2845 return (error);
28623bf9 2846 if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
fad57d0e 2847 return (error);
ca466bae 2848 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) {
fad57d0e
MD
2849 vrele(vp);
2850 return (error);
2851 }
2852 if (vp->v_type == VDIR) {
984263bc 2853 error = EISDIR;
28623bf9 2854 } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0 &&
87de5057 2855 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred)) == 0) {
984263bc 2856 VATTR_NULL(&vattr);
9697c509 2857 vattr.va_size = length;
87de5057 2858 error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
984263bc
MD
2859 }
2860 vput(vp);
2861 return (error);
2862}
2863
9697c509
DRJ
2864/*
2865 * truncate(char *path, int pad, off_t length)
2866 *
2867 * Truncate a file given its path name.
2868 */
2869int
753fd850 2870sys_truncate(struct truncate_args *uap)
9697c509 2871{
fad57d0e 2872 struct nlookupdata nd;
9697c509
DRJ
2873 int error;
2874
fad57d0e
MD
2875 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2876 if (error == 0)
2877 error = kern_truncate(&nd, uap->length);
2878 nlookup_done(&nd);
9697c509
DRJ
2879 return error;
2880}
2881
984263bc 2882int
8f6f8622 2883kern_ftruncate(int fd, off_t length)
984263bc 2884{
dadab5e9
MD
2885 struct thread *td = curthread;
2886 struct proc *p = td->td_proc;
984263bc
MD
2887 struct vattr vattr;
2888 struct vnode *vp;
2889 struct file *fp;
2890 int error;
2891
8f6f8622 2892 if (length < 0)
984263bc 2893 return(EINVAL);
5b287bba 2894 if ((error = holdvnode(p->p_fd, fd, &fp)) != 0)
984263bc 2895 return (error);
28623bf9
MD
2896 if (fp->f_nchandle.ncp) {
2897 error = ncp_writechk(&fp->f_nchandle);
72310cfb
MD
2898 if (error)
2899 goto done;
2900 }
5b287bba
MD
2901 if ((fp->f_flag & FWRITE) == 0) {
2902 error = EINVAL;
2903 goto done;
2904 }
984263bc 2905 vp = (struct vnode *)fp->f_data;
ca466bae 2906 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5b287bba 2907 if (vp->v_type == VDIR) {
984263bc 2908 error = EISDIR;
468bb1f9 2909 } else if ((error = vn_writechk(vp, NULL)) == 0) {
984263bc 2910 VATTR_NULL(&vattr);
8f6f8622 2911 vattr.va_size = length;
87de5057 2912 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
984263bc 2913 }
a11aaa81 2914 vn_unlock(vp);
5b287bba
MD
2915done:
2916 fdrop(fp);
984263bc
MD
2917 return (error);
2918}
2919
984263bc 2920/*
8f6f8622 2921 * ftruncate_args(int fd, int pad, off_t length)
41c20dac 2922 *
8f6f8622 2923 * Truncate a file given a file descriptor.
984263bc 2924 */
984263bc 2925int
753fd850 2926sys_ftruncate(struct ftruncate_args *uap)
984263bc 2927{
8f6f8622 2928 int error;
984263bc 2929
8f6f8622
DRJ
2930 error = kern_ftruncate(uap->fd, uap->length);
2931
2932 return (error);
984263bc
MD
2933}
2934
984263bc 2935/*
41c20dac
MD
2936 * fsync(int fd)
2937 *
984263bc
MD
2938 * Sync an open file.
2939 */
984263bc
MD
2940/* ARGSUSED */
2941int
753fd850 2942sys_fsync(struct fsync_args *uap)
984263bc 2943{
dadab5e9
MD
2944 struct thread *td = curthread;
2945 struct proc *p = td->td_proc;
41c20dac 2946 struct vnode *vp;
984263bc
MD
2947 struct file *fp;
2948 vm_object_t obj;
2949 int error;
2950
5b287bba 2951 if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0)
984263bc
MD
2952 return (error);
2953 vp = (struct vnode *)fp->f_data;
ca466bae 2954 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
7540ab49 2955 if ((obj = vp->v_object) != NULL)
984263bc 2956 vm_object_page_clean(obj, 0, 0, 0);
408357d8
MD
2957 if ((error = VOP_FSYNC(vp, MNT_WAIT)) == 0 && vp->v_mount)
2958 error = buf_fsync(vp);
a11aaa81 2959 vn_unlock(vp);
5b287bba 2960 fdrop(fp);
984263bc
MD
2961 return (error);
2962}
2963
984263bc 2964int
fad57d0e 2965kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
984263bc 2966{
28623bf9
MD
2967 struct nchandle fnchd;
2968 struct nchandle tnchd;
fad57d0e 2969 struct namecache *ncp;
dff430ab
MD
2970 struct vnode *fdvp;
2971 struct vnode *tdvp;
fad57d0e 2972 struct mount *mp;
984263bc
MD
2973 int error;
2974
2975 bwillwrite();
5312fa43 2976 fromnd->nl_flags |= NLC_REFDVP;
fad57d0e 2977 if ((error = nlookup(fromnd)) != 0)
984263bc 2978 return (error);
28623bf9 2979 if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
fad57d0e 2980 return (ENOENT);
28623bf9
MD
2981 fnchd.mount = fromnd->nl_nch.mount;
2982 cache_hold(&fnchd);
fad57d0e
MD
2983
2984 /*
28623bf9 2985 * unlock the source nch so we can lookup the target nch without
fad57d0e
MD
2986 * deadlocking. The target may or may not exist so we do not check
2987 * for a target vp like kern_mkdir() and other creation functions do.
2988 *
2989 * The source and target directories are ref'd and rechecked after
2990 * everything is relocked to determine if the source or target file
2991 * has been renamed.
2992 */
2993 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2994 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
28623bf9 2995 cache_unlock(&fromnd->nl_nch);
fad57d0e 2996
5312fa43 2997 tond->nl_flags |= NLC_CREATE | NLC_REFDVP;
fad57d0e 2998 if ((error = nlookup(tond)) != 0) {
28623bf9 2999 cache_drop(&fnchd);
fad57d0e
MD
3000 return (error);
3001 }
28623bf9
MD
3002 if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
3003 cache_drop(&fnchd);
fad57d0e
MD
3004 return (ENOENT);
3005 }
28623bf9
MD
3006 tnchd.mount = tond->nl_nch.mount;
3007 cache_hold(&tnchd);
fad57d0e
MD
3008
3009 /*
3010 * If the source and target are the same there is nothing to do
3011 */
28623bf9
MD
3012 if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
3013 cache_drop(&fnchd);
3014 cache_drop(&tnchd);
fad57d0e
MD
3015 return (0);
3016 }
3017
1d505369
MD
3018 /*
3019 * Mount points cannot be renamed or overwritten
3020 */
28623bf9
MD
3021 if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
3022 NCF_ISMOUNTPT
1d505369 3023 ) {
28623bf9
MD
3024 cache_drop(&fnchd);
3025 cache_drop(&tnchd);
1d505369
MD
3026 return (EINVAL);
3027 }
3028
fad57d0e 3029 /*
aa40af33
MD
3030 * relock the source ncp. NOTE AFTER RELOCKING: the source ncp
3031 * may have become invalid while it was unlocked, nc_vp and nc_mount
3032 * could be NULL.
fad57d0e 3033 */
28623bf9
MD
3034 if (cache_lock_nonblock(&fromnd->nl_nch) == 0) {
3035 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3036 } else if (fromnd->nl_nch.ncp > tond->nl_nch.ncp) {
3037 cache_lock(&fromnd->nl_nch);
3038 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
fad57d0e 3039 } else {
28623bf9
MD
3040 cache_unlock(&tond->nl_nch);
3041 cache_lock(&fromnd->nl_nch);
3042 cache_resolve(&fromnd->nl_nch, fromnd->nl_cred);
3043 cache_lock(&tond->nl_nch);
3044 cache_resolve(&tond->nl_nch, tond->nl_cred);
fad57d0e
MD
3045 }
3046 fromnd->nl_flags |= NLC_NCPISLOCKED;
3047
3048 /*
3049 * make sure the parent directories linkages are the same
3050 */
28623bf9
MD
3051 if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
3052 tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
3053 cache_drop(&fnchd);
3054 cache_drop(&tnchd);
fad57d0e
MD
3055 return (ENOENT);
3056 }
3057
3058 /*
3059 * Both the source and target must be within the same filesystem and
3060 * in the same filesystem as their parent directories within the
3061 * namecache topology.
aa40af33
MD
3062 *
3063 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
fad57d0e 3064 */
28623bf9
MD
3065 mp = fnchd.mount;
3066 if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
3067 mp != tond->nl_nch.mount) {
3068 cache_drop(&fnchd);
3069 cache_drop(&tnchd);
fad57d0e
MD
3070 return (EXDEV);
3071 }
3072
72310cfb
MD
3073 /*
3074 * Make sure the mount point is writable
3075 */
28623bf9
MD
3076 if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
3077 cache_drop(&fnchd);
3078 cache_drop(&tnchd);
72310cfb
MD
3079 return (error);
3080 }
3081
fad57d0e
MD
3082 /*
3083 * If the target exists and either the source or target is a directory,
3084 * then both must be directories.
aa40af33 3085 *
28623bf9
MD
3086 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3087 * have become NULL.
fad57d0e 3088 */
28623bf9
MD
3089 if (tond->nl_nch.ncp->nc_vp) {
3090 if (fromnd->nl_nch.ncp->nc_vp == NULL) {
aa40af33 3091 error = ENOENT;
28623bf9
MD
3092 } else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
3093 if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
fad57d0e 3094 error = ENOTDIR;
28623bf9 3095 } else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
984263bc 3096 error = EISDIR;
984263bc
MD
3097 }
3098 }
fad57d0e 3099
984263bc 3100 /*
fad57d0e
MD
3101 * You cannot rename a source into itself or a subdirectory of itself.
3102 * We check this by travsersing the target directory upwards looking
3103 * for a match against the source.
984263bc 3104 */
fad57d0e 3105 if (error == 0) {
28623bf9
MD
3106 for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
3107 if (fromnd->nl_nch.ncp == ncp) {
fad57d0e
MD
3108 error = EINVAL;
3109 break;
3110 }
984263bc 3111 }
fad57d0e
MD
3112 }
3113
28623bf9
MD
3114 cache_drop(&fnchd);
3115 cache_drop(&tnchd);
bc8fd9e0
MD
3116
3117 /*
3118 * Even though the namespaces are different, they may still represent
3119 * hardlinks to the same file. The filesystem might have a hard time
3120 * with this so we issue a NREMOVE of the source instead of a NRENAME
3121 * when we detect the situation.
3122 */
3123 if (error == 0) {
5312fa43
MD
3124 fdvp = fromnd->nl_dvp;
3125 tdvp = tond->nl_dvp;
dff430ab
MD
3126 if (fdvp == NULL || tdvp == NULL) {
3127 error = EPERM;
3128 } else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
dff430ab
MD
3129 error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
3130 fromnd->nl_cred);
bc8fd9e0 3131 } else {
28623bf9 3132 error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
dff430ab 3133 fdvp, tdvp, tond->nl_cred);
bc8fd9e0
MD
3134 }
3135 }
984263bc
MD
3136 return (error);
3137}
3138
3139/*
9697c509 3140 * rename_args(char *from, char *to)
41c20dac 3141 *
9697c509
DRJ
3142 * Rename files. Source and destination must either both be directories,
3143 * or both not be directories. If target is a directory, it must be empty.
984263bc 3144 */
984263bc 3145int
753fd850 3146sys_rename(struct rename_args *uap)
9697c509 3147{
fad57d0e 3148 struct nlookupdata fromnd, tond;
9697c509
DRJ
3149 int error;
3150
fad57d0e
MD
3151 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
3152 if (error == 0) {
3153 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
3154 if (error == 0)
3155 error = kern_rename(&fromnd, &tond);
3156 nlookup_done(&tond);
3157 }
3158 nlookup_done(&fromnd);
9697c509
DRJ
3159 return (error);
3160}
3161
3162int
fad57d0e 3163kern_mkdir(struct nlookupdata *nd, int mode)
984263bc 3164{
dadab5e9
MD
3165 struct thread *td = curthread;
3166 struct proc *p = td->td_proc;
41c20dac 3167 struct vnode *vp;
984263bc
MD
3168 struct vattr vattr;
3169 int error;
984263bc
MD
3170
3171 bwillwrite();
5312fa43 3172 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
fad57d0e 3173 if ((error = nlookup(nd)) != 0)
984263bc 3174 return (error);
fad57d0e 3175
28623bf9 3176 if (nd->nl_nch.ncp->nc_vp)
984263bc 3177 return (EEXIST);
28623bf9 3178 if ((error = ncp_writechk(&nd->nl_nch)) != 0)
72310cfb 3179 return (error);
984263bc
MD
3180 VATTR_NULL(&vattr);
3181 vattr.va_type = VDIR;
9697c509 3182 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
fad57d0e
MD
3183
3184 vp = NULL;
5312fa43 3185 error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, p->p_ucred, &vattr);
9697c509 3186 if (error == 0)
fad57d0e 3187 vput(vp);
984263bc
MD
3188 return (error);
3189}
3190
3191/*
9697c509 3192 * mkdir_args(char *path, int mode)
41c20dac 3193 *
9697c509 3194 * Make a directory fi