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