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