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