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