vfs_syscalls.c revision 1.168.2.3 1 /* $NetBSD: vfs_syscalls.c,v 1.168.2.3 2001/10/01 12:47:01 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 = crget();
1905 struct vnode *vp;
1906 int error, flags;
1907 struct nameidata nd;
1908
1909 (void)memcpy(cred, p->p_ucred, sizeof(*cred));
1910 cred->cr_ref = 1;
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 /* Override default credentials */
1916 nd.ni_cnd.cn_cred = cred;
1917 if ((error = namei(&nd)) != 0)
1918 goto out;
1919 vp = nd.ni_vp;
1920
1921 /* Flags == 0 means only check for existence. */
1922 if (SCARG(uap, flags)) {
1923 flags = 0;
1924 if (SCARG(uap, flags) & R_OK)
1925 flags |= VREAD;
1926 if (SCARG(uap, flags) & W_OK)
1927 flags |= VWRITE;
1928 if (SCARG(uap, flags) & X_OK)
1929 flags |= VEXEC;
1930
1931 error = VOP_ACCESS(vp, flags, cred, p);
1932 if (!error && (flags & VWRITE))
1933 error = vn_writechk(vp);
1934 }
1935 vput(vp);
1936 out:
1937 crfree(cred);
1938 return (error);
1939 }
1940
1941 /*
1942 * Get file status; this version follows links.
1943 */
1944 /* ARGSUSED */
1945 int
1946 sys___stat13(p, v, retval)
1947 struct proc *p;
1948 void *v;
1949 register_t *retval;
1950 {
1951 struct sys___stat13_args /* {
1952 syscallarg(const char *) path;
1953 syscallarg(struct stat *) ub;
1954 } */ *uap = v;
1955 struct stat sb;
1956 int error;
1957 struct nameidata nd;
1958
1959 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1960 SCARG(uap, path), p);
1961 if ((error = namei(&nd)) != 0)
1962 return (error);
1963 error = vn_stat(nd.ni_vp, &sb, p);
1964 vput(nd.ni_vp);
1965 if (error)
1966 return (error);
1967 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1968 return (error);
1969 }
1970
1971 /*
1972 * Get file status; this version does not follow links.
1973 */
1974 /* ARGSUSED */
1975 int
1976 sys___lstat13(p, v, retval)
1977 struct proc *p;
1978 void *v;
1979 register_t *retval;
1980 {
1981 struct sys___lstat13_args /* {
1982 syscallarg(const char *) path;
1983 syscallarg(struct stat *) ub;
1984 } */ *uap = v;
1985 struct stat sb;
1986 int error;
1987 struct nameidata nd;
1988
1989 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1990 SCARG(uap, path), p);
1991 if ((error = namei(&nd)) != 0)
1992 return (error);
1993 error = vn_stat(nd.ni_vp, &sb, p);
1994 vput(nd.ni_vp);
1995 if (error)
1996 return (error);
1997 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1998 return (error);
1999 }
2000
2001 /*
2002 * Get configurable pathname variables.
2003 */
2004 /* ARGSUSED */
2005 int
2006 sys_pathconf(p, v, retval)
2007 struct proc *p;
2008 void *v;
2009 register_t *retval;
2010 {
2011 struct sys_pathconf_args /* {
2012 syscallarg(const char *) path;
2013 syscallarg(int) name;
2014 } */ *uap = v;
2015 int error;
2016 struct nameidata nd;
2017
2018 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2019 SCARG(uap, path), p);
2020 if ((error = namei(&nd)) != 0)
2021 return (error);
2022 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2023 vput(nd.ni_vp);
2024 return (error);
2025 }
2026
2027 /*
2028 * Return target name of a symbolic link.
2029 */
2030 /* ARGSUSED */
2031 int
2032 sys_readlink(p, v, retval)
2033 struct proc *p;
2034 void *v;
2035 register_t *retval;
2036 {
2037 struct sys_readlink_args /* {
2038 syscallarg(const char *) path;
2039 syscallarg(char *) buf;
2040 syscallarg(size_t) count;
2041 } */ *uap = v;
2042 struct vnode *vp;
2043 struct iovec aiov;
2044 struct uio auio;
2045 int error;
2046 struct nameidata nd;
2047
2048 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
2049 SCARG(uap, path), p);
2050 if ((error = namei(&nd)) != 0)
2051 return (error);
2052 vp = nd.ni_vp;
2053 if (vp->v_type != VLNK)
2054 error = EINVAL;
2055 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2056 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
2057 aiov.iov_base = SCARG(uap, buf);
2058 aiov.iov_len = SCARG(uap, count);
2059 auio.uio_iov = &aiov;
2060 auio.uio_iovcnt = 1;
2061 auio.uio_offset = 0;
2062 auio.uio_rw = UIO_READ;
2063 auio.uio_segflg = UIO_USERSPACE;
2064 auio.uio_procp = p;
2065 auio.uio_resid = SCARG(uap, count);
2066 error = VOP_READLINK(vp, &auio, p->p_ucred);
2067 }
2068 vput(vp);
2069 *retval = SCARG(uap, count) - auio.uio_resid;
2070 return (error);
2071 }
2072
2073 /*
2074 * Change flags of a file given a path name.
2075 */
2076 /* ARGSUSED */
2077 int
2078 sys_chflags(p, v, retval)
2079 struct proc *p;
2080 void *v;
2081 register_t *retval;
2082 {
2083 struct sys_chflags_args /* {
2084 syscallarg(const char *) path;
2085 syscallarg(u_long) flags;
2086 } */ *uap = v;
2087 struct vnode *vp;
2088 int error;
2089 struct nameidata nd;
2090
2091 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2092 if ((error = namei(&nd)) != 0)
2093 return (error);
2094 vp = nd.ni_vp;
2095 error = change_flags(vp, SCARG(uap, flags), p);
2096 vput(vp);
2097 return (error);
2098 }
2099
2100 /*
2101 * Change flags of a file given a file descriptor.
2102 */
2103 /* ARGSUSED */
2104 int
2105 sys_fchflags(p, v, retval)
2106 struct proc *p;
2107 void *v;
2108 register_t *retval;
2109 {
2110 struct sys_fchflags_args /* {
2111 syscallarg(int) fd;
2112 syscallarg(u_long) flags;
2113 } */ *uap = v;
2114 struct vnode *vp;
2115 struct file *fp;
2116 int error;
2117
2118 /* getvnode() will use the descriptor for us */
2119 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2120 return (error);
2121 vp = (struct vnode *)fp->f_data;
2122 error = change_flags(vp, SCARG(uap, flags), p);
2123 VOP_UNLOCK(vp, 0);
2124 FILE_UNUSE(fp, p);
2125 return (error);
2126 }
2127
2128 /*
2129 * Change flags of a file given a path name; this version does
2130 * not follow links.
2131 */
2132 int
2133 sys_lchflags(p, v, retval)
2134 struct proc *p;
2135 void *v;
2136 register_t *retval;
2137 {
2138 struct sys_lchflags_args /* {
2139 syscallarg(const char *) path;
2140 syscallarg(u_long) flags;
2141 } */ *uap = v;
2142 struct vnode *vp;
2143 int error;
2144 struct nameidata nd;
2145
2146 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2147 if ((error = namei(&nd)) != 0)
2148 return (error);
2149 vp = nd.ni_vp;
2150 error = change_flags(vp, SCARG(uap, flags), p);
2151 vput(vp);
2152 return (error);
2153 }
2154
2155 /*
2156 * Common routine to change flags of a file.
2157 */
2158 int
2159 change_flags(vp, flags, p)
2160 struct vnode *vp;
2161 u_long flags;
2162 struct proc *p;
2163 {
2164 struct vattr vattr;
2165 int error;
2166
2167 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2168 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2169 /*
2170 * Non-superusers cannot change the flags on devices, even if they
2171 * own them.
2172 */
2173 if (suser(p->p_ucred, &p->p_acflag) != 0) {
2174 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2175 goto out;
2176 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2177 error = EINVAL;
2178 goto out;
2179 }
2180 }
2181 VATTR_NULL(&vattr);
2182 vattr.va_flags = flags;
2183 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2184 out:
2185 return (error);
2186 }
2187
2188 /*
2189 * Change mode of a file given path name; this version follows links.
2190 */
2191 /* ARGSUSED */
2192 int
2193 sys_chmod(p, v, retval)
2194 struct proc *p;
2195 void *v;
2196 register_t *retval;
2197 {
2198 struct sys_chmod_args /* {
2199 syscallarg(const char *) path;
2200 syscallarg(int) mode;
2201 } */ *uap = v;
2202 int error;
2203 struct nameidata nd;
2204
2205 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2206 if ((error = namei(&nd)) != 0)
2207 return (error);
2208
2209 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2210
2211 vrele(nd.ni_vp);
2212 return (error);
2213 }
2214
2215 /*
2216 * Change mode of a file given a file descriptor.
2217 */
2218 /* ARGSUSED */
2219 int
2220 sys_fchmod(p, v, retval)
2221 struct proc *p;
2222 void *v;
2223 register_t *retval;
2224 {
2225 struct sys_fchmod_args /* {
2226 syscallarg(int) fd;
2227 syscallarg(int) mode;
2228 } */ *uap = v;
2229 struct file *fp;
2230 int error;
2231
2232 /* getvnode() will use the descriptor for us */
2233 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2234 return (error);
2235
2236 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
2237 FILE_UNUSE(fp, p);
2238 return (error);
2239 }
2240
2241 /*
2242 * Change mode of a file given path name; this version does not follow links.
2243 */
2244 /* ARGSUSED */
2245 int
2246 sys_lchmod(p, v, retval)
2247 struct proc *p;
2248 void *v;
2249 register_t *retval;
2250 {
2251 struct sys_lchmod_args /* {
2252 syscallarg(const char *) path;
2253 syscallarg(int) mode;
2254 } */ *uap = v;
2255 int error;
2256 struct nameidata nd;
2257
2258 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2259 if ((error = namei(&nd)) != 0)
2260 return (error);
2261
2262 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2263
2264 vrele(nd.ni_vp);
2265 return (error);
2266 }
2267
2268 /*
2269 * Common routine to set mode given a vnode.
2270 */
2271 static int
2272 change_mode(vp, mode, p)
2273 struct vnode *vp;
2274 int mode;
2275 struct proc *p;
2276 {
2277 struct vattr vattr;
2278 int error;
2279
2280 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2281 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2282 VATTR_NULL(&vattr);
2283 vattr.va_mode = mode & ALLPERMS;
2284 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2285 VOP_UNLOCK(vp, 0);
2286 return (error);
2287 }
2288
2289 /*
2290 * Set ownership given a path name; this version follows links.
2291 */
2292 /* ARGSUSED */
2293 int
2294 sys_chown(p, v, retval)
2295 struct proc *p;
2296 void *v;
2297 register_t *retval;
2298 {
2299 struct sys_chown_args /* {
2300 syscallarg(const char *) path;
2301 syscallarg(uid_t) uid;
2302 syscallarg(gid_t) gid;
2303 } */ *uap = v;
2304 int error;
2305 struct nameidata nd;
2306
2307 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2308 if ((error = namei(&nd)) != 0)
2309 return (error);
2310
2311 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2312
2313 vrele(nd.ni_vp);
2314 return (error);
2315 }
2316
2317 /*
2318 * Set ownership given a path name; this version follows links.
2319 * Provides POSIX semantics.
2320 */
2321 /* ARGSUSED */
2322 int
2323 sys___posix_chown(p, v, retval)
2324 struct proc *p;
2325 void *v;
2326 register_t *retval;
2327 {
2328 struct sys_chown_args /* {
2329 syscallarg(const char *) path;
2330 syscallarg(uid_t) uid;
2331 syscallarg(gid_t) gid;
2332 } */ *uap = v;
2333 int error;
2334 struct nameidata nd;
2335
2336 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2337 if ((error = namei(&nd)) != 0)
2338 return (error);
2339
2340 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2341
2342 vrele(nd.ni_vp);
2343 return (error);
2344 }
2345
2346 /*
2347 * Set ownership given a file descriptor.
2348 */
2349 /* ARGSUSED */
2350 int
2351 sys_fchown(p, v, retval)
2352 struct proc *p;
2353 void *v;
2354 register_t *retval;
2355 {
2356 struct sys_fchown_args /* {
2357 syscallarg(int) fd;
2358 syscallarg(uid_t) uid;
2359 syscallarg(gid_t) gid;
2360 } */ *uap = v;
2361 int error;
2362 struct file *fp;
2363
2364 /* getvnode() will use the descriptor for us */
2365 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2366 return (error);
2367
2368 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2369 SCARG(uap, gid), p, 0);
2370 FILE_UNUSE(fp, p);
2371 return (error);
2372 }
2373
2374 /*
2375 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2376 */
2377 /* ARGSUSED */
2378 int
2379 sys___posix_fchown(p, v, retval)
2380 struct proc *p;
2381 void *v;
2382 register_t *retval;
2383 {
2384 struct sys_fchown_args /* {
2385 syscallarg(int) fd;
2386 syscallarg(uid_t) uid;
2387 syscallarg(gid_t) gid;
2388 } */ *uap = v;
2389 int error;
2390 struct file *fp;
2391
2392 /* getvnode() will use the descriptor for us */
2393 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2394 return (error);
2395
2396 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2397 SCARG(uap, gid), p, 1);
2398 FILE_UNUSE(fp, p);
2399 return (error);
2400 }
2401
2402 /*
2403 * Set ownership given a path name; this version does not follow links.
2404 */
2405 /* ARGSUSED */
2406 int
2407 sys_lchown(p, v, retval)
2408 struct proc *p;
2409 void *v;
2410 register_t *retval;
2411 {
2412 struct sys_lchown_args /* {
2413 syscallarg(const char *) path;
2414 syscallarg(uid_t) uid;
2415 syscallarg(gid_t) gid;
2416 } */ *uap = v;
2417 int error;
2418 struct nameidata nd;
2419
2420 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2421 if ((error = namei(&nd)) != 0)
2422 return (error);
2423
2424 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2425
2426 vrele(nd.ni_vp);
2427 return (error);
2428 }
2429
2430 /*
2431 * Set ownership given a path name; this version does not follow links.
2432 * Provides POSIX/XPG semantics.
2433 */
2434 /* ARGSUSED */
2435 int
2436 sys___posix_lchown(p, v, retval)
2437 struct proc *p;
2438 void *v;
2439 register_t *retval;
2440 {
2441 struct sys_lchown_args /* {
2442 syscallarg(const char *) path;
2443 syscallarg(uid_t) uid;
2444 syscallarg(gid_t) gid;
2445 } */ *uap = v;
2446 int error;
2447 struct nameidata nd;
2448
2449 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2450 if ((error = namei(&nd)) != 0)
2451 return (error);
2452
2453 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2454
2455 vrele(nd.ni_vp);
2456 return (error);
2457 }
2458
2459 /*
2460 * Common routine to set ownership given a vnode.
2461 */
2462 static int
2463 change_owner(vp, uid, gid, p, posix_semantics)
2464 struct vnode *vp;
2465 uid_t uid;
2466 gid_t gid;
2467 struct proc *p;
2468 int posix_semantics;
2469 {
2470 struct vattr vattr;
2471 mode_t newmode;
2472 int error;
2473
2474 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2475 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2476 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2477 goto out;
2478
2479 #define CHANGED(x) ((x) != -1)
2480 newmode = vattr.va_mode;
2481 if (posix_semantics) {
2482 /*
2483 * POSIX/XPG semantics: if the caller is not the super-user,
2484 * clear set-user-id and set-group-id bits. Both POSIX and
2485 * the XPG consider the behaviour for calls by the super-user
2486 * implementation-defined; we leave the set-user-id and set-
2487 * group-id settings intact in that case.
2488 */
2489 if (suser(p->p_ucred, NULL) != 0)
2490 newmode &= ~(S_ISUID | S_ISGID);
2491 } else {
2492 /*
2493 * NetBSD semantics: when changing owner and/or group,
2494 * clear the respective bit(s).
2495 */
2496 if (CHANGED(uid))
2497 newmode &= ~S_ISUID;
2498 if (CHANGED(gid))
2499 newmode &= ~S_ISGID;
2500 }
2501 /* Update va_mode iff altered. */
2502 if (vattr.va_mode == newmode)
2503 newmode = VNOVAL;
2504
2505 VATTR_NULL(&vattr);
2506 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
2507 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
2508 vattr.va_mode = newmode;
2509 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2510 #undef CHANGED
2511
2512 out:
2513 VOP_UNLOCK(vp, 0);
2514 return (error);
2515 }
2516
2517 /*
2518 * Set the access and modification times given a path name; this
2519 * version follows links.
2520 */
2521 /* ARGSUSED */
2522 int
2523 sys_utimes(p, v, retval)
2524 struct proc *p;
2525 void *v;
2526 register_t *retval;
2527 {
2528 struct sys_utimes_args /* {
2529 syscallarg(const char *) path;
2530 syscallarg(const struct timeval *) tptr;
2531 } */ *uap = v;
2532 int error;
2533 struct nameidata nd;
2534
2535 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2536 if ((error = namei(&nd)) != 0)
2537 return (error);
2538
2539 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2540
2541 vrele(nd.ni_vp);
2542 return (error);
2543 }
2544
2545 /*
2546 * Set the access and modification times given a file descriptor.
2547 */
2548 /* ARGSUSED */
2549 int
2550 sys_futimes(p, v, retval)
2551 struct proc *p;
2552 void *v;
2553 register_t *retval;
2554 {
2555 struct sys_futimes_args /* {
2556 syscallarg(int) fd;
2557 syscallarg(const struct timeval *) tptr;
2558 } */ *uap = v;
2559 int error;
2560 struct file *fp;
2561
2562 /* getvnode() will use the descriptor for us */
2563 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2564 return (error);
2565
2566 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
2567 FILE_UNUSE(fp, p);
2568 return (error);
2569 }
2570
2571 /*
2572 * Set the access and modification times given a path name; this
2573 * version does not follow links.
2574 */
2575 /* ARGSUSED */
2576 int
2577 sys_lutimes(p, v, retval)
2578 struct proc *p;
2579 void *v;
2580 register_t *retval;
2581 {
2582 struct sys_lutimes_args /* {
2583 syscallarg(const char *) path;
2584 syscallarg(const struct timeval *) tptr;
2585 } */ *uap = v;
2586 int error;
2587 struct nameidata nd;
2588
2589 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2590 if ((error = namei(&nd)) != 0)
2591 return (error);
2592
2593 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2594
2595 vrele(nd.ni_vp);
2596 return (error);
2597 }
2598
2599 /*
2600 * Common routine to set access and modification times given a vnode.
2601 */
2602 static int
2603 change_utimes(vp, tptr, p)
2604 struct vnode *vp;
2605 const struct timeval *tptr;
2606 struct proc *p;
2607 {
2608 struct timeval tv[2];
2609 struct vattr vattr;
2610 int error;
2611
2612 VATTR_NULL(&vattr);
2613 if (tptr == NULL) {
2614 microtime(&tv[0]);
2615 tv[1] = tv[0];
2616 vattr.va_vaflags |= VA_UTIMES_NULL;
2617 } else {
2618 error = copyin(tptr, tv, sizeof(tv));
2619 if (error)
2620 return (error);
2621 }
2622 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2623 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2624 vattr.va_atime.tv_sec = tv[0].tv_sec;
2625 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2626 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2627 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2628 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2629 VOP_UNLOCK(vp, 0);
2630 return (error);
2631 }
2632
2633 /*
2634 * Truncate a file given its path name.
2635 */
2636 /* ARGSUSED */
2637 int
2638 sys_truncate(p, v, retval)
2639 struct proc *p;
2640 void *v;
2641 register_t *retval;
2642 {
2643 struct sys_truncate_args /* {
2644 syscallarg(const char *) path;
2645 syscallarg(int) pad;
2646 syscallarg(off_t) length;
2647 } */ *uap = v;
2648 struct vnode *vp;
2649 struct vattr vattr;
2650 int error;
2651 struct nameidata nd;
2652
2653 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2654 if ((error = namei(&nd)) != 0)
2655 return (error);
2656 vp = nd.ni_vp;
2657 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2658 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2659 if (vp->v_type == VDIR)
2660 error = EISDIR;
2661 else if ((error = vn_writechk(vp)) == 0 &&
2662 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2663 VATTR_NULL(&vattr);
2664 vattr.va_size = SCARG(uap, length);
2665 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2666 }
2667 vput(vp);
2668 return (error);
2669 }
2670
2671 /*
2672 * Truncate a file given a file descriptor.
2673 */
2674 /* ARGSUSED */
2675 int
2676 sys_ftruncate(p, v, retval)
2677 struct proc *p;
2678 void *v;
2679 register_t *retval;
2680 {
2681 struct sys_ftruncate_args /* {
2682 syscallarg(int) fd;
2683 syscallarg(int) pad;
2684 syscallarg(off_t) length;
2685 } */ *uap = v;
2686 struct vattr vattr;
2687 struct vnode *vp;
2688 struct file *fp;
2689 int error;
2690
2691 /* getvnode() will use the descriptor for us */
2692 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2693 return (error);
2694 if ((fp->f_flag & FWRITE) == 0) {
2695 error = EINVAL;
2696 goto out;
2697 }
2698 vp = (struct vnode *)fp->f_data;
2699 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2700 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2701 if (vp->v_type == VDIR)
2702 error = EISDIR;
2703 else if ((error = vn_writechk(vp)) == 0) {
2704 VATTR_NULL(&vattr);
2705 vattr.va_size = SCARG(uap, length);
2706 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2707 }
2708 VOP_UNLOCK(vp, 0);
2709 out:
2710 FILE_UNUSE(fp, p);
2711 return (error);
2712 }
2713
2714 /*
2715 * Sync an open file.
2716 */
2717 /* ARGSUSED */
2718 int
2719 sys_fsync(p, v, retval)
2720 struct proc *p;
2721 void *v;
2722 register_t *retval;
2723 {
2724 struct sys_fsync_args /* {
2725 syscallarg(int) fd;
2726 } */ *uap = v;
2727 struct vnode *vp;
2728 struct file *fp;
2729 int error;
2730
2731 /* getvnode() will use the descriptor for us */
2732 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2733 return (error);
2734 vp = (struct vnode *)fp->f_data;
2735 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2736 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p);
2737 if (error == 0 && bioops.io_fsync != NULL &&
2738 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
2739 (*bioops.io_fsync)(vp);
2740 VOP_UNLOCK(vp, 0);
2741 FILE_UNUSE(fp, p);
2742 return (error);
2743 }
2744
2745 /*
2746 * Sync the data of an open file.
2747 */
2748 /* ARGSUSED */
2749 int
2750 sys_fdatasync(p, v, retval)
2751 struct proc *p;
2752 void *v;
2753 register_t *retval;
2754 {
2755 struct sys_fdatasync_args /* {
2756 syscallarg(int) fd;
2757 } */ *uap = v;
2758 struct vnode *vp;
2759 struct file *fp;
2760 int error;
2761
2762 /* getvnode() will use the descriptor for us */
2763 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2764 return (error);
2765 vp = (struct vnode *)fp->f_data;
2766 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2767 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);
2768 VOP_UNLOCK(vp, 0);
2769 FILE_UNUSE(fp, p);
2770 return (error);
2771 }
2772
2773 /*
2774 * Rename files, (standard) BSD semantics frontend.
2775 */
2776 /* ARGSUSED */
2777 int
2778 sys_rename(p, v, retval)
2779 struct proc *p;
2780 void *v;
2781 register_t *retval;
2782 {
2783 struct sys_rename_args /* {
2784 syscallarg(const char *) from;
2785 syscallarg(const char *) to;
2786 } */ *uap = v;
2787
2788 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2789 }
2790
2791 /*
2792 * Rename files, POSIX semantics frontend.
2793 */
2794 /* ARGSUSED */
2795 int
2796 sys___posix_rename(p, v, retval)
2797 struct proc *p;
2798 void *v;
2799 register_t *retval;
2800 {
2801 struct sys___posix_rename_args /* {
2802 syscallarg(const char *) from;
2803 syscallarg(const char *) to;
2804 } */ *uap = v;
2805
2806 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2807 }
2808
2809 /*
2810 * Rename files. Source and destination must either both be directories,
2811 * or both not be directories. If target is a directory, it must be empty.
2812 * If `from' and `to' refer to the same object, the value of the `retain'
2813 * argument is used to determine whether `from' will be
2814 *
2815 * (retain == 0) deleted unless `from' and `to' refer to the same
2816 * object in the file system's name space (BSD).
2817 * (retain == 1) always retained (POSIX).
2818 */
2819 static int
2820 rename_files(from, to, p, retain)
2821 const char *from, *to;
2822 struct proc *p;
2823 int retain;
2824 {
2825 struct vnode *tvp, *fvp, *tdvp;
2826 struct nameidata fromnd, tond;
2827 int error;
2828
2829 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2830 from, p);
2831 if ((error = namei(&fromnd)) != 0)
2832 return (error);
2833 fvp = fromnd.ni_vp;
2834 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2835 UIO_USERSPACE, to, p);
2836 if ((error = namei(&tond)) != 0) {
2837 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2838 vrele(fromnd.ni_dvp);
2839 vrele(fvp);
2840 goto out1;
2841 }
2842 tdvp = tond.ni_dvp;
2843 tvp = tond.ni_vp;
2844
2845 if (tvp != NULL) {
2846 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2847 error = ENOTDIR;
2848 goto out;
2849 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2850 error = EISDIR;
2851 goto out;
2852 }
2853 }
2854
2855 if (fvp == tdvp)
2856 error = EINVAL;
2857
2858 /*
2859 * Source and destination refer to the same object.
2860 */
2861 if (fvp == tvp) {
2862 if (retain)
2863 error = -1;
2864 else if (fromnd.ni_dvp == tdvp &&
2865 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2866 !memcmp(fromnd.ni_cnd.cn_nameptr,
2867 tond.ni_cnd.cn_nameptr,
2868 fromnd.ni_cnd.cn_namelen))
2869 error = -1;
2870 }
2871
2872 out:
2873 if (!error) {
2874 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2875 if (fromnd.ni_dvp != tdvp)
2876 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2877 if (tvp) {
2878 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2879 }
2880 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2881 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2882 } else {
2883 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2884 if (tdvp == tvp)
2885 vrele(tdvp);
2886 else
2887 vput(tdvp);
2888 if (tvp)
2889 vput(tvp);
2890 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2891 vrele(fromnd.ni_dvp);
2892 vrele(fvp);
2893 }
2894 vrele(tond.ni_startdir);
2895 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
2896 out1:
2897 if (fromnd.ni_startdir)
2898 vrele(fromnd.ni_startdir);
2899 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
2900 return (error == -1 ? 0 : error);
2901 }
2902
2903 /*
2904 * Make a directory file.
2905 */
2906 /* ARGSUSED */
2907 int
2908 sys_mkdir(p, v, retval)
2909 struct proc *p;
2910 void *v;
2911 register_t *retval;
2912 {
2913 struct sys_mkdir_args /* {
2914 syscallarg(const char *) path;
2915 syscallarg(int) mode;
2916 } */ *uap = v;
2917 struct vnode *vp;
2918 struct vattr vattr;
2919 int error;
2920 struct nameidata nd;
2921
2922 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2923 if ((error = namei(&nd)) != 0)
2924 return (error);
2925 vp = nd.ni_vp;
2926 if (vp != NULL) {
2927 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2928 if (nd.ni_dvp == vp)
2929 vrele(nd.ni_dvp);
2930 else
2931 vput(nd.ni_dvp);
2932 vrele(vp);
2933 return (EEXIST);
2934 }
2935 VATTR_NULL(&vattr);
2936 vattr.va_type = VDIR;
2937 vattr.va_mode =
2938 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
2939 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2940 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2941 if (!error)
2942 vput(nd.ni_vp);
2943 return (error);
2944 }
2945
2946 /*
2947 * Remove a directory file.
2948 */
2949 /* ARGSUSED */
2950 int
2951 sys_rmdir(p, v, retval)
2952 struct proc *p;
2953 void *v;
2954 register_t *retval;
2955 {
2956 struct sys_rmdir_args /* {
2957 syscallarg(const char *) path;
2958 } */ *uap = v;
2959 struct vnode *vp;
2960 int error;
2961 struct nameidata nd;
2962
2963 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2964 SCARG(uap, path), p);
2965 if ((error = namei(&nd)) != 0)
2966 return (error);
2967 vp = nd.ni_vp;
2968 if (vp->v_type != VDIR) {
2969 error = ENOTDIR;
2970 goto out;
2971 }
2972 /*
2973 * No rmdir "." please.
2974 */
2975 if (nd.ni_dvp == vp) {
2976 error = EINVAL;
2977 goto out;
2978 }
2979 /*
2980 * The root of a mounted filesystem cannot be deleted.
2981 */
2982 if (vp->v_flag & VROOT)
2983 error = EBUSY;
2984 out:
2985 if (!error) {
2986 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2987 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2988 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2989 } else {
2990 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2991 if (nd.ni_dvp == vp)
2992 vrele(nd.ni_dvp);
2993 else
2994 vput(nd.ni_dvp);
2995 vput(vp);
2996 }
2997 return (error);
2998 }
2999
3000 /*
3001 * Read a block of directory entries in a file system independent format.
3002 */
3003 int
3004 sys_getdents(p, v, retval)
3005 struct proc *p;
3006 void *v;
3007 register_t *retval;
3008 {
3009 struct sys_getdents_args /* {
3010 syscallarg(int) fd;
3011 syscallarg(char *) buf;
3012 syscallarg(size_t) count;
3013 } */ *uap = v;
3014 struct file *fp;
3015 int error, done;
3016
3017 /* getvnode() will use the descriptor for us */
3018 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3019 return (error);
3020 if ((fp->f_flag & FREAD) == 0) {
3021 error = EBADF;
3022 goto out;
3023 }
3024 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3025 SCARG(uap, count), &done, p, 0, 0);
3026 *retval = done;
3027 out:
3028 FILE_UNUSE(fp, p);
3029 return (error);
3030 }
3031
3032 /*
3033 * Set the mode mask for creation of filesystem nodes.
3034 */
3035 int
3036 sys_umask(p, v, retval)
3037 struct proc *p;
3038 void *v;
3039 register_t *retval;
3040 {
3041 struct sys_umask_args /* {
3042 syscallarg(mode_t) newmask;
3043 } */ *uap = v;
3044 struct cwdinfo *cwdi;
3045
3046 cwdi = p->p_cwdi;
3047 *retval = cwdi->cwdi_cmask;
3048 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3049 return (0);
3050 }
3051
3052 /*
3053 * Void all references to file by ripping underlying filesystem
3054 * away from vnode.
3055 */
3056 /* ARGSUSED */
3057 int
3058 sys_revoke(p, v, retval)
3059 struct proc *p;
3060 void *v;
3061 register_t *retval;
3062 {
3063 struct sys_revoke_args /* {
3064 syscallarg(const char *) path;
3065 } */ *uap = v;
3066 struct vnode *vp;
3067 struct vattr vattr;
3068 int error;
3069 struct nameidata nd;
3070
3071 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3072 if ((error = namei(&nd)) != 0)
3073 return (error);
3074 vp = nd.ni_vp;
3075 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
3076 goto out;
3077 if (p->p_ucred->cr_uid != vattr.va_uid &&
3078 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
3079 goto out;
3080 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
3081 VOP_REVOKE(vp, REVOKEALIAS);
3082 out:
3083 vrele(vp);
3084 return (error);
3085 }
3086
3087 /*
3088 * Convert a user file descriptor to a kernel file entry.
3089 */
3090 int
3091 getvnode(fdp, fd, fpp)
3092 struct filedesc *fdp;
3093 int fd;
3094 struct file **fpp;
3095 {
3096 struct vnode *vp;
3097 struct file *fp;
3098
3099 if ((fp = fd_getfile(fdp, fd)) == NULL)
3100 return (EBADF);
3101
3102 FILE_USE(fp);
3103
3104 if (fp->f_type != DTYPE_VNODE) {
3105 FILE_UNUSE(fp, NULL);
3106 return (EINVAL);
3107 }
3108
3109 vp = (struct vnode *)fp->f_data;
3110 if (vp->v_type == VBAD) {
3111 FILE_UNUSE(fp, NULL);
3112 return (EBADF);
3113 }
3114
3115 *fpp = fp;
3116 return (0);
3117 }
3118