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