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