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