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