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