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