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