vfs_syscalls.c revision 1.147.4.1 1 /* $NetBSD: vfs_syscalls.c,v 1.147.4.1 1999/10/19 12:50:08 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
41 */
42
43 #include "opt_compat_netbsd.h"
44 #include "opt_compat_43.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/proc.h>
56 #include <sys/uio.h>
57 #include <sys/malloc.h>
58 #include <sys/dirent.h>
59
60 #include <sys/syscallargs.h>
61
62 #include <vm/vm.h>
63 #include <sys/sysctl.h>
64
65 #include <miscfs/genfs/genfs.h>
66 #include <miscfs/syncfs/syncfs.h>
67
68 #include <uvm/uvm_extern.h>
69
70 static int change_dir __P((struct nameidata *, struct proc *));
71 static int change_mode __P((struct vnode *, int, struct proc *p));
72 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
73 int));
74 static int change_utimes __P((struct vnode *vp, const struct timeval *,
75 struct proc *p));
76 static int rename_files __P((const char *, const char *, struct proc *, int));
77
78 void checkdirs __P((struct vnode *));
79 int dounmount __P((struct mount *, int, struct proc *));
80
81 /*
82 * Virtual File System System Calls
83 */
84
85 /*
86 * Mount a file system.
87 */
88
89 /*
90 * This table is used to maintain compatibility with 4.3BSD
91 * and NetBSD 0.9 mount syscalls. Note, the order is important!
92 *
93 * Also note that not all of these had actual numbers in 4.3BSD
94 * or NetBSD 0.9!
95 */
96 const char *mountcompatnames[] = {
97 NULL, /* 0 = MOUNT_NONE */
98 MOUNT_FFS, /* 1 */
99 MOUNT_NFS, /* 2 */
100 MOUNT_MFS, /* 3 */
101 MOUNT_MSDOS, /* 4 */
102 MOUNT_LFS, /* 5 */
103 NULL, /* 6 = MOUNT_LOFS */
104 MOUNT_FDESC, /* 7 */
105 MOUNT_PORTAL, /* 8 */
106 MOUNT_NULL, /* 9 */
107 MOUNT_UMAP, /* 10 */
108 MOUNT_KERNFS, /* 11 */
109 MOUNT_PROCFS, /* 12 */
110 MOUNT_AFS, /* 13 */
111 MOUNT_CD9660, /* 14 = MOUNT_ISOFS */
112 MOUNT_UNION, /* 15 */
113 MOUNT_ADOSFS, /* 16 */
114 MOUNT_EXT2FS, /* 17 */
115 MOUNT_CODA, /* 18 */
116 MOUNT_FILECORE, /* 19 */
117 MOUNT_NTFS, /* 20 */
118 };
119 const int nmountcompatnames = sizeof(mountcompatnames) /
120 sizeof(mountcompatnames[0]);
121
122 /* ARGSUSED */
123 int
124 sys_mount(p, v, retval)
125 struct proc *p;
126 void *v;
127 register_t *retval;
128 {
129 register struct sys_mount_args /* {
130 syscallarg(const char *) type;
131 syscallarg(const char *) path;
132 syscallarg(int) flags;
133 syscallarg(void *) data;
134 } */ *uap = v;
135 struct vnode *vp;
136 struct mount *mp;
137 int error, flag = 0;
138 char fstypename[MFSNAMELEN];
139 struct vattr va;
140 struct nameidata nd;
141 struct vfsops *vfs;
142
143 /*
144 * Get vnode to be covered
145 */
146 NDINIT(&nd, LOOKUP, FOLLOW , UIO_USERSPACE,
147 SCARG(uap, path), p);
148 if ((error = namei(&nd)) != 0)
149 return (error);
150 vp = nd.ni_vp;
151 /*
152 * A lookup in VFS_MOUNT might result in an attempt to
153 * lock this vnode again, so make the lock resursive.
154 */
155 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
156 if (SCARG(uap, flags) & MNT_UPDATE) {
157 if ((vp->v_flag & VROOT) == 0) {
158 vput(vp);
159 return (EINVAL);
160 }
161 mp = vp->v_mount;
162 flag = mp->mnt_flag;
163 vfs = mp->mnt_op;
164 /*
165 * We only allow the filesystem to be reloaded if it
166 * is currently mounted read-only.
167 */
168 if ((SCARG(uap, flags) & MNT_RELOAD) &&
169 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
170 vput(vp);
171 return (EOPNOTSUPP); /* Needs translation */
172 }
173 /*
174 * In "highly secure" mode, don't let the caller do anything
175 * but downgrade a filesystem from read-write to read-only.
176 * (see also below; MNT_UPDATE is required.)
177 */
178 if (securelevel >= 2 &&
179 (SCARG(uap, flags) !=
180 (mp->mnt_flag | MNT_RDONLY |
181 MNT_RELOAD | MNT_FORCE | MNT_UPDATE))) {
182 vput(vp);
183 return (EPERM);
184 }
185 mp->mnt_flag |=
186 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
187 /*
188 * Only root, or the user that did the original mount is
189 * permitted to update it.
190 */
191 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
192 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
193 vput(vp);
194 return (error);
195 }
196 /*
197 * Do not allow NFS export by non-root users. For non-root
198 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
199 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
200 */
201 if (p->p_ucred->cr_uid != 0) {
202 if (SCARG(uap, flags) & MNT_EXPORTED) {
203 vput(vp);
204 return (EPERM);
205 }
206 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
207 if (flag & MNT_NOEXEC)
208 SCARG(uap, flags) |= MNT_NOEXEC;
209 }
210 if (vfs_busy(mp, LK_NOWAIT, 0)) {
211 vput(vp);
212 return (EPERM);
213 }
214 VOP_UNLOCK(vp, 0);
215 goto update;
216 } else {
217 if (securelevel >= 2)
218 return (EPERM);
219 }
220 /*
221 * If the user is not root, ensure that they own the directory
222 * onto which we are attempting to mount.
223 */
224 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
225 (va.va_uid != p->p_ucred->cr_uid &&
226 (error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
227 vput(vp);
228 return (error);
229 }
230 /*
231 * Do not allow NFS export by non-root users. For non-root users,
232 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
233 * mount point is already MNT_NOEXEC.
234 */
235 if (p->p_ucred->cr_uid != 0) {
236 if (SCARG(uap, flags) & MNT_EXPORTED) {
237 vput(vp);
238 return (EPERM);
239 }
240 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
241 if (vp->v_mount->mnt_flag & MNT_NOEXEC)
242 SCARG(uap, flags) |= MNT_NOEXEC;
243 }
244 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
245 return (error);
246 if (vp->v_type != VDIR) {
247 vput(vp);
248 return (ENOTDIR);
249 }
250 error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
251 if (error) {
252 #if defined(COMPAT_09) || defined(COMPAT_43)
253 /*
254 * Historically filesystem types were identified by number.
255 * If we get an integer for the filesystem type instead of a
256 * string, we check to see if it matches one of the historic
257 * filesystem types.
258 */
259 u_long fsindex = (u_long)SCARG(uap, type);
260 if (fsindex >= nmountcompatnames ||
261 mountcompatnames[fsindex] == NULL) {
262 vput(vp);
263 return (ENODEV);
264 }
265 strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
266 #else
267 vput(vp);
268 return (error);
269 #endif
270 }
271 #ifdef COMPAT_10
272 /* Accept `ufs' as an alias for `ffs'. */
273 if (!strncmp(fstypename, "ufs", MFSNAMELEN))
274 strncpy(fstypename, "ffs", MFSNAMELEN);
275 #endif
276 if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
277 vput(vp);
278 return (ENODEV);
279 }
280 if (vp->v_mountedhere != NULL) {
281 vput(vp);
282 return (EBUSY);
283 }
284
285 /*
286 * Allocate and initialize the file system.
287 */
288 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
289 M_MOUNT, M_WAITOK);
290 memset((char *)mp, 0, (u_long)sizeof(struct mount));
291 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
292 (void)vfs_busy(mp, LK_NOWAIT, 0);
293 mp->mnt_op = vfs;
294 vfs->vfs_refcount++;
295 mp->mnt_vnodecovered = vp;
296 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
297 update:
298 /*
299 * Set the mount level flags.
300 */
301 if (SCARG(uap, flags) & MNT_RDONLY)
302 mp->mnt_flag |= MNT_RDONLY;
303 else if (mp->mnt_flag & MNT_RDONLY)
304 mp->mnt_flag |= MNT_WANTRDWR;
305 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
306 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
307 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
308 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
309 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
310 MNT_NOCOREDUMP | MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
311 /*
312 * Mount the filesystem.
313 */
314 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
315 if (mp->mnt_flag & MNT_UPDATE) {
316 vrele(vp);
317 if (mp->mnt_flag & MNT_WANTRDWR)
318 mp->mnt_flag &= ~MNT_RDONLY;
319 mp->mnt_flag &=~
320 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
321 if (error)
322 mp->mnt_flag = flag;
323 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
324 if (mp->mnt_syncer == NULL)
325 error = vfs_allocate_syncvnode(mp);
326 } else {
327 if (mp->mnt_syncer != NULL)
328 vgone(mp->mnt_syncer);
329 mp->mnt_syncer = NULL;
330 }
331 vfs_unbusy(mp);
332 return (error);
333 }
334 /*
335 * Put the new filesystem on the mount list after root.
336 */
337 cache_purge(vp);
338 if (!error) {
339 vp->v_mountedhere = mp;
340 simple_lock(&mountlist_slock);
341 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
342 simple_unlock(&mountlist_slock);
343 checkdirs(vp);
344 VOP_UNLOCK(vp, 0);
345 if ((mp->mnt_flag & MNT_RDONLY) == 0)
346 error = vfs_allocate_syncvnode(mp);
347 vfs_unbusy(mp);
348 (void) VFS_STATFS(mp, &mp->mnt_stat, p);
349 if ((error = VFS_START(mp, 0, p)))
350 vrele(vp);
351 } else {
352 vp->v_mountedhere = (struct mount *)0;
353 vfs->vfs_refcount--;
354 vfs_unbusy(mp);
355 free((caddr_t)mp, M_MOUNT);
356 vput(vp);
357 }
358 return (error);
359 }
360
361 /*
362 * Scan all active processes to see if any of them have a current
363 * or root directory onto which the new filesystem has just been
364 * mounted. If so, replace them with the new mount point.
365 */
366 void
367 checkdirs(olddp)
368 struct vnode *olddp;
369 {
370 struct cwdinfo *cwdi;
371 struct vnode *newdp;
372 struct proc *p;
373
374 if (olddp->v_usecount == 1)
375 return;
376 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
377 panic("mount: lost mount");
378 proclist_lock_read();
379 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
380 cwdi = p->p_cwdi;
381 if (cwdi->cwdi_cdir == olddp) {
382 vrele(cwdi->cwdi_cdir);
383 VREF(newdp);
384 cwdi->cwdi_cdir = newdp;
385 }
386 if (cwdi->cwdi_rdir == olddp) {
387 vrele(cwdi->cwdi_rdir);
388 VREF(newdp);
389 cwdi->cwdi_rdir = newdp;
390 }
391 }
392 proclist_unlock_read();
393 if (rootvnode == olddp) {
394 vrele(rootvnode);
395 VREF(newdp);
396 rootvnode = newdp;
397 }
398 vput(newdp);
399 }
400
401 /*
402 * Unmount a file system.
403 *
404 * Note: unmount takes a path to the vnode mounted on as argument,
405 * not special file (as before).
406 */
407 /* ARGSUSED */
408 int
409 sys_unmount(p, v, retval)
410 struct proc *p;
411 void *v;
412 register_t *retval;
413 {
414 register struct sys_unmount_args /* {
415 syscallarg(const char *) path;
416 syscallarg(int) flags;
417 } */ *uap = v;
418 register struct vnode *vp;
419 struct mount *mp;
420 int error;
421 struct nameidata nd;
422
423 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
424 SCARG(uap, path), p);
425 if ((error = namei(&nd)) != 0)
426 return (error);
427 vp = nd.ni_vp;
428 mp = vp->v_mount;
429
430 /*
431 * Only root, or the user that did the original mount is
432 * permitted to unmount this filesystem.
433 */
434 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
435 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
436 vput(vp);
437 return (error);
438 }
439
440 /*
441 * Don't allow unmounting the root file system.
442 */
443 if (mp->mnt_flag & MNT_ROOTFS) {
444 vput(vp);
445 return (EINVAL);
446 }
447
448 /*
449 * Must be the root of the filesystem
450 */
451 if ((vp->v_flag & VROOT) == 0) {
452 vput(vp);
453 return (EINVAL);
454 }
455 vput(vp);
456
457 if (vfs_busy(mp, 0, 0))
458 return (EBUSY);
459
460 return (dounmount(mp, SCARG(uap, flags), p));
461 }
462
463 /*
464 * Do the actual file system unmount. File system is assumed to have been
465 * marked busy by the caller.
466 */
467 int
468 dounmount(mp, flags, p)
469 register struct mount *mp;
470 int flags;
471 struct proc *p;
472 {
473 struct vnode *coveredvp;
474 int error;
475 int async;
476
477 simple_lock(&mountlist_slock);
478 mp->mnt_flag |= MNT_UNMOUNT;
479 vfs_unbusy(mp);
480 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
481 if (mp->mnt_flag & MNT_EXPUBLIC)
482 vfs_setpublicfs(NULL, NULL, NULL);
483 async = mp->mnt_flag & MNT_ASYNC;
484 mp->mnt_flag &=~ MNT_ASYNC;
485 cache_purgevfs(mp); /* remove cache entries for this file sys */
486 if (mp->mnt_syncer != NULL)
487 vgone(mp->mnt_syncer);
488 if (((mp->mnt_flag & MNT_RDONLY) ||
489 (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
490 (flags & MNT_FORCE))
491 error = VFS_UNMOUNT(mp, flags, p);
492 simple_lock(&mountlist_slock);
493 if (error) {
494 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
495 (void) vfs_allocate_syncvnode(mp);
496 mp->mnt_flag &= ~MNT_UNMOUNT;
497 mp->mnt_flag |= async;
498 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
499 &mountlist_slock);
500 while(mp->mnt_wcnt > 0) {
501 wakeup((caddr_t)mp);
502 tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1", 0);
503 }
504 return (error);
505 }
506 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
507 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
508 coveredvp->v_mountedhere = NULL;
509 vrele(coveredvp);
510 }
511 mp->mnt_op->vfs_refcount--;
512 if (mp->mnt_vnodelist.lh_first != NULL)
513 panic("unmount: dangling vnode");
514 mp->mnt_flag |= MNT_GONE;
515 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
516 while(mp->mnt_wcnt > 0) {
517 wakeup((caddr_t)mp);
518 tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0);
519 }
520 free((caddr_t)mp, M_MOUNT);
521 return (0);
522 }
523
524 /*
525 * Sync each mounted filesystem.
526 */
527 #ifdef DEBUG
528 int syncprt = 0;
529 struct ctldebug debug0 = { "syncprt", &syncprt };
530 #endif
531
532 /* ARGSUSED */
533 int
534 sys_sync(p, v, retval)
535 struct proc *p;
536 void *v;
537 register_t *retval;
538 {
539 register struct mount *mp, *nmp;
540 int asyncflag;
541
542 simple_lock(&mountlist_slock);
543 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
544 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
545 nmp = mp->mnt_list.cqe_prev;
546 continue;
547 }
548 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
549 asyncflag = mp->mnt_flag & MNT_ASYNC;
550 mp->mnt_flag &= ~MNT_ASYNC;
551 uvm_vnp_sync(mp);
552 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
553 if (asyncflag)
554 mp->mnt_flag |= MNT_ASYNC;
555 }
556 simple_lock(&mountlist_slock);
557 nmp = mp->mnt_list.cqe_prev;
558 vfs_unbusy(mp);
559
560 }
561 simple_unlock(&mountlist_slock);
562 #ifdef DEBUG
563 if (syncprt)
564 vfs_bufstats();
565 #endif /* DEBUG */
566 return (0);
567 }
568
569 /*
570 * Change filesystem quotas.
571 */
572 /* ARGSUSED */
573 int
574 sys_quotactl(p, v, retval)
575 struct proc *p;
576 void *v;
577 register_t *retval;
578 {
579 register struct sys_quotactl_args /* {
580 syscallarg(const char *) path;
581 syscallarg(int) cmd;
582 syscallarg(int) uid;
583 syscallarg(caddr_t) arg;
584 } */ *uap = v;
585 register struct mount *mp;
586 int error;
587 struct nameidata nd;
588
589 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
590 if ((error = namei(&nd)) != 0)
591 return (error);
592 mp = nd.ni_vp->v_mount;
593 vrele(nd.ni_vp);
594 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
595 SCARG(uap, arg), p));
596 }
597
598 /*
599 * Get filesystem statistics.
600 */
601 /* ARGSUSED */
602 int
603 sys_statfs(p, v, retval)
604 struct proc *p;
605 void *v;
606 register_t *retval;
607 {
608 register struct sys_statfs_args /* {
609 syscallarg(const char *) path;
610 syscallarg(struct statfs *) buf;
611 } */ *uap = v;
612 register struct mount *mp;
613 register struct statfs *sp;
614 int error;
615 struct nameidata nd;
616
617 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
618 if ((error = namei(&nd)) != 0)
619 return (error);
620 mp = nd.ni_vp->v_mount;
621 sp = &mp->mnt_stat;
622 vrele(nd.ni_vp);
623 if ((error = VFS_STATFS(mp, sp, p)) != 0)
624 return (error);
625 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
626 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
627 }
628
629 /*
630 * Get filesystem statistics.
631 */
632 /* ARGSUSED */
633 int
634 sys_fstatfs(p, v, retval)
635 struct proc *p;
636 void *v;
637 register_t *retval;
638 {
639 register struct sys_fstatfs_args /* {
640 syscallarg(int) fd;
641 syscallarg(struct statfs *) buf;
642 } */ *uap = v;
643 struct file *fp;
644 struct mount *mp;
645 register struct statfs *sp;
646 int error;
647
648 /* getvnode() will use the descriptor for us */
649 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
650 return (error);
651 mp = ((struct vnode *)fp->f_data)->v_mount;
652 sp = &mp->mnt_stat;
653 if ((error = VFS_STATFS(mp, sp, p)) != 0)
654 goto out;
655 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
656 error = copyout(sp, SCARG(uap, buf), sizeof(*sp));
657 out:
658 FILE_UNUSE(fp, p);
659 return (error);
660 }
661
662 /*
663 * Get statistics on all filesystems.
664 */
665 int
666 sys_getfsstat(p, v, retval)
667 struct proc *p;
668 void *v;
669 register_t *retval;
670 {
671 register struct sys_getfsstat_args /* {
672 syscallarg(struct statfs *) buf;
673 syscallarg(long) bufsize;
674 syscallarg(int) flags;
675 } */ *uap = v;
676 register struct mount *mp, *nmp;
677 register struct statfs *sp;
678 caddr_t sfsp;
679 long count, maxcount, error;
680
681 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
682 sfsp = (caddr_t)SCARG(uap, buf);
683 simple_lock(&mountlist_slock);
684 count = 0;
685 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
686 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
687 nmp = mp->mnt_list.cqe_next;
688 continue;
689 }
690 if (sfsp && count < maxcount) {
691 sp = &mp->mnt_stat;
692 /*
693 * If MNT_NOWAIT or MNT_LAZY is specified, do not
694 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
695 * overrides MNT_NOWAIT.
696 */
697 if (SCARG(uap, flags) != MNT_NOWAIT &&
698 SCARG(uap, flags) != MNT_LAZY &&
699 (SCARG(uap, flags) == MNT_WAIT ||
700 SCARG(uap, flags) == 0) &&
701 (error = VFS_STATFS(mp, sp, p)) != 0) {
702 simple_lock(&mountlist_slock);
703 nmp = mp->mnt_list.cqe_next;
704 vfs_unbusy(mp);
705 continue;
706 }
707 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
708 error = copyout(sp, sfsp, sizeof(*sp));
709 if (error) {
710 vfs_unbusy(mp);
711 return (error);
712 }
713 sfsp += sizeof(*sp);
714 }
715 count++;
716 simple_lock(&mountlist_slock);
717 nmp = mp->mnt_list.cqe_next;
718 vfs_unbusy(mp);
719 }
720 simple_unlock(&mountlist_slock);
721 if (sfsp && count > maxcount)
722 *retval = maxcount;
723 else
724 *retval = count;
725 return (0);
726 }
727
728 /*
729 * Change current working directory to a given file descriptor.
730 */
731 /* ARGSUSED */
732 int
733 sys_fchdir(p, v, retval)
734 struct proc *p;
735 void *v;
736 register_t *retval;
737 {
738 struct sys_fchdir_args /* {
739 syscallarg(int) fd;
740 } */ *uap = v;
741 struct filedesc *fdp = p->p_fd;
742 struct cwdinfo *cwdi = p->p_cwdi;
743 struct vnode *vp, *tdp;
744 struct mount *mp;
745 struct file *fp;
746 int error;
747
748 /* getvnode() will use the descriptor for us */
749 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
750 return (error);
751 vp = (struct vnode *)fp->f_data;
752
753 VREF(vp);
754 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
755 if (vp->v_type != VDIR)
756 error = ENOTDIR;
757 else
758 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
759 while (!error && (mp = vp->v_mountedhere) != NULL) {
760 if (vfs_busy(mp, 0, 0))
761 continue;
762 error = VFS_ROOT(mp, &tdp);
763 vfs_unbusy(mp);
764 if (error)
765 break;
766 vput(vp);
767 vp = tdp;
768 }
769 if (error) {
770 vput(vp);
771 goto out;
772 }
773 VOP_UNLOCK(vp, 0);
774
775 /*
776 * Disallow changing to a directory not under the process's
777 * current root directory (if there is one).
778 */
779 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
780 vrele(vp);
781 error = EPERM; /* operation not permitted */
782 goto out;
783 }
784
785 vrele(cwdi->cwdi_cdir);
786 cwdi->cwdi_cdir = vp;
787 out:
788 FILE_UNUSE(fp, p);
789 return (error);
790 }
791
792 /*
793 * Change this process's notion of the root directory to a given file descriptor.
794 */
795
796 int
797 sys_fchroot(p, v, retval)
798 struct proc *p;
799 void *v;
800 register_t *retval;
801 {
802 struct sys_fchroot_args *uap = v;
803 struct filedesc *fdp = p->p_fd;
804 struct cwdinfo *cwdi = p->p_cwdi;
805 struct vnode *vp;
806 struct file *fp;
807 int error;
808
809 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
810 return error;
811 /* getvnode() will use the descriptor for us */
812 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
813 return error;
814 vp = (struct vnode *) fp->f_data;
815 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
816 if (vp->v_type != VDIR)
817 error = ENOTDIR;
818 else
819 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
820 VOP_UNLOCK(vp, 0);
821 if (error)
822 goto out;
823 VREF(vp);
824
825 /*
826 * Prevent escaping from chroot by putting the root under
827 * the working directory. Silently chdir to / if we aren't
828 * already there.
829 */
830 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
831 /*
832 * XXX would be more failsafe to change directory to a
833 * deadfs node here instead
834 */
835 vrele(cwdi->cwdi_cdir);
836 VREF(vp);
837 cwdi->cwdi_cdir = vp;
838 }
839
840 if (cwdi->cwdi_rdir != NULL)
841 vrele(cwdi->cwdi_rdir);
842 cwdi->cwdi_rdir = vp;
843 out:
844 FILE_UNUSE(fp, p);
845 return (error);
846 }
847
848
849
850 /*
851 * Change current working directory (``.'').
852 */
853 /* ARGSUSED */
854 int
855 sys_chdir(p, v, retval)
856 struct proc *p;
857 void *v;
858 register_t *retval;
859 {
860 struct sys_chdir_args /* {
861 syscallarg(const char *) path;
862 } */ *uap = v;
863 struct cwdinfo *cwdi = p->p_cwdi;
864 int error;
865 struct nameidata nd;
866
867 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
868 SCARG(uap, path), p);
869 if ((error = change_dir(&nd, p)) != 0)
870 return (error);
871 vrele(cwdi->cwdi_cdir);
872 cwdi->cwdi_cdir = nd.ni_vp;
873 return (0);
874 }
875
876 /*
877 * Change notion of root (``/'') directory.
878 */
879 /* ARGSUSED */
880 int
881 sys_chroot(p, v, retval)
882 struct proc *p;
883 void *v;
884 register_t *retval;
885 {
886 struct sys_chroot_args /* {
887 syscallarg(const char *) path;
888 } */ *uap = v;
889 struct cwdinfo *cwdi = p->p_cwdi;
890 struct vnode *vp;
891 int error;
892 struct nameidata nd;
893
894 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
895 return (error);
896 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
897 SCARG(uap, path), p);
898 if ((error = change_dir(&nd, p)) != 0)
899 return (error);
900 if (cwdi->cwdi_rdir != NULL)
901 vrele(cwdi->cwdi_rdir);
902 vp = nd.ni_vp;
903 cwdi->cwdi_rdir = vp;
904
905 /*
906 * Prevent escaping from chroot by putting the root under
907 * the working directory. Silently chdir to / if we aren't
908 * already there.
909 */
910 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
911 /*
912 * XXX would be more failsafe to change directory to a
913 * deadfs node here instead
914 */
915 vrele(cwdi->cwdi_cdir);
916 VREF(vp);
917 cwdi->cwdi_cdir = vp;
918 }
919
920 return (0);
921 }
922
923 /*
924 * Common routine for chroot and chdir.
925 */
926 static int
927 change_dir(ndp, p)
928 register struct nameidata *ndp;
929 struct proc *p;
930 {
931 struct vnode *vp;
932 int error;
933
934 if ((error = namei(ndp)) != 0)
935 return (error);
936 vp = ndp->ni_vp;
937 if (vp->v_type != VDIR)
938 error = ENOTDIR;
939 else
940 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
941
942 if (error)
943 vput(vp);
944 else
945 VOP_UNLOCK(vp, 0);
946 return (error);
947 }
948
949 /*
950 * Check permissions, allocate an open file structure,
951 * and call the device open routine if any.
952 */
953 int
954 sys_open(p, v, retval)
955 struct proc *p;
956 void *v;
957 register_t *retval;
958 {
959 register struct sys_open_args /* {
960 syscallarg(const char *) path;
961 syscallarg(int) flags;
962 syscallarg(int) mode;
963 } */ *uap = v;
964 struct cwdinfo *cwdi = p->p_cwdi;
965 struct filedesc *fdp = p->p_fd;
966 struct file *fp;
967 struct vnode *vp;
968 int flags, cmode;
969 int type, indx, error;
970 struct flock lf;
971 struct nameidata nd;
972 extern struct fileops vnops;
973
974 flags = FFLAGS(SCARG(uap, flags));
975 if ((flags & (FREAD | FWRITE)) == 0)
976 return (EINVAL);
977 /* falloc() will use the file descriptor for us */
978 if ((error = falloc(p, &fp, &indx)) != 0)
979 return (error);
980 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
981 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
982 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
983 if ((error = vn_open(&nd, flags, cmode)) != 0) {
984 FILE_UNUSE(fp, p);
985 ffree(fp);
986 if ((error == ENODEV || error == ENXIO) &&
987 p->p_dupfd >= 0 && /* XXX from fdopen */
988 (error =
989 dupfdopen(p, indx, p->p_dupfd, flags, error)) == 0) {
990 *retval = indx;
991 return (0);
992 }
993 if (error == ERESTART)
994 error = EINTR;
995 fdp->fd_ofiles[indx] = NULL;
996 return (error);
997 }
998 p->p_dupfd = 0;
999 vp = nd.ni_vp;
1000 fp->f_flag = flags & FMASK;
1001 fp->f_type = DTYPE_VNODE;
1002 fp->f_ops = &vnops;
1003 fp->f_data = (caddr_t)vp;
1004 if (flags & (O_EXLOCK | O_SHLOCK)) {
1005 lf.l_whence = SEEK_SET;
1006 lf.l_start = 0;
1007 lf.l_len = 0;
1008 if (flags & O_EXLOCK)
1009 lf.l_type = F_WRLCK;
1010 else
1011 lf.l_type = F_RDLCK;
1012 type = F_FLOCK;
1013 if ((flags & FNONBLOCK) == 0)
1014 type |= F_WAIT;
1015 VOP_UNLOCK(vp, 0);
1016 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
1017 if (error) {
1018 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1019 FILE_UNUSE(fp, p);
1020 ffree(fp);
1021 fdp->fd_ofiles[indx] = NULL;
1022 return (error);
1023 }
1024 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1025 fp->f_flag |= FHASLOCK;
1026 }
1027 VOP_UNLOCK(vp, 0);
1028 *retval = indx;
1029 FILE_UNUSE(fp, p);
1030 return (0);
1031 }
1032
1033 /*
1034 * Get file handle system call
1035 */
1036 int
1037 sys_getfh(p, v, retval)
1038 struct proc *p;
1039 register void *v;
1040 register_t *retval;
1041 {
1042 register struct sys_getfh_args /* {
1043 syscallarg(char *) fname;
1044 syscallarg(fhandle_t *) fhp;
1045 } */ *uap = v;
1046 register struct vnode *vp;
1047 fhandle_t fh;
1048 int error;
1049 struct nameidata nd;
1050
1051 /*
1052 * Must be super user
1053 */
1054 error = suser(p->p_ucred, &p->p_acflag);
1055 if (error)
1056 return (error);
1057 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1058 SCARG(uap, fname), p);
1059 error = namei(&nd);
1060 if (error)
1061 return (error);
1062 vp = nd.ni_vp;
1063 memset((caddr_t)&fh, 0, sizeof(fh));
1064 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1065 error = VFS_VPTOFH(vp, &fh.fh_fid);
1066 vput(vp);
1067 if (error)
1068 return (error);
1069 error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
1070 return (error);
1071 }
1072
1073 /*
1074 * Open a file given a file handle.
1075 *
1076 * Check permissions, allocate an open file structure,
1077 * and call the device open routine if any.
1078 */
1079 int
1080 sys_fhopen(p, v, retval)
1081 struct proc *p;
1082 void *v;
1083 register_t *retval;
1084 {
1085 register struct sys_fhopen_args /* {
1086 syscallarg(const fhandle_t *) fhp;
1087 syscallarg(int) flags;
1088 } */ *uap = v;
1089 struct filedesc *fdp = p->p_fd;
1090 struct file *fp;
1091 struct vnode *vp = NULL;
1092 struct mount *mp;
1093 struct ucred *cred = p->p_ucred;
1094 int flags;
1095 struct file *nfp;
1096 int type, indx, error=0;
1097 struct flock lf;
1098 struct vattr va;
1099 fhandle_t fh;
1100 extern struct fileops vnops;
1101
1102 /*
1103 * Must be super user
1104 */
1105 if ((error = suser(p->p_ucred, &p->p_acflag)))
1106 return (error);
1107
1108 flags = FFLAGS(SCARG(uap, flags));
1109 if ((flags & (FREAD | FWRITE)) == 0)
1110 return (EINVAL);
1111 if ((flags & O_CREAT))
1112 return (EINVAL);
1113 /* falloc() will use the file descriptor for us */
1114 if ((error = falloc(p, &nfp, &indx)) != 0)
1115 return (error);
1116 fp = nfp;
1117 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1118 goto bad;
1119
1120 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
1121 error = ESTALE;
1122 goto bad;
1123 }
1124
1125 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
1126 vp = NULL; /* most likely unnecessary sanity for bad: */
1127 goto bad;
1128 }
1129
1130 /* Now do an effective vn_open */
1131
1132 if (vp->v_type == VSOCK) {
1133 error = EOPNOTSUPP;
1134 goto bad;
1135 }
1136 if (flags & FREAD) {
1137 if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
1138 goto bad;
1139 }
1140 if (flags & (FWRITE | O_TRUNC)) {
1141 if (vp->v_type == VDIR) {
1142 error = EISDIR;
1143 goto bad;
1144 }
1145 if ((error = vn_writechk(vp)) != 0 ||
1146 (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
1147 goto bad;
1148 }
1149 if (flags & O_TRUNC) {
1150 VOP_UNLOCK(vp, 0); /* XXX */
1151 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1152 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1153 VATTR_NULL(&va);
1154 va.va_size = 0;
1155 if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
1156 goto bad;
1157 }
1158 if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
1159 goto bad;
1160 if (flags & FWRITE)
1161 vp->v_writecount++;
1162
1163 /* done with modified vn_open, now finish what sys_open does. */
1164
1165 fp->f_flag = flags & FMASK;
1166 fp->f_type = DTYPE_VNODE;
1167 fp->f_ops = &vnops;
1168 fp->f_data = (caddr_t)vp;
1169 if (flags & (O_EXLOCK | O_SHLOCK)) {
1170 lf.l_whence = SEEK_SET;
1171 lf.l_start = 0;
1172 lf.l_len = 0;
1173 if (flags & O_EXLOCK)
1174 lf.l_type = F_WRLCK;
1175 else
1176 lf.l_type = F_RDLCK;
1177 type = F_FLOCK;
1178 if ((flags & FNONBLOCK) == 0)
1179 type |= F_WAIT;
1180 VOP_UNLOCK(vp, 0);
1181 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
1182 if (error) {
1183 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1184 FILE_UNUSE(fp, p);
1185 ffree(fp);
1186 fdp->fd_ofiles[indx] = NULL;
1187 return (error);
1188 }
1189 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1190 fp->f_flag |= FHASLOCK;
1191 }
1192 VOP_UNLOCK(vp, 0);
1193 *retval = indx;
1194 FILE_UNUSE(fp, p);
1195 return (0);
1196
1197 bad:
1198 FILE_UNUSE(fp, p);
1199 ffree(fp);
1200 fdp->fd_ofiles[indx] = NULL;
1201 if (vp != NULL)
1202 vput(vp);
1203 return (error);
1204 }
1205
1206 /* ARGSUSED */
1207 int
1208 sys_fhstat(p, v, retval)
1209 struct proc *p;
1210 void *v;
1211 register_t *retval;
1212 {
1213 register struct sys_fhstat_args /* {
1214 syscallarg(const fhandle_t *) fhp;
1215 syscallarg(struct stat *) sb;
1216 } */ *uap = v;
1217 struct stat sb;
1218 int error;
1219 fhandle_t fh;
1220 struct mount *mp;
1221 struct vnode *vp;
1222
1223 /*
1224 * Must be super user
1225 */
1226 if ((error = suser(p->p_ucred, &p->p_acflag)))
1227 return (error);
1228
1229 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1230 return (error);
1231
1232 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1233 return (ESTALE);
1234 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1235 return (error);
1236 error = vn_stat(vp, &sb, p);
1237 vput(vp);
1238 if (error)
1239 return (error);
1240 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
1241 return (error);
1242 }
1243
1244 /* ARGSUSED */
1245 int
1246 sys_fhstatfs(p, v, retval)
1247 struct proc *p;
1248 void *v;
1249 register_t *retval;
1250 {
1251 register struct sys_fhstatfs_args /*
1252 syscallarg(const fhandle_t *) fhp;
1253 syscallarg(struct statfs *) buf;
1254 } */ *uap = v;
1255 struct statfs sp;
1256 fhandle_t fh;
1257 struct mount *mp;
1258 struct vnode *vp;
1259 int error;
1260
1261 /*
1262 * Must be super user
1263 */
1264 if ((error = suser(p->p_ucred, &p->p_acflag)))
1265 return (error);
1266
1267 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1268 return (error);
1269
1270 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1271 return (ESTALE);
1272 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1273 return (error);
1274 mp = vp->v_mount;
1275 vput(vp);
1276 if ((error = VFS_STATFS(mp, &sp, p)) != 0)
1277 return (error);
1278 sp.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1279 return (copyout(&sp, SCARG(uap, buf), sizeof(sp)));
1280 }
1281
1282 /*
1283 * Create a special file.
1284 */
1285 /* ARGSUSED */
1286 int
1287 sys_mknod(p, v, retval)
1288 struct proc *p;
1289 void *v;
1290 register_t *retval;
1291 {
1292 register struct sys_mknod_args /* {
1293 syscallarg(const char *) path;
1294 syscallarg(int) mode;
1295 syscallarg(int) dev;
1296 } */ *uap = v;
1297 register struct vnode *vp;
1298 struct vattr vattr;
1299 int error;
1300 int whiteout = 0;
1301 struct nameidata nd;
1302
1303 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1304 return (error);
1305 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1306 if ((error = namei(&nd)) != 0)
1307 return (error);
1308 vp = nd.ni_vp;
1309 if (vp != NULL)
1310 error = EEXIST;
1311 else {
1312 VATTR_NULL(&vattr);
1313 vattr.va_mode =
1314 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1315 vattr.va_rdev = SCARG(uap, dev);
1316 whiteout = 0;
1317
1318 switch (SCARG(uap, mode) & S_IFMT) {
1319 case S_IFMT: /* used by badsect to flag bad sectors */
1320 vattr.va_type = VBAD;
1321 break;
1322 case S_IFCHR:
1323 vattr.va_type = VCHR;
1324 break;
1325 case S_IFBLK:
1326 vattr.va_type = VBLK;
1327 break;
1328 case S_IFWHT:
1329 whiteout = 1;
1330 break;
1331 default:
1332 error = EINVAL;
1333 break;
1334 }
1335 }
1336 if (!error) {
1337 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1338 if (whiteout) {
1339 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1340 if (error)
1341 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1342 vput(nd.ni_dvp);
1343 } else {
1344 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1345 &nd.ni_cnd, &vattr);
1346 }
1347 } else {
1348 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1349 if (nd.ni_dvp == vp)
1350 vrele(nd.ni_dvp);
1351 else
1352 vput(nd.ni_dvp);
1353 if (vp)
1354 vrele(vp);
1355 }
1356 return (error);
1357 }
1358
1359 /*
1360 * Create a named pipe.
1361 */
1362 /* ARGSUSED */
1363 int
1364 sys_mkfifo(p, v, retval)
1365 struct proc *p;
1366 void *v;
1367 register_t *retval;
1368 {
1369 register struct sys_mkfifo_args /* {
1370 syscallarg(const char *) path;
1371 syscallarg(int) mode;
1372 } */ *uap = v;
1373 struct vattr vattr;
1374 int error;
1375 struct nameidata nd;
1376
1377 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1378 if ((error = namei(&nd)) != 0)
1379 return (error);
1380 if (nd.ni_vp != NULL) {
1381 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1382 if (nd.ni_dvp == nd.ni_vp)
1383 vrele(nd.ni_dvp);
1384 else
1385 vput(nd.ni_dvp);
1386 vrele(nd.ni_vp);
1387 return (EEXIST);
1388 }
1389 VATTR_NULL(&vattr);
1390 vattr.va_type = VFIFO;
1391 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1392 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1393 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
1394 }
1395
1396 /*
1397 * Make a hard file link.
1398 */
1399 /* ARGSUSED */
1400 int
1401 sys_link(p, v, retval)
1402 struct proc *p;
1403 void *v;
1404 register_t *retval;
1405 {
1406 register struct sys_link_args /* {
1407 syscallarg(const char *) path;
1408 syscallarg(const char *) link;
1409 } */ *uap = v;
1410 register struct vnode *vp;
1411 struct nameidata nd;
1412 int error;
1413
1414 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1415 if ((error = namei(&nd)) != 0)
1416 return (error);
1417 vp = nd.ni_vp;
1418 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1419 if ((error = namei(&nd)) != 0)
1420 goto out;
1421 if (nd.ni_vp) {
1422 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1423 if (nd.ni_dvp == nd.ni_vp)
1424 vrele(nd.ni_dvp);
1425 else
1426 vput(nd.ni_dvp);
1427 vrele(nd.ni_vp);
1428 error = EEXIST;
1429 goto out;
1430 }
1431 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1432 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1433 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1434 out:
1435 vrele(vp);
1436 return (error);
1437 }
1438
1439 /*
1440 * Make a symbolic link.
1441 */
1442 /* ARGSUSED */
1443 int
1444 sys_symlink(p, v, retval)
1445 struct proc *p;
1446 void *v;
1447 register_t *retval;
1448 {
1449 register struct sys_symlink_args /* {
1450 syscallarg(const char *) path;
1451 syscallarg(const char *) link;
1452 } */ *uap = v;
1453 struct vattr vattr;
1454 char *path;
1455 int error;
1456 struct nameidata nd;
1457
1458 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1459 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1460 if (error)
1461 goto out;
1462 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1463 if ((error = namei(&nd)) != 0)
1464 goto out;
1465 if (nd.ni_vp) {
1466 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1467 if (nd.ni_dvp == nd.ni_vp)
1468 vrele(nd.ni_dvp);
1469 else
1470 vput(nd.ni_dvp);
1471 vrele(nd.ni_vp);
1472 error = EEXIST;
1473 goto out;
1474 }
1475 VATTR_NULL(&vattr);
1476 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1477 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1478 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1479 out:
1480 FREE(path, M_NAMEI);
1481 return (error);
1482 }
1483
1484 /*
1485 * Delete a whiteout from the filesystem.
1486 */
1487 /* ARGSUSED */
1488 int
1489 sys_undelete(p, v, retval)
1490 struct proc *p;
1491 void *v;
1492 register_t *retval;
1493 {
1494 register struct sys_undelete_args /* {
1495 syscallarg(const char *) path;
1496 } */ *uap = v;
1497 int error;
1498 struct nameidata nd;
1499
1500 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1501 SCARG(uap, path), p);
1502 error = namei(&nd);
1503 if (error)
1504 return (error);
1505
1506 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1507 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1508 if (nd.ni_dvp == nd.ni_vp)
1509 vrele(nd.ni_dvp);
1510 else
1511 vput(nd.ni_dvp);
1512 if (nd.ni_vp)
1513 vrele(nd.ni_vp);
1514 return (EEXIST);
1515 }
1516
1517 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1518 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1519 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1520 vput(nd.ni_dvp);
1521 return (error);
1522 }
1523
1524 /*
1525 * Delete a name from the filesystem.
1526 */
1527 /* ARGSUSED */
1528 int
1529 sys_unlink(p, v, retval)
1530 struct proc *p;
1531 void *v;
1532 register_t *retval;
1533 {
1534 struct sys_unlink_args /* {
1535 syscallarg(const char *) path;
1536 } */ *uap = v;
1537 register struct vnode *vp;
1538 int error;
1539 struct nameidata nd;
1540
1541 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1542 SCARG(uap, path), p);
1543 if ((error = namei(&nd)) != 0)
1544 return (error);
1545 vp = nd.ni_vp;
1546
1547 /*
1548 * The root of a mounted filesystem cannot be deleted.
1549 */
1550 if (vp->v_flag & VROOT) {
1551 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1552 if (nd.ni_dvp == vp)
1553 vrele(nd.ni_dvp);
1554 else
1555 vput(nd.ni_dvp);
1556 vput(vp);
1557 error = EBUSY;
1558 goto out;
1559 }
1560
1561 (void)uvm_vnp_uncache(vp);
1562
1563 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1564 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1565 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1566 out:
1567 return (error);
1568 }
1569
1570 /*
1571 * Reposition read/write file offset.
1572 */
1573 int
1574 sys_lseek(p, v, retval)
1575 struct proc *p;
1576 void *v;
1577 register_t *retval;
1578 {
1579 register struct sys_lseek_args /* {
1580 syscallarg(int) fd;
1581 syscallarg(int) pad;
1582 syscallarg(off_t) offset;
1583 syscallarg(int) whence;
1584 } */ *uap = v;
1585 struct ucred *cred = p->p_ucred;
1586 register struct filedesc *fdp = p->p_fd;
1587 register struct file *fp;
1588 struct vnode *vp;
1589 struct vattr vattr;
1590 register off_t newoff;
1591 int error;
1592
1593 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1594 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
1595 (fp->f_iflags & FIF_WANTCLOSE) != 0)
1596 return (EBADF);
1597
1598 FILE_USE(fp);
1599
1600 vp = (struct vnode *)fp->f_data;
1601 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1602 error = ESPIPE;
1603 goto out;
1604 }
1605
1606 switch (SCARG(uap, whence)) {
1607 case SEEK_CUR:
1608 newoff = fp->f_offset + SCARG(uap, offset);
1609 break;
1610 case SEEK_END:
1611 error = VOP_GETATTR(vp, &vattr, cred, p);
1612 if (error)
1613 goto out;
1614 newoff = SCARG(uap, offset) + vattr.va_size;
1615 break;
1616 case SEEK_SET:
1617 newoff = SCARG(uap, offset);
1618 break;
1619 default:
1620 error = EINVAL;
1621 goto out;
1622 }
1623 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1624 goto out;
1625
1626 *(off_t *)retval = fp->f_offset = newoff;
1627 out:
1628 FILE_UNUSE(fp, p);
1629 return (error);
1630 }
1631
1632 /*
1633 * Positional read system call.
1634 */
1635 int
1636 sys_pread(p, v, retval)
1637 struct proc *p;
1638 void *v;
1639 register_t *retval;
1640 {
1641 struct sys_pread_args /* {
1642 syscallarg(int) fd;
1643 syscallarg(void *) buf;
1644 syscallarg(size_t) nbyte;
1645 syscallarg(off_t) offset;
1646 } */ *uap = v;
1647 struct filedesc *fdp = p->p_fd;
1648 struct file *fp;
1649 struct vnode *vp;
1650 off_t offset;
1651 int error, fd = SCARG(uap, fd);
1652
1653 if ((u_int)fd >= fdp->fd_nfiles ||
1654 (fp = fdp->fd_ofiles[fd]) == NULL ||
1655 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1656 (fp->f_flag & FREAD) == 0)
1657 return (EBADF);
1658
1659 FILE_USE(fp);
1660
1661 vp = (struct vnode *)fp->f_data;
1662 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1663 error = ESPIPE;
1664 goto out;
1665 }
1666
1667 offset = SCARG(uap, offset);
1668
1669 /*
1670 * XXX This works because no file systems actually
1671 * XXX take any action on the seek operation.
1672 */
1673 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1674 goto out;
1675
1676 /* dofileread() will unuse the descriptor for us */
1677 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1678 &offset, 0, retval));
1679
1680 out:
1681 FILE_UNUSE(fp, p);
1682 return (error);
1683 }
1684
1685 /*
1686 * Positional scatter read system call.
1687 */
1688 int
1689 sys_preadv(p, v, retval)
1690 struct proc *p;
1691 void *v;
1692 register_t *retval;
1693 {
1694 struct sys_preadv_args /* {
1695 syscallarg(int) fd;
1696 syscallarg(const struct iovec *) iovp;
1697 syscallarg(int) iovcnt;
1698 syscallarg(off_t) offset;
1699 } */ *uap = v;
1700 struct filedesc *fdp = p->p_fd;
1701 struct file *fp;
1702 struct vnode *vp;
1703 off_t offset;
1704 int error, fd = SCARG(uap, fd);
1705
1706 if ((u_int)fd >= fdp->fd_nfiles ||
1707 (fp = fdp->fd_ofiles[fd]) == NULL ||
1708 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1709 (fp->f_flag & FREAD) == 0)
1710 return (EBADF);
1711
1712 FILE_USE(fp);
1713
1714 vp = (struct vnode *)fp->f_data;
1715 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1716 error = ESPIPE;
1717 goto out;
1718 }
1719
1720 offset = SCARG(uap, offset);
1721
1722 /*
1723 * XXX This works because no file systems actually
1724 * XXX take any action on the seek operation.
1725 */
1726 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1727 goto out;
1728
1729 /* dofilereadv() will unuse the descriptor for us */
1730 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1731 &offset, 0, retval));
1732
1733 out:
1734 FILE_UNUSE(fp, p);
1735 return (error);
1736 }
1737
1738 /*
1739 * Positional write system call.
1740 */
1741 int
1742 sys_pwrite(p, v, retval)
1743 struct proc *p;
1744 void *v;
1745 register_t *retval;
1746 {
1747 struct sys_pwrite_args /* {
1748 syscallarg(int) fd;
1749 syscallarg(const void *) buf;
1750 syscallarg(size_t) nbyte;
1751 syscallarg(off_t) offset;
1752 } */ *uap = v;
1753 struct filedesc *fdp = p->p_fd;
1754 struct file *fp;
1755 struct vnode *vp;
1756 off_t offset;
1757 int error, fd = SCARG(uap, fd);
1758
1759 if ((u_int)fd >= fdp->fd_nfiles ||
1760 (fp = fdp->fd_ofiles[fd]) == NULL ||
1761 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1762 (fp->f_flag & FWRITE) == 0)
1763 return (EBADF);
1764
1765 FILE_USE(fp);
1766
1767 vp = (struct vnode *)fp->f_data;
1768 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1769 error = ESPIPE;
1770 goto out;
1771 }
1772
1773 offset = SCARG(uap, offset);
1774
1775 /*
1776 * XXX This works because no file systems actually
1777 * XXX take any action on the seek operation.
1778 */
1779 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1780 goto out;
1781
1782 /* dofilewrite() will unuse the descriptor for us */
1783 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1784 &offset, 0, retval));
1785
1786 out:
1787 FILE_UNUSE(fp, p);
1788 return (error);
1789 }
1790
1791 /*
1792 * Positional gather write system call.
1793 */
1794 int
1795 sys_pwritev(p, v, retval)
1796 struct proc *p;
1797 void *v;
1798 register_t *retval;
1799 {
1800 struct sys_pwritev_args /* {
1801 syscallarg(int) fd;
1802 syscallarg(const struct iovec *) iovp;
1803 syscallarg(int) iovcnt;
1804 syscallarg(off_t) offset;
1805 } */ *uap = v;
1806 struct filedesc *fdp = p->p_fd;
1807 struct file *fp;
1808 struct vnode *vp;
1809 off_t offset;
1810 int error, fd = SCARG(uap, fd);
1811
1812 if ((u_int)fd >= fdp->fd_nfiles ||
1813 (fp = fdp->fd_ofiles[fd]) == NULL ||
1814 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1815 (fp->f_flag & FWRITE) == 0)
1816 return (EBADF);
1817
1818 FILE_USE(fp);
1819
1820 vp = (struct vnode *)fp->f_data;
1821 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1822 error = ESPIPE;
1823 goto out;
1824 }
1825
1826 offset = SCARG(uap, offset);
1827
1828 /*
1829 * XXX This works because no file systems actually
1830 * XXX take any action on the seek operation.
1831 */
1832 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1833 goto out;
1834
1835 /* dofilewritev() will unuse the descriptor for us */
1836 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1837 &offset, 0, retval));
1838
1839 out:
1840 FILE_UNUSE(fp, p);
1841 return (error);
1842 }
1843
1844 /*
1845 * Check access permissions.
1846 */
1847 int
1848 sys_access(p, v, retval)
1849 struct proc *p;
1850 void *v;
1851 register_t *retval;
1852 {
1853 register struct sys_access_args /* {
1854 syscallarg(const char *) path;
1855 syscallarg(int) flags;
1856 } */ *uap = v;
1857 register struct ucred *cred = p->p_ucred;
1858 register struct vnode *vp;
1859 int error, flags, t_gid, t_uid;
1860 struct nameidata nd;
1861
1862 t_uid = cred->cr_uid;
1863 t_gid = cred->cr_gid;
1864 cred->cr_uid = p->p_cred->p_ruid;
1865 cred->cr_gid = p->p_cred->p_rgid;
1866 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1867 SCARG(uap, path), p);
1868 if ((error = namei(&nd)) != 0)
1869 goto out1;
1870 vp = nd.ni_vp;
1871
1872 /* Flags == 0 means only check for existence. */
1873 if (SCARG(uap, flags)) {
1874 flags = 0;
1875 if (SCARG(uap, flags) & R_OK)
1876 flags |= VREAD;
1877 if (SCARG(uap, flags) & W_OK)
1878 flags |= VWRITE;
1879 if (SCARG(uap, flags) & X_OK)
1880 flags |= VEXEC;
1881
1882 error = VOP_ACCESS(vp, flags, cred, p);
1883 if (!error && (flags & VWRITE))
1884 error = vn_writechk(vp);
1885 }
1886 vput(vp);
1887 out1:
1888 cred->cr_uid = t_uid;
1889 cred->cr_gid = t_gid;
1890 return (error);
1891 }
1892
1893 /*
1894 * Get file status; this version follows links.
1895 */
1896 /* ARGSUSED */
1897 int
1898 sys___stat13(p, v, retval)
1899 struct proc *p;
1900 void *v;
1901 register_t *retval;
1902 {
1903 register struct sys___stat13_args /* {
1904 syscallarg(const char *) path;
1905 syscallarg(struct stat *) ub;
1906 } */ *uap = v;
1907 struct stat sb;
1908 int error;
1909 struct nameidata nd;
1910
1911 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1912 SCARG(uap, path), p);
1913 if ((error = namei(&nd)) != 0)
1914 return (error);
1915 error = vn_stat(nd.ni_vp, &sb, p);
1916 vput(nd.ni_vp);
1917 if (error)
1918 return (error);
1919 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1920 return (error);
1921 }
1922
1923 /*
1924 * Get file status; this version does not follow links.
1925 */
1926 /* ARGSUSED */
1927 int
1928 sys___lstat13(p, v, retval)
1929 struct proc *p;
1930 void *v;
1931 register_t *retval;
1932 {
1933 register struct sys___lstat13_args /* {
1934 syscallarg(const char *) path;
1935 syscallarg(struct stat *) ub;
1936 } */ *uap = v;
1937 struct stat sb;
1938 int error;
1939 struct nameidata nd;
1940
1941 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1942 SCARG(uap, path), p);
1943 if ((error = namei(&nd)) != 0)
1944 return (error);
1945 error = vn_stat(nd.ni_vp, &sb, p);
1946 vput(nd.ni_vp);
1947 if (error)
1948 return (error);
1949 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1950 return (error);
1951 }
1952
1953 /*
1954 * Get configurable pathname variables.
1955 */
1956 /* ARGSUSED */
1957 int
1958 sys_pathconf(p, v, retval)
1959 struct proc *p;
1960 void *v;
1961 register_t *retval;
1962 {
1963 register struct sys_pathconf_args /* {
1964 syscallarg(const char *) path;
1965 syscallarg(int) name;
1966 } */ *uap = v;
1967 int error;
1968 struct nameidata nd;
1969
1970 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1971 SCARG(uap, path), p);
1972 if ((error = namei(&nd)) != 0)
1973 return (error);
1974 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1975 vput(nd.ni_vp);
1976 return (error);
1977 }
1978
1979 /*
1980 * Return target name of a symbolic link.
1981 */
1982 /* ARGSUSED */
1983 int
1984 sys_readlink(p, v, retval)
1985 struct proc *p;
1986 void *v;
1987 register_t *retval;
1988 {
1989 register struct sys_readlink_args /* {
1990 syscallarg(const char *) path;
1991 syscallarg(char *) buf;
1992 syscallarg(size_t) count;
1993 } */ *uap = v;
1994 register struct vnode *vp;
1995 struct iovec aiov;
1996 struct uio auio;
1997 int error;
1998 struct nameidata nd;
1999
2000 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
2001 SCARG(uap, path), p);
2002 if ((error = namei(&nd)) != 0)
2003 return (error);
2004 vp = nd.ni_vp;
2005 if (vp->v_type != VLNK)
2006 error = EINVAL;
2007 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2008 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
2009 aiov.iov_base = SCARG(uap, buf);
2010 aiov.iov_len = SCARG(uap, count);
2011 auio.uio_iov = &aiov;
2012 auio.uio_iovcnt = 1;
2013 auio.uio_offset = 0;
2014 auio.uio_rw = UIO_READ;
2015 auio.uio_segflg = UIO_USERSPACE;
2016 auio.uio_procp = p;
2017 auio.uio_resid = SCARG(uap, count);
2018 error = VOP_READLINK(vp, &auio, p->p_ucred);
2019 }
2020 vput(vp);
2021 *retval = SCARG(uap, count) - auio.uio_resid;
2022 return (error);
2023 }
2024
2025 /*
2026 * Change flags of a file given a path name.
2027 */
2028 /* ARGSUSED */
2029 int
2030 sys_chflags(p, v, retval)
2031 struct proc *p;
2032 void *v;
2033 register_t *retval;
2034 {
2035 register struct sys_chflags_args /* {
2036 syscallarg(const char *) path;
2037 syscallarg(u_long) flags;
2038 } */ *uap = v;
2039 register struct vnode *vp;
2040 struct vattr vattr;
2041 int error;
2042 struct nameidata nd;
2043
2044 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2045 if ((error = namei(&nd)) != 0)
2046 return (error);
2047 vp = nd.ni_vp;
2048 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2049 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2050 /* Non-superusers cannot change the flags on devices, even if they
2051 own them. */
2052 if (suser(p->p_ucred, &p->p_acflag)) {
2053 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2054 goto out;
2055 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2056 error = EINVAL;
2057 goto out;
2058 }
2059 }
2060 VATTR_NULL(&vattr);
2061 vattr.va_flags = SCARG(uap, flags);
2062 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2063 out:
2064 vput(vp);
2065 return (error);
2066 }
2067
2068 /*
2069 * Change flags of a file given a file descriptor.
2070 */
2071 /* ARGSUSED */
2072 int
2073 sys_fchflags(p, v, retval)
2074 struct proc *p;
2075 void *v;
2076 register_t *retval;
2077 {
2078 register struct sys_fchflags_args /* {
2079 syscallarg(int) fd;
2080 syscallarg(u_long) flags;
2081 } */ *uap = v;
2082 struct vattr vattr;
2083 struct vnode *vp;
2084 struct file *fp;
2085 int error;
2086
2087 /* getvnode() will use the descriptor for us */
2088 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2089 return (error);
2090 vp = (struct vnode *)fp->f_data;
2091 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2092 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2093 /* Non-superusers cannot change the flags on devices, even if they
2094 own them. */
2095 if (suser(p->p_ucred, &p->p_acflag)) {
2096 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
2097 != 0)
2098 goto out;
2099 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2100 error = EINVAL;
2101 goto out;
2102 }
2103 }
2104 VATTR_NULL(&vattr);
2105 vattr.va_flags = SCARG(uap, flags);
2106 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2107 out:
2108 VOP_UNLOCK(vp, 0);
2109 FILE_UNUSE(fp, p);
2110 return (error);
2111 }
2112
2113 /*
2114 * Change mode of a file given path name; this version follows links.
2115 */
2116 /* ARGSUSED */
2117 int
2118 sys_chmod(p, v, retval)
2119 struct proc *p;
2120 void *v;
2121 register_t *retval;
2122 {
2123 register struct sys_chmod_args /* {
2124 syscallarg(const char *) path;
2125 syscallarg(int) mode;
2126 } */ *uap = v;
2127 int error;
2128 struct nameidata nd;
2129
2130 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2131 if ((error = namei(&nd)) != 0)
2132 return (error);
2133
2134 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2135
2136 vrele(nd.ni_vp);
2137 return (error);
2138 }
2139
2140 /*
2141 * Change mode of a file given a file descriptor.
2142 */
2143 /* ARGSUSED */
2144 int
2145 sys_fchmod(p, v, retval)
2146 struct proc *p;
2147 void *v;
2148 register_t *retval;
2149 {
2150 register struct sys_fchmod_args /* {
2151 syscallarg(int) fd;
2152 syscallarg(int) mode;
2153 } */ *uap = v;
2154 struct file *fp;
2155 int error;
2156
2157 /* getvnode() will use the descriptor for us */
2158 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2159 return (error);
2160
2161 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
2162 FILE_UNUSE(fp, p);
2163 return (error);
2164 }
2165
2166 /*
2167 * Change mode of a file given path name; this version does not follow links.
2168 */
2169 /* ARGSUSED */
2170 int
2171 sys_lchmod(p, v, retval)
2172 struct proc *p;
2173 void *v;
2174 register_t *retval;
2175 {
2176 register struct sys_lchmod_args /* {
2177 syscallarg(const char *) path;
2178 syscallarg(int) mode;
2179 } */ *uap = v;
2180 int error;
2181 struct nameidata nd;
2182
2183 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2184 if ((error = namei(&nd)) != 0)
2185 return (error);
2186
2187 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2188
2189 vrele(nd.ni_vp);
2190 return (error);
2191 }
2192
2193 /*
2194 * Common routine to set mode given a vnode.
2195 */
2196 static int
2197 change_mode(vp, mode, p)
2198 struct vnode *vp;
2199 int mode;
2200 struct proc *p;
2201 {
2202 struct vattr vattr;
2203 int error;
2204
2205 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2206 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2207 VATTR_NULL(&vattr);
2208 vattr.va_mode = mode & ALLPERMS;
2209 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2210 VOP_UNLOCK(vp, 0);
2211 return (error);
2212 }
2213
2214 /*
2215 * Set ownership given a path name; this version follows links.
2216 */
2217 /* ARGSUSED */
2218 int
2219 sys_chown(p, v, retval)
2220 struct proc *p;
2221 void *v;
2222 register_t *retval;
2223 {
2224 register struct sys_chown_args /* {
2225 syscallarg(const char *) path;
2226 syscallarg(uid_t) uid;
2227 syscallarg(gid_t) gid;
2228 } */ *uap = v;
2229 int error;
2230 struct nameidata nd;
2231
2232 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2233 if ((error = namei(&nd)) != 0)
2234 return (error);
2235
2236 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2237
2238 vrele(nd.ni_vp);
2239 return (error);
2240 }
2241
2242 /*
2243 * Set ownership given a path name; this version follows links.
2244 * Provides POSIX semantics.
2245 */
2246 /* ARGSUSED */
2247 int
2248 sys___posix_chown(p, v, retval)
2249 struct proc *p;
2250 void *v;
2251 register_t *retval;
2252 {
2253 register struct sys_chown_args /* {
2254 syscallarg(const char *) path;
2255 syscallarg(uid_t) uid;
2256 syscallarg(gid_t) gid;
2257 } */ *uap = v;
2258 int error;
2259 struct nameidata nd;
2260
2261 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2262 if ((error = namei(&nd)) != 0)
2263 return (error);
2264
2265 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2266
2267 vrele(nd.ni_vp);
2268 return (error);
2269 }
2270
2271 /*
2272 * Set ownership given a file descriptor.
2273 */
2274 /* ARGSUSED */
2275 int
2276 sys_fchown(p, v, retval)
2277 struct proc *p;
2278 void *v;
2279 register_t *retval;
2280 {
2281 register struct sys_fchown_args /* {
2282 syscallarg(int) fd;
2283 syscallarg(uid_t) uid;
2284 syscallarg(gid_t) gid;
2285 } */ *uap = v;
2286 int error;
2287 struct file *fp;
2288
2289 /* getvnode() will use the descriptor for us */
2290 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2291 return (error);
2292
2293 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2294 SCARG(uap, gid), p, 0);
2295 FILE_UNUSE(fp, p);
2296 return (error);
2297 }
2298
2299 /*
2300 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2301 */
2302 /* ARGSUSED */
2303 int
2304 sys___posix_fchown(p, v, retval)
2305 struct proc *p;
2306 void *v;
2307 register_t *retval;
2308 {
2309 register struct sys_fchown_args /* {
2310 syscallarg(int) fd;
2311 syscallarg(uid_t) uid;
2312 syscallarg(gid_t) gid;
2313 } */ *uap = v;
2314 int error;
2315 struct file *fp;
2316
2317 /* getvnode() will use the descriptor for us */
2318 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2319 return (error);
2320
2321 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2322 SCARG(uap, gid), p, 1);
2323 FILE_UNUSE(fp, p);
2324 return (error);
2325 }
2326
2327 /*
2328 * Set ownership given a path name; this version does not follow links.
2329 */
2330 /* ARGSUSED */
2331 int
2332 sys_lchown(p, v, retval)
2333 struct proc *p;
2334 void *v;
2335 register_t *retval;
2336 {
2337 register struct sys_lchown_args /* {
2338 syscallarg(const char *) path;
2339 syscallarg(uid_t) uid;
2340 syscallarg(gid_t) gid;
2341 } */ *uap = v;
2342 int error;
2343 struct nameidata nd;
2344
2345 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2346 if ((error = namei(&nd)) != 0)
2347 return (error);
2348
2349 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2350
2351 vrele(nd.ni_vp);
2352 return (error);
2353 }
2354
2355 /*
2356 * Set ownership given a path name; this version does not follow links.
2357 * Provides POSIX/XPG semantics.
2358 */
2359 /* ARGSUSED */
2360 int
2361 sys___posix_lchown(p, v, retval)
2362 struct proc *p;
2363 void *v;
2364 register_t *retval;
2365 {
2366 register struct sys_lchown_args /* {
2367 syscallarg(const char *) path;
2368 syscallarg(uid_t) uid;
2369 syscallarg(gid_t) gid;
2370 } */ *uap = v;
2371 int error;
2372 struct nameidata nd;
2373
2374 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2375 if ((error = namei(&nd)) != 0)
2376 return (error);
2377
2378 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2379
2380 vrele(nd.ni_vp);
2381 return (error);
2382 }
2383
2384 /*
2385 * Common routine to set ownership given a vnode.
2386 */
2387 static int
2388 change_owner(vp, uid, gid, p, posix_semantics)
2389 register struct vnode *vp;
2390 uid_t uid;
2391 gid_t gid;
2392 struct proc *p;
2393 int posix_semantics;
2394 {
2395 struct vattr vattr;
2396 mode_t newmode;
2397 int error;
2398
2399 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2400 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2401 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2402 goto out;
2403
2404 #define CHANGED(x) ((x) != -1)
2405 newmode = vattr.va_mode;
2406 if (posix_semantics) {
2407 /*
2408 * POSIX/XPG semantics: if the caller is not the super-user,
2409 * clear set-user-id and set-group-id bits. Both POSIX and
2410 * the XPG consider the behaviour for calls by the super-user
2411 * implementation-defined; we leave the set-user-id and set-
2412 * group-id settings intact in that case.
2413 */
2414 if (suser(p->p_ucred, NULL) != 0)
2415 newmode &= ~(S_ISUID | S_ISGID);
2416 } else {
2417 /*
2418 * NetBSD semantics: when changing owner and/or group,
2419 * clear the respective bit(s).
2420 */
2421 if (CHANGED(uid))
2422 newmode &= ~S_ISUID;
2423 if (CHANGED(gid))
2424 newmode &= ~S_ISGID;
2425 }
2426 /* Update va_mode iff altered. */
2427 if (vattr.va_mode == newmode)
2428 newmode = VNOVAL;
2429
2430 VATTR_NULL(&vattr);
2431 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
2432 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
2433 vattr.va_mode = newmode;
2434 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2435 #undef CHANGED
2436
2437 out:
2438 VOP_UNLOCK(vp, 0);
2439 return (error);
2440 }
2441
2442 /*
2443 * Set the access and modification times given a path name; this
2444 * version follows links.
2445 */
2446 /* ARGSUSED */
2447 int
2448 sys_utimes(p, v, retval)
2449 struct proc *p;
2450 void *v;
2451 register_t *retval;
2452 {
2453 register struct sys_utimes_args /* {
2454 syscallarg(const char *) path;
2455 syscallarg(const struct timeval *) tptr;
2456 } */ *uap = v;
2457 int error;
2458 struct nameidata nd;
2459
2460 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2461 if ((error = namei(&nd)) != 0)
2462 return (error);
2463
2464 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2465
2466 vrele(nd.ni_vp);
2467 return (error);
2468 }
2469
2470 /*
2471 * Set the access and modification times given a file descriptor.
2472 */
2473 /* ARGSUSED */
2474 int
2475 sys_futimes(p, v, retval)
2476 struct proc *p;
2477 void *v;
2478 register_t *retval;
2479 {
2480 register struct sys_futimes_args /* {
2481 syscallarg(int) fd;
2482 syscallarg(const struct timeval *) tptr;
2483 } */ *uap = v;
2484 int error;
2485 struct file *fp;
2486
2487 /* getvnode() will use the descriptor for us */
2488 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2489 return (error);
2490
2491 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
2492 FILE_UNUSE(fp, p);
2493 return (error);
2494 }
2495
2496 /*
2497 * Set the access and modification times given a path name; this
2498 * version does not follow links.
2499 */
2500 /* ARGSUSED */
2501 int
2502 sys_lutimes(p, v, retval)
2503 struct proc *p;
2504 void *v;
2505 register_t *retval;
2506 {
2507 register struct sys_lutimes_args /* {
2508 syscallarg(const char *) path;
2509 syscallarg(const struct timeval *) tptr;
2510 } */ *uap = v;
2511 int error;
2512 struct nameidata nd;
2513
2514 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2515 if ((error = namei(&nd)) != 0)
2516 return (error);
2517
2518 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2519
2520 vrele(nd.ni_vp);
2521 return (error);
2522 }
2523
2524 /*
2525 * Common routine to set access and modification times given a vnode.
2526 */
2527 static int
2528 change_utimes(vp, tptr, p)
2529 struct vnode *vp;
2530 const struct timeval *tptr;
2531 struct proc *p;
2532 {
2533 struct timeval tv[2];
2534 struct vattr vattr;
2535 int error;
2536
2537 VATTR_NULL(&vattr);
2538 if (tptr == NULL) {
2539 microtime(&tv[0]);
2540 tv[1] = tv[0];
2541 vattr.va_vaflags |= VA_UTIMES_NULL;
2542 } else {
2543 error = copyin(tptr, tv, sizeof(tv));
2544 if (error)
2545 return (error);
2546 }
2547 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2548 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2549 vattr.va_atime.tv_sec = tv[0].tv_sec;
2550 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2551 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2552 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2553 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2554 VOP_UNLOCK(vp, 0);
2555 return (error);
2556 }
2557
2558 /*
2559 * Truncate a file given its path name.
2560 */
2561 /* ARGSUSED */
2562 int
2563 sys_truncate(p, v, retval)
2564 struct proc *p;
2565 void *v;
2566 register_t *retval;
2567 {
2568 register struct sys_truncate_args /* {
2569 syscallarg(const char *) path;
2570 syscallarg(int) pad;
2571 syscallarg(off_t) length;
2572 } */ *uap = v;
2573 register struct vnode *vp;
2574 struct vattr vattr;
2575 int error;
2576 struct nameidata nd;
2577
2578 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2579 if ((error = namei(&nd)) != 0)
2580 return (error);
2581 vp = nd.ni_vp;
2582 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2583 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2584 if (vp->v_type == VDIR)
2585 error = EISDIR;
2586 else if ((error = vn_writechk(vp)) == 0 &&
2587 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2588 VATTR_NULL(&vattr);
2589 vattr.va_size = SCARG(uap, length);
2590 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2591 }
2592 vput(vp);
2593 return (error);
2594 }
2595
2596 /*
2597 * Truncate a file given a file descriptor.
2598 */
2599 /* ARGSUSED */
2600 int
2601 sys_ftruncate(p, v, retval)
2602 struct proc *p;
2603 void *v;
2604 register_t *retval;
2605 {
2606 register struct sys_ftruncate_args /* {
2607 syscallarg(int) fd;
2608 syscallarg(int) pad;
2609 syscallarg(off_t) length;
2610 } */ *uap = v;
2611 struct vattr vattr;
2612 struct vnode *vp;
2613 struct file *fp;
2614 int error;
2615
2616 /* getvnode() will use the descriptor for us */
2617 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2618 return (error);
2619 if ((fp->f_flag & FWRITE) == 0) {
2620 error = EINVAL;
2621 goto out;
2622 }
2623 vp = (struct vnode *)fp->f_data;
2624 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2625 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2626 if (vp->v_type == VDIR)
2627 error = EISDIR;
2628 else if ((error = vn_writechk(vp)) == 0) {
2629 VATTR_NULL(&vattr);
2630 vattr.va_size = SCARG(uap, length);
2631 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2632 }
2633 VOP_UNLOCK(vp, 0);
2634 out:
2635 FILE_UNUSE(fp, p);
2636 return (error);
2637 }
2638
2639 /*
2640 * Sync an open file.
2641 */
2642 /* ARGSUSED */
2643 int
2644 sys_fsync(p, v, retval)
2645 struct proc *p;
2646 void *v;
2647 register_t *retval;
2648 {
2649 struct sys_fsync_args /* {
2650 syscallarg(int) fd;
2651 } */ *uap = v;
2652 register struct vnode *vp;
2653 struct file *fp;
2654 int error;
2655
2656 /* getvnode() will use the descriptor for us */
2657 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2658 return (error);
2659 vp = (struct vnode *)fp->f_data;
2660 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2661 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
2662 if (error == 0 && bioops.io_fsync != NULL)
2663 (*bioops.io_fsync)(vp);
2664 VOP_UNLOCK(vp, 0);
2665 FILE_UNUSE(fp, p);
2666 return (error);
2667 }
2668
2669 /*
2670 * Sync the data of an open file.
2671 */
2672 /* ARGSUSED */
2673 int
2674 sys_fdatasync(p, v, retval)
2675 struct proc *p;
2676 void *v;
2677 register_t *retval;
2678 {
2679 struct sys_fdatasync_args /* {
2680 syscallarg(int) fd;
2681 } */ *uap = v;
2682 struct vnode *vp;
2683 struct file *fp;
2684 int error;
2685
2686 /* getvnode() will use the descriptor for us */
2687 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2688 return (error);
2689 vp = (struct vnode *)fp->f_data;
2690 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2691 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
2692 if (error == 0 && bioops.io_fsync != NULL)
2693 (*bioops.io_fsync)(vp);
2694 VOP_UNLOCK(vp, 0);
2695 FILE_UNUSE(fp, p);
2696 return (error);
2697 }
2698
2699 /*
2700 * Rename files, (standard) BSD semantics frontend.
2701 */
2702 /* ARGSUSED */
2703 int
2704 sys_rename(p, v, retval)
2705 struct proc *p;
2706 void *v;
2707 register_t *retval;
2708 {
2709 register struct sys_rename_args /* {
2710 syscallarg(const char *) from;
2711 syscallarg(const char *) to;
2712 } */ *uap = v;
2713
2714 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2715 }
2716
2717 /*
2718 * Rename files, POSIX semantics frontend.
2719 */
2720 /* ARGSUSED */
2721 int
2722 sys___posix_rename(p, v, retval)
2723 struct proc *p;
2724 void *v;
2725 register_t *retval;
2726 {
2727 register struct sys___posix_rename_args /* {
2728 syscallarg(const char *) from;
2729 syscallarg(const char *) to;
2730 } */ *uap = v;
2731
2732 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2733 }
2734
2735 /*
2736 * Rename files. Source and destination must either both be directories,
2737 * or both not be directories. If target is a directory, it must be empty.
2738 * If `from' and `to' refer to the same object, the value of the `retain'
2739 * argument is used to determine whether `from' will be
2740 *
2741 * (retain == 0) deleted unless `from' and `to' refer to the same
2742 * object in the file system's name space (BSD).
2743 * (retain == 1) always retained (POSIX).
2744 */
2745 static int
2746 rename_files(from, to, p, retain)
2747 const char *from, *to;
2748 struct proc *p;
2749 int retain;
2750 {
2751 register struct vnode *tvp, *fvp, *tdvp;
2752 struct nameidata fromnd, tond;
2753 int error;
2754
2755 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2756 from, p);
2757 if ((error = namei(&fromnd)) != 0)
2758 return (error);
2759 fvp = fromnd.ni_vp;
2760 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2761 UIO_USERSPACE, to, p);
2762 if ((error = namei(&tond)) != 0) {
2763 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2764 vrele(fromnd.ni_dvp);
2765 vrele(fvp);
2766 goto out1;
2767 }
2768 tdvp = tond.ni_dvp;
2769 tvp = tond.ni_vp;
2770
2771 if (tvp != NULL) {
2772 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2773 error = ENOTDIR;
2774 goto out;
2775 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2776 error = EISDIR;
2777 goto out;
2778 }
2779 }
2780
2781 if (fvp == tdvp)
2782 error = EINVAL;
2783
2784 /*
2785 * Source and destination refer to the same object.
2786 */
2787 if (fvp == tvp) {
2788 if (retain)
2789 error = -1;
2790 else if (fromnd.ni_dvp == tdvp &&
2791 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2792 !memcmp(fromnd.ni_cnd.cn_nameptr,
2793 tond.ni_cnd.cn_nameptr,
2794 fromnd.ni_cnd.cn_namelen))
2795 error = -1;
2796 }
2797
2798 out:
2799 if (!error) {
2800 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2801 if (fromnd.ni_dvp != tdvp)
2802 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2803 if (tvp) {
2804 (void)uvm_vnp_uncache(tvp);
2805 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2806 }
2807 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2808 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2809 } else {
2810 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2811 if (tdvp == tvp)
2812 vrele(tdvp);
2813 else
2814 vput(tdvp);
2815 if (tvp)
2816 vput(tvp);
2817 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2818 vrele(fromnd.ni_dvp);
2819 vrele(fvp);
2820 }
2821 vrele(tond.ni_startdir);
2822 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
2823 out1:
2824 if (fromnd.ni_startdir)
2825 vrele(fromnd.ni_startdir);
2826 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
2827 return (error == -1 ? 0 : error);
2828 }
2829
2830 /*
2831 * Make a directory file.
2832 */
2833 /* ARGSUSED */
2834 int
2835 sys_mkdir(p, v, retval)
2836 struct proc *p;
2837 void *v;
2838 register_t *retval;
2839 {
2840 register struct sys_mkdir_args /* {
2841 syscallarg(const char *) path;
2842 syscallarg(int) mode;
2843 } */ *uap = v;
2844 register struct vnode *vp;
2845 struct vattr vattr;
2846 int error;
2847 struct nameidata nd;
2848
2849 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2850 if ((error = namei(&nd)) != 0)
2851 return (error);
2852 vp = nd.ni_vp;
2853 if (vp != NULL) {
2854 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2855 if (nd.ni_dvp == vp)
2856 vrele(nd.ni_dvp);
2857 else
2858 vput(nd.ni_dvp);
2859 vrele(vp);
2860 return (EEXIST);
2861 }
2862 VATTR_NULL(&vattr);
2863 vattr.va_type = VDIR;
2864 vattr.va_mode =
2865 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
2866 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2867 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2868 if (!error)
2869 vput(nd.ni_vp);
2870 return (error);
2871 }
2872
2873 /*
2874 * Remove a directory file.
2875 */
2876 /* ARGSUSED */
2877 int
2878 sys_rmdir(p, v, retval)
2879 struct proc *p;
2880 void *v;
2881 register_t *retval;
2882 {
2883 struct sys_rmdir_args /* {
2884 syscallarg(const char *) path;
2885 } */ *uap = v;
2886 register struct vnode *vp;
2887 int error;
2888 struct nameidata nd;
2889
2890 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2891 SCARG(uap, path), p);
2892 if ((error = namei(&nd)) != 0)
2893 return (error);
2894 vp = nd.ni_vp;
2895 if (vp->v_type != VDIR) {
2896 error = ENOTDIR;
2897 goto out;
2898 }
2899 /*
2900 * No rmdir "." please.
2901 */
2902 if (nd.ni_dvp == vp) {
2903 error = EINVAL;
2904 goto out;
2905 }
2906 /*
2907 * The root of a mounted filesystem cannot be deleted.
2908 */
2909 if (vp->v_flag & VROOT)
2910 error = EBUSY;
2911 out:
2912 if (!error) {
2913 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2914 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2915 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2916 } else {
2917 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2918 if (nd.ni_dvp == vp)
2919 vrele(nd.ni_dvp);
2920 else
2921 vput(nd.ni_dvp);
2922 vput(vp);
2923 }
2924 return (error);
2925 }
2926
2927 /*
2928 * Read a block of directory entries in a file system independent format.
2929 */
2930 int
2931 sys_getdents(p, v, retval)
2932 struct proc *p;
2933 void *v;
2934 register_t *retval;
2935 {
2936 register struct sys_getdents_args /* {
2937 syscallarg(int) fd;
2938 syscallarg(char *) buf;
2939 syscallarg(size_t) count;
2940 } */ *uap = v;
2941 struct file *fp;
2942 int error, done;
2943
2944 /* getvnode() will use the descriptor for us */
2945 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2946 return (error);
2947 if ((fp->f_flag & FREAD) == 0) {
2948 error = EBADF;
2949 goto out;
2950 }
2951 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
2952 SCARG(uap, count), &done, p, 0, 0);
2953 *retval = done;
2954 out:
2955 FILE_UNUSE(fp, p);
2956 return (error);
2957 }
2958
2959 /*
2960 * Set the mode mask for creation of filesystem nodes.
2961 */
2962 int
2963 sys_umask(p, v, retval)
2964 struct proc *p;
2965 void *v;
2966 register_t *retval;
2967 {
2968 struct sys_umask_args /* {
2969 syscallarg(mode_t) newmask;
2970 } */ *uap = v;
2971 struct cwdinfo *cwdi;
2972
2973 cwdi = p->p_cwdi;
2974 *retval = cwdi->cwdi_cmask;
2975 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
2976 return (0);
2977 }
2978
2979 /*
2980 * Void all references to file by ripping underlying filesystem
2981 * away from vnode.
2982 */
2983 /* ARGSUSED */
2984 int
2985 sys_revoke(p, v, retval)
2986 struct proc *p;
2987 void *v;
2988 register_t *retval;
2989 {
2990 register struct sys_revoke_args /* {
2991 syscallarg(const char *) path;
2992 } */ *uap = v;
2993 register struct vnode *vp;
2994 struct vattr vattr;
2995 int error;
2996 struct nameidata nd;
2997
2998 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2999 if ((error = namei(&nd)) != 0)
3000 return (error);
3001 vp = nd.ni_vp;
3002 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
3003 goto out;
3004 if (p->p_ucred->cr_uid != vattr.va_uid &&
3005 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
3006 goto out;
3007 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
3008 VOP_REVOKE(vp, REVOKEALL);
3009 out:
3010 vrele(vp);
3011 return (error);
3012 }
3013
3014 /*
3015 * Convert a user file descriptor to a kernel file entry.
3016 */
3017 int
3018 getvnode(fdp, fd, fpp)
3019 struct filedesc *fdp;
3020 int fd;
3021 struct file **fpp;
3022 {
3023 struct vnode *vp;
3024 struct file *fp;
3025
3026 if ((u_int)fd >= fdp->fd_nfiles ||
3027 (fp = fdp->fd_ofiles[fd]) == NULL ||
3028 (fp->f_iflags & FIF_WANTCLOSE) != 0)
3029 return (EBADF);
3030
3031 FILE_USE(fp);
3032
3033 if (fp->f_type != DTYPE_VNODE) {
3034 FILE_UNUSE(fp, NULL);
3035 return (EINVAL);
3036 }
3037
3038 vp = (struct vnode *)fp->f_data;
3039 if (vp->v_type == VBAD) {
3040 FILE_UNUSE(fp, NULL);
3041 return (EBADF);
3042 }
3043
3044 *fpp = fp;
3045 return (0);
3046 }
3047