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