vfs_syscalls.c revision 1.168 1 /* $NetBSD: vfs_syscalls.c,v 1.168 2001/07/24 15:39:31 assar 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;
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 if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
1190 goto bad;
1191 if (vp->v_type == VREG &&
1192 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
1193 error = EIO;
1194 goto bad;
1195 }
1196 if (flags & FWRITE)
1197 vp->v_writecount++;
1198
1199 /* done with modified vn_open, now finish what sys_open does. */
1200
1201 fp->f_flag = flags & FMASK;
1202 fp->f_type = DTYPE_VNODE;
1203 fp->f_ops = &vnops;
1204 fp->f_data = (caddr_t)vp;
1205 if (flags & (O_EXLOCK | O_SHLOCK)) {
1206 lf.l_whence = SEEK_SET;
1207 lf.l_start = 0;
1208 lf.l_len = 0;
1209 if (flags & O_EXLOCK)
1210 lf.l_type = F_WRLCK;
1211 else
1212 lf.l_type = F_RDLCK;
1213 type = F_FLOCK;
1214 if ((flags & FNONBLOCK) == 0)
1215 type |= F_WAIT;
1216 VOP_UNLOCK(vp, 0);
1217 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
1218 if (error) {
1219 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1220 FILE_UNUSE(fp, p);
1221 ffree(fp);
1222 fdremove(fdp, indx);
1223 return (error);
1224 }
1225 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1226 fp->f_flag |= FHASLOCK;
1227 }
1228 VOP_UNLOCK(vp, 0);
1229 *retval = indx;
1230 FILE_SET_MATURE(fp);
1231 FILE_UNUSE(fp, p);
1232 return (0);
1233
1234 bad:
1235 FILE_UNUSE(fp, p);
1236 ffree(fp);
1237 fdremove(fdp, indx);
1238 if (vp != NULL)
1239 vput(vp);
1240 return (error);
1241 }
1242
1243 /* ARGSUSED */
1244 int
1245 sys_fhstat(p, v, retval)
1246 struct proc *p;
1247 void *v;
1248 register_t *retval;
1249 {
1250 struct sys_fhstat_args /* {
1251 syscallarg(const fhandle_t *) fhp;
1252 syscallarg(struct stat *) sb;
1253 } */ *uap = v;
1254 struct stat sb;
1255 int error;
1256 fhandle_t fh;
1257 struct mount *mp;
1258 struct vnode *vp;
1259
1260 /*
1261 * Must be super user
1262 */
1263 if ((error = suser(p->p_ucred, &p->p_acflag)))
1264 return (error);
1265
1266 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1267 return (error);
1268
1269 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1270 return (ESTALE);
1271 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1272 return (error);
1273 error = vn_stat(vp, &sb, p);
1274 vput(vp);
1275 if (error)
1276 return (error);
1277 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
1278 return (error);
1279 }
1280
1281 /* ARGSUSED */
1282 int
1283 sys_fhstatfs(p, v, retval)
1284 struct proc *p;
1285 void *v;
1286 register_t *retval;
1287 {
1288 struct sys_fhstatfs_args /*
1289 syscallarg(const fhandle_t *) fhp;
1290 syscallarg(struct statfs *) buf;
1291 } */ *uap = v;
1292 struct statfs sp;
1293 fhandle_t fh;
1294 struct mount *mp;
1295 struct vnode *vp;
1296 int error;
1297
1298 /*
1299 * Must be super user
1300 */
1301 if ((error = suser(p->p_ucred, &p->p_acflag)))
1302 return (error);
1303
1304 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1305 return (error);
1306
1307 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1308 return (ESTALE);
1309 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1310 return (error);
1311 mp = vp->v_mount;
1312 vput(vp);
1313 if ((error = VFS_STATFS(mp, &sp, p)) != 0)
1314 return (error);
1315 sp.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1316 sp.f_oflags = sp.f_flags & 0xffff;
1317 return (copyout(&sp, SCARG(uap, buf), sizeof(sp)));
1318 }
1319
1320 /*
1321 * Create a special file.
1322 */
1323 /* ARGSUSED */
1324 int
1325 sys_mknod(p, v, retval)
1326 struct proc *p;
1327 void *v;
1328 register_t *retval;
1329 {
1330 struct sys_mknod_args /* {
1331 syscallarg(const char *) path;
1332 syscallarg(int) mode;
1333 syscallarg(int) dev;
1334 } */ *uap = v;
1335 struct vnode *vp;
1336 struct vattr vattr;
1337 int error;
1338 int whiteout = 0;
1339 struct nameidata nd;
1340
1341 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1342 return (error);
1343 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1344 if ((error = namei(&nd)) != 0)
1345 return (error);
1346 vp = nd.ni_vp;
1347 if (vp != NULL)
1348 error = EEXIST;
1349 else {
1350 VATTR_NULL(&vattr);
1351 vattr.va_mode =
1352 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1353 vattr.va_rdev = SCARG(uap, dev);
1354 whiteout = 0;
1355
1356 switch (SCARG(uap, mode) & S_IFMT) {
1357 case S_IFMT: /* used by badsect to flag bad sectors */
1358 vattr.va_type = VBAD;
1359 break;
1360 case S_IFCHR:
1361 vattr.va_type = VCHR;
1362 break;
1363 case S_IFBLK:
1364 vattr.va_type = VBLK;
1365 break;
1366 case S_IFWHT:
1367 whiteout = 1;
1368 break;
1369 default:
1370 error = EINVAL;
1371 break;
1372 }
1373 }
1374 if (!error) {
1375 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1376 if (whiteout) {
1377 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1378 if (error)
1379 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1380 vput(nd.ni_dvp);
1381 } else {
1382 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1383 &nd.ni_cnd, &vattr);
1384 if (error == 0)
1385 vput(nd.ni_vp);
1386 }
1387 } else {
1388 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1389 if (nd.ni_dvp == vp)
1390 vrele(nd.ni_dvp);
1391 else
1392 vput(nd.ni_dvp);
1393 if (vp)
1394 vrele(vp);
1395 }
1396 return (error);
1397 }
1398
1399 /*
1400 * Create a named pipe.
1401 */
1402 /* ARGSUSED */
1403 int
1404 sys_mkfifo(p, v, retval)
1405 struct proc *p;
1406 void *v;
1407 register_t *retval;
1408 {
1409 struct sys_mkfifo_args /* {
1410 syscallarg(const char *) path;
1411 syscallarg(int) mode;
1412 } */ *uap = v;
1413 struct vattr vattr;
1414 int error;
1415 struct nameidata nd;
1416
1417 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1418 if ((error = namei(&nd)) != 0)
1419 return (error);
1420 if (nd.ni_vp != NULL) {
1421 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1422 if (nd.ni_dvp == nd.ni_vp)
1423 vrele(nd.ni_dvp);
1424 else
1425 vput(nd.ni_dvp);
1426 vrele(nd.ni_vp);
1427 return (EEXIST);
1428 }
1429 VATTR_NULL(&vattr);
1430 vattr.va_type = VFIFO;
1431 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1432 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1433 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1434 if (error == 0)
1435 vput(nd.ni_vp);
1436 return (error);
1437 }
1438
1439 /*
1440 * Make a hard file link.
1441 */
1442 /* ARGSUSED */
1443 int
1444 sys_link(p, v, retval)
1445 struct proc *p;
1446 void *v;
1447 register_t *retval;
1448 {
1449 struct sys_link_args /* {
1450 syscallarg(const char *) path;
1451 syscallarg(const char *) link;
1452 } */ *uap = v;
1453 struct vnode *vp;
1454 struct nameidata nd;
1455 int error;
1456
1457 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1458 if ((error = namei(&nd)) != 0)
1459 return (error);
1460 vp = nd.ni_vp;
1461 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1462 if ((error = namei(&nd)) != 0)
1463 goto out;
1464 if (nd.ni_vp) {
1465 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1466 if (nd.ni_dvp == nd.ni_vp)
1467 vrele(nd.ni_dvp);
1468 else
1469 vput(nd.ni_dvp);
1470 vrele(nd.ni_vp);
1471 error = EEXIST;
1472 goto out;
1473 }
1474 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1475 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1476 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1477 out:
1478 vrele(vp);
1479 return (error);
1480 }
1481
1482 /*
1483 * Make a symbolic link.
1484 */
1485 /* ARGSUSED */
1486 int
1487 sys_symlink(p, v, retval)
1488 struct proc *p;
1489 void *v;
1490 register_t *retval;
1491 {
1492 struct sys_symlink_args /* {
1493 syscallarg(const char *) path;
1494 syscallarg(const char *) link;
1495 } */ *uap = v;
1496 struct vattr vattr;
1497 char *path;
1498 int error;
1499 struct nameidata nd;
1500
1501 path = PNBUF_GET();
1502 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1503 if (error)
1504 goto out;
1505 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1506 if ((error = namei(&nd)) != 0)
1507 goto out;
1508 if (nd.ni_vp) {
1509 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1510 if (nd.ni_dvp == nd.ni_vp)
1511 vrele(nd.ni_dvp);
1512 else
1513 vput(nd.ni_dvp);
1514 vrele(nd.ni_vp);
1515 error = EEXIST;
1516 goto out;
1517 }
1518 VATTR_NULL(&vattr);
1519 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1520 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1521 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1522 if (error == 0)
1523 vput(nd.ni_vp);
1524 out:
1525 PNBUF_PUT(path);
1526 return (error);
1527 }
1528
1529 /*
1530 * Delete a whiteout from the filesystem.
1531 */
1532 /* ARGSUSED */
1533 int
1534 sys_undelete(p, v, retval)
1535 struct proc *p;
1536 void *v;
1537 register_t *retval;
1538 {
1539 struct sys_undelete_args /* {
1540 syscallarg(const char *) path;
1541 } */ *uap = v;
1542 int error;
1543 struct nameidata nd;
1544
1545 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1546 SCARG(uap, path), p);
1547 error = namei(&nd);
1548 if (error)
1549 return (error);
1550
1551 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1552 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1553 if (nd.ni_dvp == nd.ni_vp)
1554 vrele(nd.ni_dvp);
1555 else
1556 vput(nd.ni_dvp);
1557 if (nd.ni_vp)
1558 vrele(nd.ni_vp);
1559 return (EEXIST);
1560 }
1561
1562 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1563 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1564 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1565 vput(nd.ni_dvp);
1566 return (error);
1567 }
1568
1569 /*
1570 * Delete a name from the filesystem.
1571 */
1572 /* ARGSUSED */
1573 int
1574 sys_unlink(p, v, retval)
1575 struct proc *p;
1576 void *v;
1577 register_t *retval;
1578 {
1579 struct sys_unlink_args /* {
1580 syscallarg(const char *) path;
1581 } */ *uap = v;
1582 struct vnode *vp;
1583 int error;
1584 struct nameidata nd;
1585
1586 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1587 SCARG(uap, path), p);
1588 if ((error = namei(&nd)) != 0)
1589 return (error);
1590 vp = nd.ni_vp;
1591
1592 /*
1593 * The root of a mounted filesystem cannot be deleted.
1594 */
1595 if (vp->v_flag & VROOT) {
1596 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1597 if (nd.ni_dvp == vp)
1598 vrele(nd.ni_dvp);
1599 else
1600 vput(nd.ni_dvp);
1601 vput(vp);
1602 error = EBUSY;
1603 goto out;
1604 }
1605
1606 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1607 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1608 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1609 out:
1610 return (error);
1611 }
1612
1613 /*
1614 * Reposition read/write file offset.
1615 */
1616 int
1617 sys_lseek(p, v, retval)
1618 struct proc *p;
1619 void *v;
1620 register_t *retval;
1621 {
1622 struct sys_lseek_args /* {
1623 syscallarg(int) fd;
1624 syscallarg(int) pad;
1625 syscallarg(off_t) offset;
1626 syscallarg(int) whence;
1627 } */ *uap = v;
1628 struct ucred *cred = p->p_ucred;
1629 struct filedesc *fdp = p->p_fd;
1630 struct file *fp;
1631 struct vnode *vp;
1632 struct vattr vattr;
1633 off_t newoff;
1634 int error;
1635
1636 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
1637 return (EBADF);
1638
1639 FILE_USE(fp);
1640
1641 vp = (struct vnode *)fp->f_data;
1642 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1643 error = ESPIPE;
1644 goto out;
1645 }
1646
1647 switch (SCARG(uap, whence)) {
1648 case SEEK_CUR:
1649 newoff = fp->f_offset + SCARG(uap, offset);
1650 break;
1651 case SEEK_END:
1652 error = VOP_GETATTR(vp, &vattr, cred, p);
1653 if (error)
1654 goto out;
1655 newoff = SCARG(uap, offset) + vattr.va_size;
1656 break;
1657 case SEEK_SET:
1658 newoff = SCARG(uap, offset);
1659 break;
1660 default:
1661 error = EINVAL;
1662 goto out;
1663 }
1664 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1665 goto out;
1666
1667 *(off_t *)retval = fp->f_offset = newoff;
1668 out:
1669 FILE_UNUSE(fp, p);
1670 return (error);
1671 }
1672
1673 /*
1674 * Positional read system call.
1675 */
1676 int
1677 sys_pread(p, v, retval)
1678 struct proc *p;
1679 void *v;
1680 register_t *retval;
1681 {
1682 struct sys_pread_args /* {
1683 syscallarg(int) fd;
1684 syscallarg(void *) buf;
1685 syscallarg(size_t) nbyte;
1686 syscallarg(off_t) offset;
1687 } */ *uap = v;
1688 struct filedesc *fdp = p->p_fd;
1689 struct file *fp;
1690 struct vnode *vp;
1691 off_t offset;
1692 int error, fd = SCARG(uap, fd);
1693
1694 if ((fp = fd_getfile(fdp, fd)) == NULL)
1695 return (EBADF);
1696
1697 if ((fp->f_flag & FREAD) == 0)
1698 return (EBADF);
1699
1700 FILE_USE(fp);
1701
1702 vp = (struct vnode *)fp->f_data;
1703 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1704 error = ESPIPE;
1705 goto out;
1706 }
1707
1708 offset = SCARG(uap, offset);
1709
1710 /*
1711 * XXX This works because no file systems actually
1712 * XXX take any action on the seek operation.
1713 */
1714 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1715 goto out;
1716
1717 /* dofileread() will unuse the descriptor for us */
1718 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1719 &offset, 0, retval));
1720
1721 out:
1722 FILE_UNUSE(fp, p);
1723 return (error);
1724 }
1725
1726 /*
1727 * Positional scatter read system call.
1728 */
1729 int
1730 sys_preadv(p, v, retval)
1731 struct proc *p;
1732 void *v;
1733 register_t *retval;
1734 {
1735 struct sys_preadv_args /* {
1736 syscallarg(int) fd;
1737 syscallarg(const struct iovec *) iovp;
1738 syscallarg(int) iovcnt;
1739 syscallarg(off_t) offset;
1740 } */ *uap = v;
1741 struct filedesc *fdp = p->p_fd;
1742 struct file *fp;
1743 struct vnode *vp;
1744 off_t offset;
1745 int error, fd = SCARG(uap, fd);
1746
1747 if ((fp = fd_getfile(fdp, fd)) == NULL)
1748 return (EBADF);
1749
1750 if ((fp->f_flag & FREAD) == 0)
1751 return (EBADF);
1752
1753 FILE_USE(fp);
1754
1755 vp = (struct vnode *)fp->f_data;
1756 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1757 error = ESPIPE;
1758 goto out;
1759 }
1760
1761 offset = SCARG(uap, offset);
1762
1763 /*
1764 * XXX This works because no file systems actually
1765 * XXX take any action on the seek operation.
1766 */
1767 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1768 goto out;
1769
1770 /* dofilereadv() will unuse the descriptor for us */
1771 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1772 &offset, 0, retval));
1773
1774 out:
1775 FILE_UNUSE(fp, p);
1776 return (error);
1777 }
1778
1779 /*
1780 * Positional write system call.
1781 */
1782 int
1783 sys_pwrite(p, v, retval)
1784 struct proc *p;
1785 void *v;
1786 register_t *retval;
1787 {
1788 struct sys_pwrite_args /* {
1789 syscallarg(int) fd;
1790 syscallarg(const void *) buf;
1791 syscallarg(size_t) nbyte;
1792 syscallarg(off_t) offset;
1793 } */ *uap = v;
1794 struct filedesc *fdp = p->p_fd;
1795 struct file *fp;
1796 struct vnode *vp;
1797 off_t offset;
1798 int error, fd = SCARG(uap, fd);
1799
1800 if ((fp = fd_getfile(fdp, fd)) == NULL)
1801 return (EBADF);
1802
1803 if ((fp->f_flag & FWRITE) == 0)
1804 return (EBADF);
1805
1806 FILE_USE(fp);
1807
1808 vp = (struct vnode *)fp->f_data;
1809 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1810 error = ESPIPE;
1811 goto out;
1812 }
1813
1814 offset = SCARG(uap, offset);
1815
1816 /*
1817 * XXX This works because no file systems actually
1818 * XXX take any action on the seek operation.
1819 */
1820 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1821 goto out;
1822
1823 /* dofilewrite() will unuse the descriptor for us */
1824 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1825 &offset, 0, retval));
1826
1827 out:
1828 FILE_UNUSE(fp, p);
1829 return (error);
1830 }
1831
1832 /*
1833 * Positional gather write system call.
1834 */
1835 int
1836 sys_pwritev(p, v, retval)
1837 struct proc *p;
1838 void *v;
1839 register_t *retval;
1840 {
1841 struct sys_pwritev_args /* {
1842 syscallarg(int) fd;
1843 syscallarg(const struct iovec *) iovp;
1844 syscallarg(int) iovcnt;
1845 syscallarg(off_t) offset;
1846 } */ *uap = v;
1847 struct filedesc *fdp = p->p_fd;
1848 struct file *fp;
1849 struct vnode *vp;
1850 off_t offset;
1851 int error, fd = SCARG(uap, fd);
1852
1853 if ((fp = fd_getfile(fdp, fd)) == NULL)
1854 return (EBADF);
1855
1856 if ((fp->f_flag & FWRITE) == 0)
1857 return (EBADF);
1858
1859 FILE_USE(fp);
1860
1861 vp = (struct vnode *)fp->f_data;
1862 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1863 error = ESPIPE;
1864 goto out;
1865 }
1866
1867 offset = SCARG(uap, offset);
1868
1869 /*
1870 * XXX This works because no file systems actually
1871 * XXX take any action on the seek operation.
1872 */
1873 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1874 goto out;
1875
1876 /* dofilewritev() will unuse the descriptor for us */
1877 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1878 &offset, 0, retval));
1879
1880 out:
1881 FILE_UNUSE(fp, p);
1882 return (error);
1883 }
1884
1885 /*
1886 * Check access permissions.
1887 */
1888 int
1889 sys_access(p, v, retval)
1890 struct proc *p;
1891 void *v;
1892 register_t *retval;
1893 {
1894 struct sys_access_args /* {
1895 syscallarg(const char *) path;
1896 syscallarg(int) flags;
1897 } */ *uap = v;
1898 struct ucred *cred = p->p_ucred;
1899 struct vnode *vp;
1900 int error, flags, t_gid, t_uid;
1901 struct nameidata nd;
1902
1903 t_uid = cred->cr_uid;
1904 t_gid = cred->cr_gid;
1905 cred->cr_uid = p->p_cred->p_ruid;
1906 cred->cr_gid = p->p_cred->p_rgid;
1907 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1908 SCARG(uap, path), p);
1909 if ((error = namei(&nd)) != 0)
1910 goto out1;
1911 vp = nd.ni_vp;
1912
1913 /* Flags == 0 means only check for existence. */
1914 if (SCARG(uap, flags)) {
1915 flags = 0;
1916 if (SCARG(uap, flags) & R_OK)
1917 flags |= VREAD;
1918 if (SCARG(uap, flags) & W_OK)
1919 flags |= VWRITE;
1920 if (SCARG(uap, flags) & X_OK)
1921 flags |= VEXEC;
1922
1923 error = VOP_ACCESS(vp, flags, cred, p);
1924 if (!error && (flags & VWRITE))
1925 error = vn_writechk(vp);
1926 }
1927 vput(vp);
1928 out1:
1929 cred->cr_uid = t_uid;
1930 cred->cr_gid = t_gid;
1931 return (error);
1932 }
1933
1934 /*
1935 * Get file status; this version follows links.
1936 */
1937 /* ARGSUSED */
1938 int
1939 sys___stat13(p, v, retval)
1940 struct proc *p;
1941 void *v;
1942 register_t *retval;
1943 {
1944 struct sys___stat13_args /* {
1945 syscallarg(const char *) path;
1946 syscallarg(struct stat *) ub;
1947 } */ *uap = v;
1948 struct stat sb;
1949 int error;
1950 struct nameidata nd;
1951
1952 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1953 SCARG(uap, path), p);
1954 if ((error = namei(&nd)) != 0)
1955 return (error);
1956 error = vn_stat(nd.ni_vp, &sb, p);
1957 vput(nd.ni_vp);
1958 if (error)
1959 return (error);
1960 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1961 return (error);
1962 }
1963
1964 /*
1965 * Get file status; this version does not follow links.
1966 */
1967 /* ARGSUSED */
1968 int
1969 sys___lstat13(p, v, retval)
1970 struct proc *p;
1971 void *v;
1972 register_t *retval;
1973 {
1974 struct sys___lstat13_args /* {
1975 syscallarg(const char *) path;
1976 syscallarg(struct stat *) ub;
1977 } */ *uap = v;
1978 struct stat sb;
1979 int error;
1980 struct nameidata nd;
1981
1982 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1983 SCARG(uap, path), p);
1984 if ((error = namei(&nd)) != 0)
1985 return (error);
1986 error = vn_stat(nd.ni_vp, &sb, p);
1987 vput(nd.ni_vp);
1988 if (error)
1989 return (error);
1990 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1991 return (error);
1992 }
1993
1994 /*
1995 * Get configurable pathname variables.
1996 */
1997 /* ARGSUSED */
1998 int
1999 sys_pathconf(p, v, retval)
2000 struct proc *p;
2001 void *v;
2002 register_t *retval;
2003 {
2004 struct sys_pathconf_args /* {
2005 syscallarg(const char *) path;
2006 syscallarg(int) name;
2007 } */ *uap = v;
2008 int error;
2009 struct nameidata nd;
2010
2011 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2012 SCARG(uap, path), p);
2013 if ((error = namei(&nd)) != 0)
2014 return (error);
2015 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2016 vput(nd.ni_vp);
2017 return (error);
2018 }
2019
2020 /*
2021 * Return target name of a symbolic link.
2022 */
2023 /* ARGSUSED */
2024 int
2025 sys_readlink(p, v, retval)
2026 struct proc *p;
2027 void *v;
2028 register_t *retval;
2029 {
2030 struct sys_readlink_args /* {
2031 syscallarg(const char *) path;
2032 syscallarg(char *) buf;
2033 syscallarg(size_t) count;
2034 } */ *uap = v;
2035 struct vnode *vp;
2036 struct iovec aiov;
2037 struct uio auio;
2038 int error;
2039 struct nameidata nd;
2040
2041 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
2042 SCARG(uap, path), p);
2043 if ((error = namei(&nd)) != 0)
2044 return (error);
2045 vp = nd.ni_vp;
2046 if (vp->v_type != VLNK)
2047 error = EINVAL;
2048 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2049 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
2050 aiov.iov_base = SCARG(uap, buf);
2051 aiov.iov_len = SCARG(uap, count);
2052 auio.uio_iov = &aiov;
2053 auio.uio_iovcnt = 1;
2054 auio.uio_offset = 0;
2055 auio.uio_rw = UIO_READ;
2056 auio.uio_segflg = UIO_USERSPACE;
2057 auio.uio_procp = p;
2058 auio.uio_resid = SCARG(uap, count);
2059 error = VOP_READLINK(vp, &auio, p->p_ucred);
2060 }
2061 vput(vp);
2062 *retval = SCARG(uap, count) - auio.uio_resid;
2063 return (error);
2064 }
2065
2066 /*
2067 * Change flags of a file given a path name.
2068 */
2069 /* ARGSUSED */
2070 int
2071 sys_chflags(p, v, retval)
2072 struct proc *p;
2073 void *v;
2074 register_t *retval;
2075 {
2076 struct sys_chflags_args /* {
2077 syscallarg(const char *) path;
2078 syscallarg(u_long) flags;
2079 } */ *uap = v;
2080 struct vnode *vp;
2081 int error;
2082 struct nameidata nd;
2083
2084 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2085 if ((error = namei(&nd)) != 0)
2086 return (error);
2087 vp = nd.ni_vp;
2088 error = change_flags(vp, SCARG(uap, flags), p);
2089 vput(vp);
2090 return (error);
2091 }
2092
2093 /*
2094 * Change flags of a file given a file descriptor.
2095 */
2096 /* ARGSUSED */
2097 int
2098 sys_fchflags(p, v, retval)
2099 struct proc *p;
2100 void *v;
2101 register_t *retval;
2102 {
2103 struct sys_fchflags_args /* {
2104 syscallarg(int) fd;
2105 syscallarg(u_long) flags;
2106 } */ *uap = v;
2107 struct vnode *vp;
2108 struct file *fp;
2109 int error;
2110
2111 /* getvnode() will use the descriptor for us */
2112 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2113 return (error);
2114 vp = (struct vnode *)fp->f_data;
2115 error = change_flags(vp, SCARG(uap, flags), p);
2116 VOP_UNLOCK(vp, 0);
2117 FILE_UNUSE(fp, p);
2118 return (error);
2119 }
2120
2121 /*
2122 * Change flags of a file given a path name; this version does
2123 * not follow links.
2124 */
2125 int
2126 sys_lchflags(p, v, retval)
2127 struct proc *p;
2128 void *v;
2129 register_t *retval;
2130 {
2131 struct sys_lchflags_args /* {
2132 syscallarg(const char *) path;
2133 syscallarg(u_long) flags;
2134 } */ *uap = v;
2135 struct vnode *vp;
2136 int error;
2137 struct nameidata nd;
2138
2139 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2140 if ((error = namei(&nd)) != 0)
2141 return (error);
2142 vp = nd.ni_vp;
2143 error = change_flags(vp, SCARG(uap, flags), p);
2144 vput(vp);
2145 return (error);
2146 }
2147
2148 /*
2149 * Common routine to change flags of a file.
2150 */
2151 int
2152 change_flags(vp, flags, p)
2153 struct vnode *vp;
2154 u_long flags;
2155 struct proc *p;
2156 {
2157 struct vattr vattr;
2158 int error;
2159
2160 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2161 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2162 /*
2163 * Non-superusers cannot change the flags on devices, even if they
2164 * own them.
2165 */
2166 if (suser(p->p_ucred, &p->p_acflag) != 0) {
2167 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2168 goto out;
2169 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2170 error = EINVAL;
2171 goto out;
2172 }
2173 }
2174 VATTR_NULL(&vattr);
2175 vattr.va_flags = flags;
2176 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2177 out:
2178 return (error);
2179 }
2180
2181 /*
2182 * Change mode of a file given path name; this version follows links.
2183 */
2184 /* ARGSUSED */
2185 int
2186 sys_chmod(p, v, retval)
2187 struct proc *p;
2188 void *v;
2189 register_t *retval;
2190 {
2191 struct sys_chmod_args /* {
2192 syscallarg(const char *) path;
2193 syscallarg(int) mode;
2194 } */ *uap = v;
2195 int error;
2196 struct nameidata nd;
2197
2198 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2199 if ((error = namei(&nd)) != 0)
2200 return (error);
2201
2202 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2203
2204 vrele(nd.ni_vp);
2205 return (error);
2206 }
2207
2208 /*
2209 * Change mode of a file given a file descriptor.
2210 */
2211 /* ARGSUSED */
2212 int
2213 sys_fchmod(p, v, retval)
2214 struct proc *p;
2215 void *v;
2216 register_t *retval;
2217 {
2218 struct sys_fchmod_args /* {
2219 syscallarg(int) fd;
2220 syscallarg(int) mode;
2221 } */ *uap = v;
2222 struct file *fp;
2223 int error;
2224
2225 /* getvnode() will use the descriptor for us */
2226 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2227 return (error);
2228
2229 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
2230 FILE_UNUSE(fp, p);
2231 return (error);
2232 }
2233
2234 /*
2235 * Change mode of a file given path name; this version does not follow links.
2236 */
2237 /* ARGSUSED */
2238 int
2239 sys_lchmod(p, v, retval)
2240 struct proc *p;
2241 void *v;
2242 register_t *retval;
2243 {
2244 struct sys_lchmod_args /* {
2245 syscallarg(const char *) path;
2246 syscallarg(int) mode;
2247 } */ *uap = v;
2248 int error;
2249 struct nameidata nd;
2250
2251 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2252 if ((error = namei(&nd)) != 0)
2253 return (error);
2254
2255 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2256
2257 vrele(nd.ni_vp);
2258 return (error);
2259 }
2260
2261 /*
2262 * Common routine to set mode given a vnode.
2263 */
2264 static int
2265 change_mode(vp, mode, p)
2266 struct vnode *vp;
2267 int mode;
2268 struct proc *p;
2269 {
2270 struct vattr vattr;
2271 int error;
2272
2273 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2274 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2275 VATTR_NULL(&vattr);
2276 vattr.va_mode = mode & ALLPERMS;
2277 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2278 VOP_UNLOCK(vp, 0);
2279 return (error);
2280 }
2281
2282 /*
2283 * Set ownership given a path name; this version follows links.
2284 */
2285 /* ARGSUSED */
2286 int
2287 sys_chown(p, v, retval)
2288 struct proc *p;
2289 void *v;
2290 register_t *retval;
2291 {
2292 struct sys_chown_args /* {
2293 syscallarg(const char *) path;
2294 syscallarg(uid_t) uid;
2295 syscallarg(gid_t) gid;
2296 } */ *uap = v;
2297 int error;
2298 struct nameidata nd;
2299
2300 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2301 if ((error = namei(&nd)) != 0)
2302 return (error);
2303
2304 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2305
2306 vrele(nd.ni_vp);
2307 return (error);
2308 }
2309
2310 /*
2311 * Set ownership given a path name; this version follows links.
2312 * Provides POSIX semantics.
2313 */
2314 /* ARGSUSED */
2315 int
2316 sys___posix_chown(p, v, retval)
2317 struct proc *p;
2318 void *v;
2319 register_t *retval;
2320 {
2321 struct sys_chown_args /* {
2322 syscallarg(const char *) path;
2323 syscallarg(uid_t) uid;
2324 syscallarg(gid_t) gid;
2325 } */ *uap = v;
2326 int error;
2327 struct nameidata nd;
2328
2329 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2330 if ((error = namei(&nd)) != 0)
2331 return (error);
2332
2333 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2334
2335 vrele(nd.ni_vp);
2336 return (error);
2337 }
2338
2339 /*
2340 * Set ownership given a file descriptor.
2341 */
2342 /* ARGSUSED */
2343 int
2344 sys_fchown(p, v, retval)
2345 struct proc *p;
2346 void *v;
2347 register_t *retval;
2348 {
2349 struct sys_fchown_args /* {
2350 syscallarg(int) fd;
2351 syscallarg(uid_t) uid;
2352 syscallarg(gid_t) gid;
2353 } */ *uap = v;
2354 int error;
2355 struct file *fp;
2356
2357 /* getvnode() will use the descriptor for us */
2358 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2359 return (error);
2360
2361 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2362 SCARG(uap, gid), p, 0);
2363 FILE_UNUSE(fp, p);
2364 return (error);
2365 }
2366
2367 /*
2368 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2369 */
2370 /* ARGSUSED */
2371 int
2372 sys___posix_fchown(p, v, retval)
2373 struct proc *p;
2374 void *v;
2375 register_t *retval;
2376 {
2377 struct sys_fchown_args /* {
2378 syscallarg(int) fd;
2379 syscallarg(uid_t) uid;
2380 syscallarg(gid_t) gid;
2381 } */ *uap = v;
2382 int error;
2383 struct file *fp;
2384
2385 /* getvnode() will use the descriptor for us */
2386 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2387 return (error);
2388
2389 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2390 SCARG(uap, gid), p, 1);
2391 FILE_UNUSE(fp, p);
2392 return (error);
2393 }
2394
2395 /*
2396 * Set ownership given a path name; this version does not follow links.
2397 */
2398 /* ARGSUSED */
2399 int
2400 sys_lchown(p, v, retval)
2401 struct proc *p;
2402 void *v;
2403 register_t *retval;
2404 {
2405 struct sys_lchown_args /* {
2406 syscallarg(const char *) path;
2407 syscallarg(uid_t) uid;
2408 syscallarg(gid_t) gid;
2409 } */ *uap = v;
2410 int error;
2411 struct nameidata nd;
2412
2413 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2414 if ((error = namei(&nd)) != 0)
2415 return (error);
2416
2417 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2418
2419 vrele(nd.ni_vp);
2420 return (error);
2421 }
2422
2423 /*
2424 * Set ownership given a path name; this version does not follow links.
2425 * Provides POSIX/XPG semantics.
2426 */
2427 /* ARGSUSED */
2428 int
2429 sys___posix_lchown(p, v, retval)
2430 struct proc *p;
2431 void *v;
2432 register_t *retval;
2433 {
2434 struct sys_lchown_args /* {
2435 syscallarg(const char *) path;
2436 syscallarg(uid_t) uid;
2437 syscallarg(gid_t) gid;
2438 } */ *uap = v;
2439 int error;
2440 struct nameidata nd;
2441
2442 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2443 if ((error = namei(&nd)) != 0)
2444 return (error);
2445
2446 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2447
2448 vrele(nd.ni_vp);
2449 return (error);
2450 }
2451
2452 /*
2453 * Common routine to set ownership given a vnode.
2454 */
2455 static int
2456 change_owner(vp, uid, gid, p, posix_semantics)
2457 struct vnode *vp;
2458 uid_t uid;
2459 gid_t gid;
2460 struct proc *p;
2461 int posix_semantics;
2462 {
2463 struct vattr vattr;
2464 mode_t newmode;
2465 int error;
2466
2467 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2469 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2470 goto out;
2471
2472 #define CHANGED(x) ((x) != -1)
2473 newmode = vattr.va_mode;
2474 if (posix_semantics) {
2475 /*
2476 * POSIX/XPG semantics: if the caller is not the super-user,
2477 * clear set-user-id and set-group-id bits. Both POSIX and
2478 * the XPG consider the behaviour for calls by the super-user
2479 * implementation-defined; we leave the set-user-id and set-
2480 * group-id settings intact in that case.
2481 */
2482 if (suser(p->p_ucred, NULL) != 0)
2483 newmode &= ~(S_ISUID | S_ISGID);
2484 } else {
2485 /*
2486 * NetBSD semantics: when changing owner and/or group,
2487 * clear the respective bit(s).
2488 */
2489 if (CHANGED(uid))
2490 newmode &= ~S_ISUID;
2491 if (CHANGED(gid))
2492 newmode &= ~S_ISGID;
2493 }
2494 /* Update va_mode iff altered. */
2495 if (vattr.va_mode == newmode)
2496 newmode = VNOVAL;
2497
2498 VATTR_NULL(&vattr);
2499 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
2500 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
2501 vattr.va_mode = newmode;
2502 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2503 #undef CHANGED
2504
2505 out:
2506 VOP_UNLOCK(vp, 0);
2507 return (error);
2508 }
2509
2510 /*
2511 * Set the access and modification times given a path name; this
2512 * version follows links.
2513 */
2514 /* ARGSUSED */
2515 int
2516 sys_utimes(p, v, retval)
2517 struct proc *p;
2518 void *v;
2519 register_t *retval;
2520 {
2521 struct sys_utimes_args /* {
2522 syscallarg(const char *) path;
2523 syscallarg(const struct timeval *) tptr;
2524 } */ *uap = v;
2525 int error;
2526 struct nameidata nd;
2527
2528 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2529 if ((error = namei(&nd)) != 0)
2530 return (error);
2531
2532 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2533
2534 vrele(nd.ni_vp);
2535 return (error);
2536 }
2537
2538 /*
2539 * Set the access and modification times given a file descriptor.
2540 */
2541 /* ARGSUSED */
2542 int
2543 sys_futimes(p, v, retval)
2544 struct proc *p;
2545 void *v;
2546 register_t *retval;
2547 {
2548 struct sys_futimes_args /* {
2549 syscallarg(int) fd;
2550 syscallarg(const struct timeval *) tptr;
2551 } */ *uap = v;
2552 int error;
2553 struct file *fp;
2554
2555 /* getvnode() will use the descriptor for us */
2556 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2557 return (error);
2558
2559 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
2560 FILE_UNUSE(fp, p);
2561 return (error);
2562 }
2563
2564 /*
2565 * Set the access and modification times given a path name; this
2566 * version does not follow links.
2567 */
2568 /* ARGSUSED */
2569 int
2570 sys_lutimes(p, v, retval)
2571 struct proc *p;
2572 void *v;
2573 register_t *retval;
2574 {
2575 struct sys_lutimes_args /* {
2576 syscallarg(const char *) path;
2577 syscallarg(const struct timeval *) tptr;
2578 } */ *uap = v;
2579 int error;
2580 struct nameidata nd;
2581
2582 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2583 if ((error = namei(&nd)) != 0)
2584 return (error);
2585
2586 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2587
2588 vrele(nd.ni_vp);
2589 return (error);
2590 }
2591
2592 /*
2593 * Common routine to set access and modification times given a vnode.
2594 */
2595 static int
2596 change_utimes(vp, tptr, p)
2597 struct vnode *vp;
2598 const struct timeval *tptr;
2599 struct proc *p;
2600 {
2601 struct timeval tv[2];
2602 struct vattr vattr;
2603 int error;
2604
2605 VATTR_NULL(&vattr);
2606 if (tptr == NULL) {
2607 microtime(&tv[0]);
2608 tv[1] = tv[0];
2609 vattr.va_vaflags |= VA_UTIMES_NULL;
2610 } else {
2611 error = copyin(tptr, tv, sizeof(tv));
2612 if (error)
2613 return (error);
2614 }
2615 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2616 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2617 vattr.va_atime.tv_sec = tv[0].tv_sec;
2618 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2619 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2620 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2621 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2622 VOP_UNLOCK(vp, 0);
2623 return (error);
2624 }
2625
2626 /*
2627 * Truncate a file given its path name.
2628 */
2629 /* ARGSUSED */
2630 int
2631 sys_truncate(p, v, retval)
2632 struct proc *p;
2633 void *v;
2634 register_t *retval;
2635 {
2636 struct sys_truncate_args /* {
2637 syscallarg(const char *) path;
2638 syscallarg(int) pad;
2639 syscallarg(off_t) length;
2640 } */ *uap = v;
2641 struct vnode *vp;
2642 struct vattr vattr;
2643 int error;
2644 struct nameidata nd;
2645
2646 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2647 if ((error = namei(&nd)) != 0)
2648 return (error);
2649 vp = nd.ni_vp;
2650 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2651 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2652 if (vp->v_type == VDIR)
2653 error = EISDIR;
2654 else if ((error = vn_writechk(vp)) == 0 &&
2655 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2656 VATTR_NULL(&vattr);
2657 vattr.va_size = SCARG(uap, length);
2658 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2659 }
2660 vput(vp);
2661 return (error);
2662 }
2663
2664 /*
2665 * Truncate a file given a file descriptor.
2666 */
2667 /* ARGSUSED */
2668 int
2669 sys_ftruncate(p, v, retval)
2670 struct proc *p;
2671 void *v;
2672 register_t *retval;
2673 {
2674 struct sys_ftruncate_args /* {
2675 syscallarg(int) fd;
2676 syscallarg(int) pad;
2677 syscallarg(off_t) length;
2678 } */ *uap = v;
2679 struct vattr vattr;
2680 struct vnode *vp;
2681 struct file *fp;
2682 int error;
2683
2684 /* getvnode() will use the descriptor for us */
2685 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2686 return (error);
2687 if ((fp->f_flag & FWRITE) == 0) {
2688 error = EINVAL;
2689 goto out;
2690 }
2691 vp = (struct vnode *)fp->f_data;
2692 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2693 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2694 if (vp->v_type == VDIR)
2695 error = EISDIR;
2696 else if ((error = vn_writechk(vp)) == 0) {
2697 VATTR_NULL(&vattr);
2698 vattr.va_size = SCARG(uap, length);
2699 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2700 }
2701 VOP_UNLOCK(vp, 0);
2702 out:
2703 FILE_UNUSE(fp, p);
2704 return (error);
2705 }
2706
2707 /*
2708 * Sync an open file.
2709 */
2710 /* ARGSUSED */
2711 int
2712 sys_fsync(p, v, retval)
2713 struct proc *p;
2714 void *v;
2715 register_t *retval;
2716 {
2717 struct sys_fsync_args /* {
2718 syscallarg(int) fd;
2719 } */ *uap = v;
2720 struct vnode *vp;
2721 struct file *fp;
2722 int error;
2723
2724 /* getvnode() will use the descriptor for us */
2725 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2726 return (error);
2727 vp = (struct vnode *)fp->f_data;
2728 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2729 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p);
2730 if (error == 0 && bioops.io_fsync != NULL &&
2731 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
2732 (*bioops.io_fsync)(vp);
2733 VOP_UNLOCK(vp, 0);
2734 FILE_UNUSE(fp, p);
2735 return (error);
2736 }
2737
2738 /*
2739 * Sync the data of an open file.
2740 */
2741 /* ARGSUSED */
2742 int
2743 sys_fdatasync(p, v, retval)
2744 struct proc *p;
2745 void *v;
2746 register_t *retval;
2747 {
2748 struct sys_fdatasync_args /* {
2749 syscallarg(int) fd;
2750 } */ *uap = v;
2751 struct vnode *vp;
2752 struct file *fp;
2753 int error;
2754
2755 /* getvnode() will use the descriptor for us */
2756 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2757 return (error);
2758 vp = (struct vnode *)fp->f_data;
2759 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2760 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);
2761 VOP_UNLOCK(vp, 0);
2762 FILE_UNUSE(fp, p);
2763 return (error);
2764 }
2765
2766 /*
2767 * Rename files, (standard) BSD semantics frontend.
2768 */
2769 /* ARGSUSED */
2770 int
2771 sys_rename(p, v, retval)
2772 struct proc *p;
2773 void *v;
2774 register_t *retval;
2775 {
2776 struct sys_rename_args /* {
2777 syscallarg(const char *) from;
2778 syscallarg(const char *) to;
2779 } */ *uap = v;
2780
2781 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2782 }
2783
2784 /*
2785 * Rename files, POSIX semantics frontend.
2786 */
2787 /* ARGSUSED */
2788 int
2789 sys___posix_rename(p, v, retval)
2790 struct proc *p;
2791 void *v;
2792 register_t *retval;
2793 {
2794 struct sys___posix_rename_args /* {
2795 syscallarg(const char *) from;
2796 syscallarg(const char *) to;
2797 } */ *uap = v;
2798
2799 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2800 }
2801
2802 /*
2803 * Rename files. Source and destination must either both be directories,
2804 * or both not be directories. If target is a directory, it must be empty.
2805 * If `from' and `to' refer to the same object, the value of the `retain'
2806 * argument is used to determine whether `from' will be
2807 *
2808 * (retain == 0) deleted unless `from' and `to' refer to the same
2809 * object in the file system's name space (BSD).
2810 * (retain == 1) always retained (POSIX).
2811 */
2812 static int
2813 rename_files(from, to, p, retain)
2814 const char *from, *to;
2815 struct proc *p;
2816 int retain;
2817 {
2818 struct vnode *tvp, *fvp, *tdvp;
2819 struct nameidata fromnd, tond;
2820 int error;
2821
2822 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2823 from, p);
2824 if ((error = namei(&fromnd)) != 0)
2825 return (error);
2826 fvp = fromnd.ni_vp;
2827 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2828 UIO_USERSPACE, to, p);
2829 if ((error = namei(&tond)) != 0) {
2830 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2831 vrele(fromnd.ni_dvp);
2832 vrele(fvp);
2833 goto out1;
2834 }
2835 tdvp = tond.ni_dvp;
2836 tvp = tond.ni_vp;
2837
2838 if (tvp != NULL) {
2839 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2840 error = ENOTDIR;
2841 goto out;
2842 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2843 error = EISDIR;
2844 goto out;
2845 }
2846 }
2847
2848 if (fvp == tdvp)
2849 error = EINVAL;
2850
2851 /*
2852 * Source and destination refer to the same object.
2853 */
2854 if (fvp == tvp) {
2855 if (retain)
2856 error = -1;
2857 else if (fromnd.ni_dvp == tdvp &&
2858 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2859 !memcmp(fromnd.ni_cnd.cn_nameptr,
2860 tond.ni_cnd.cn_nameptr,
2861 fromnd.ni_cnd.cn_namelen))
2862 error = -1;
2863 }
2864
2865 out:
2866 if (!error) {
2867 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2868 if (fromnd.ni_dvp != tdvp)
2869 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2870 if (tvp) {
2871 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2872 }
2873 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2874 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2875 } else {
2876 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2877 if (tdvp == tvp)
2878 vrele(tdvp);
2879 else
2880 vput(tdvp);
2881 if (tvp)
2882 vput(tvp);
2883 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2884 vrele(fromnd.ni_dvp);
2885 vrele(fvp);
2886 }
2887 vrele(tond.ni_startdir);
2888 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
2889 out1:
2890 if (fromnd.ni_startdir)
2891 vrele(fromnd.ni_startdir);
2892 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
2893 return (error == -1 ? 0 : error);
2894 }
2895
2896 /*
2897 * Make a directory file.
2898 */
2899 /* ARGSUSED */
2900 int
2901 sys_mkdir(p, v, retval)
2902 struct proc *p;
2903 void *v;
2904 register_t *retval;
2905 {
2906 struct sys_mkdir_args /* {
2907 syscallarg(const char *) path;
2908 syscallarg(int) mode;
2909 } */ *uap = v;
2910 struct vnode *vp;
2911 struct vattr vattr;
2912 int error;
2913 struct nameidata nd;
2914
2915 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2916 if ((error = namei(&nd)) != 0)
2917 return (error);
2918 vp = nd.ni_vp;
2919 if (vp != NULL) {
2920 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2921 if (nd.ni_dvp == vp)
2922 vrele(nd.ni_dvp);
2923 else
2924 vput(nd.ni_dvp);
2925 vrele(vp);
2926 return (EEXIST);
2927 }
2928 VATTR_NULL(&vattr);
2929 vattr.va_type = VDIR;
2930 vattr.va_mode =
2931 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
2932 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2933 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2934 if (!error)
2935 vput(nd.ni_vp);
2936 return (error);
2937 }
2938
2939 /*
2940 * Remove a directory file.
2941 */
2942 /* ARGSUSED */
2943 int
2944 sys_rmdir(p, v, retval)
2945 struct proc *p;
2946 void *v;
2947 register_t *retval;
2948 {
2949 struct sys_rmdir_args /* {
2950 syscallarg(const char *) path;
2951 } */ *uap = v;
2952 struct vnode *vp;
2953 int error;
2954 struct nameidata nd;
2955
2956 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2957 SCARG(uap, path), p);
2958 if ((error = namei(&nd)) != 0)
2959 return (error);
2960 vp = nd.ni_vp;
2961 if (vp->v_type != VDIR) {
2962 error = ENOTDIR;
2963 goto out;
2964 }
2965 /*
2966 * No rmdir "." please.
2967 */
2968 if (nd.ni_dvp == vp) {
2969 error = EINVAL;
2970 goto out;
2971 }
2972 /*
2973 * The root of a mounted filesystem cannot be deleted.
2974 */
2975 if (vp->v_flag & VROOT)
2976 error = EBUSY;
2977 out:
2978 if (!error) {
2979 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2980 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2981 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2982 } else {
2983 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2984 if (nd.ni_dvp == vp)
2985 vrele(nd.ni_dvp);
2986 else
2987 vput(nd.ni_dvp);
2988 vput(vp);
2989 }
2990 return (error);
2991 }
2992
2993 /*
2994 * Read a block of directory entries in a file system independent format.
2995 */
2996 int
2997 sys_getdents(p, v, retval)
2998 struct proc *p;
2999 void *v;
3000 register_t *retval;
3001 {
3002 struct sys_getdents_args /* {
3003 syscallarg(int) fd;
3004 syscallarg(char *) buf;
3005 syscallarg(size_t) count;
3006 } */ *uap = v;
3007 struct file *fp;
3008 int error, done;
3009
3010 /* getvnode() will use the descriptor for us */
3011 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3012 return (error);
3013 if ((fp->f_flag & FREAD) == 0) {
3014 error = EBADF;
3015 goto out;
3016 }
3017 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3018 SCARG(uap, count), &done, p, 0, 0);
3019 *retval = done;
3020 out:
3021 FILE_UNUSE(fp, p);
3022 return (error);
3023 }
3024
3025 /*
3026 * Set the mode mask for creation of filesystem nodes.
3027 */
3028 int
3029 sys_umask(p, v, retval)
3030 struct proc *p;
3031 void *v;
3032 register_t *retval;
3033 {
3034 struct sys_umask_args /* {
3035 syscallarg(mode_t) newmask;
3036 } */ *uap = v;
3037 struct cwdinfo *cwdi;
3038
3039 cwdi = p->p_cwdi;
3040 *retval = cwdi->cwdi_cmask;
3041 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3042 return (0);
3043 }
3044
3045 /*
3046 * Void all references to file by ripping underlying filesystem
3047 * away from vnode.
3048 */
3049 /* ARGSUSED */
3050 int
3051 sys_revoke(p, v, retval)
3052 struct proc *p;
3053 void *v;
3054 register_t *retval;
3055 {
3056 struct sys_revoke_args /* {
3057 syscallarg(const char *) path;
3058 } */ *uap = v;
3059 struct vnode *vp;
3060 struct vattr vattr;
3061 int error;
3062 struct nameidata nd;
3063
3064 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3065 if ((error = namei(&nd)) != 0)
3066 return (error);
3067 vp = nd.ni_vp;
3068 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
3069 goto out;
3070 if (p->p_ucred->cr_uid != vattr.va_uid &&
3071 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
3072 goto out;
3073 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
3074 VOP_REVOKE(vp, REVOKEALL);
3075 out:
3076 vrele(vp);
3077 return (error);
3078 }
3079
3080 /*
3081 * Convert a user file descriptor to a kernel file entry.
3082 */
3083 int
3084 getvnode(fdp, fd, fpp)
3085 struct filedesc *fdp;
3086 int fd;
3087 struct file **fpp;
3088 {
3089 struct vnode *vp;
3090 struct file *fp;
3091
3092 if ((fp = fd_getfile(fdp, fd)) == NULL)
3093 return (EBADF);
3094
3095 FILE_USE(fp);
3096
3097 if (fp->f_type != DTYPE_VNODE) {
3098 FILE_UNUSE(fp, NULL);
3099 return (EINVAL);
3100 }
3101
3102 vp = (struct vnode *)fp->f_data;
3103 if (vp->v_type == VBAD) {
3104 FILE_UNUSE(fp, NULL);
3105 return (EBADF);
3106 }
3107
3108 *fpp = fp;
3109 return (0);
3110 }
3111