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