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