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