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