vfs_syscalls.c revision 1.220 1 /* $NetBSD: vfs_syscalls.c,v 1.220 2005/05/29 22:24:15 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.220 2005/05/29 22:24:15 christos Exp $");
41
42 #include "opt_compat_netbsd.h"
43 #include "opt_compat_43.h"
44 #include "opt_ktrace.h"
45 #include "fss.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h>
52 #include <sys/file.h>
53 #include <sys/stat.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/proc.h>
57 #include <sys/uio.h>
58 #include <sys/malloc.h>
59 #include <sys/dirent.h>
60 #include <sys/extattr.h>
61 #include <sys/sysctl.h>
62 #include <sys/sa.h>
63 #include <sys/syscallargs.h>
64 #ifdef KTRACE
65 #include <sys/ktrace.h>
66 #endif
67 #ifdef VERIFIED_EXEC
68 #include <sys/verified_exec.h>
69 #endif
70
71 #include <miscfs/genfs/genfs.h>
72 #include <miscfs/syncfs/syncfs.h>
73
74 #if NFSS > 0
75 #include <dev/fssvar.h>
76 #endif
77
78 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
79
80 static int change_dir(struct nameidata *, struct proc *);
81 static int change_flags(struct vnode *, u_long, struct proc *);
82 static int change_mode(struct vnode *, int, struct proc *p);
83 static int change_owner(struct vnode *, uid_t, gid_t, struct proc *, int);
84 static int change_utimes(struct vnode *vp, const struct timeval *,
85 struct proc *p);
86 static int rename_files(const char *, const char *, struct proc *, int);
87
88 void checkdirs(struct vnode *);
89
90 int dovfsusermount = 0;
91
92 /*
93 * Virtual File System System Calls
94 */
95
96 /*
97 * Mount a file system.
98 */
99
100 #if defined(COMPAT_09) || defined(COMPAT_43)
101 /*
102 * This table is used to maintain compatibility with 4.3BSD
103 * and NetBSD 0.9 mount syscalls. Note, the order is important!
104 *
105 * Do not modify this table. It should only contain filesystems
106 * supported by NetBSD 0.9 and 4.3BSD.
107 */
108 const char * const mountcompatnames[] = {
109 NULL, /* 0 = MOUNT_NONE */
110 MOUNT_FFS, /* 1 = MOUNT_UFS */
111 MOUNT_NFS, /* 2 */
112 MOUNT_MFS, /* 3 */
113 MOUNT_MSDOS, /* 4 */
114 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */
115 MOUNT_FDESC, /* 6 */
116 MOUNT_KERNFS, /* 7 */
117 NULL, /* 8 = MOUNT_DEVFS */
118 MOUNT_AFS, /* 9 */
119 };
120 const int nmountcompatnames = sizeof(mountcompatnames) /
121 sizeof(mountcompatnames[0]);
122 #endif /* COMPAT_09 || COMPAT_43 */
123
124 /* ARGSUSED */
125 int
126 sys_mount(l, v, retval)
127 struct lwp *l;
128 void *v;
129 register_t *retval;
130 {
131 struct sys_mount_args /* {
132 syscallarg(const char *) type;
133 syscallarg(const char *) path;
134 syscallarg(int) flags;
135 syscallarg(void *) data;
136 } */ *uap = v;
137 struct proc *p = l->l_proc;
138 struct vnode *vp;
139 struct mount *mp;
140 int error, flag = 0;
141 char fstypename[MFSNAMELEN];
142 struct vattr va;
143 struct nameidata nd;
144 struct vfsops *vfs;
145
146 /*
147 * if MNT_GETARGS is specified, it should be only flag.
148 */
149
150 if ((SCARG(uap, flags) & MNT_GETARGS) != 0 &&
151 (SCARG(uap, flags) & ~MNT_GETARGS) != 0) {
152 return EINVAL;
153 }
154
155 if (dovfsusermount == 0 && (SCARG(uap, flags) & MNT_GETARGS) == 0 &&
156 (error = suser(p->p_ucred, &p->p_acflag)))
157 return (error);
158 /*
159 * Get vnode to be covered
160 */
161 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
162 SCARG(uap, path), p);
163 if ((error = namei(&nd)) != 0)
164 return (error);
165 vp = nd.ni_vp;
166 /*
167 * A lookup in VFS_MOUNT might result in an attempt to
168 * lock this vnode again, so make the lock recursive.
169 */
170 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
171 if (SCARG(uap, flags) & (MNT_UPDATE | MNT_GETARGS)) {
172 if ((vp->v_flag & VROOT) == 0) {
173 vput(vp);
174 return (EINVAL);
175 }
176 mp = vp->v_mount;
177 flag = mp->mnt_flag;
178 vfs = mp->mnt_op;
179 /*
180 * We only allow the filesystem to be reloaded if it
181 * is currently mounted read-only.
182 */
183 if ((SCARG(uap, flags) & MNT_RELOAD) &&
184 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
185 vput(vp);
186 return (EOPNOTSUPP); /* Needs translation */
187 }
188 /*
189 * In "highly secure" mode, don't let the caller do anything
190 * but downgrade a filesystem from read-write to read-only.
191 * (see also below; MNT_UPDATE or MNT_GETARGS is required.)
192 */
193 if (securelevel >= 2 &&
194 SCARG(uap, flags) != MNT_GETARGS &&
195 SCARG(uap, flags) !=
196 (mp->mnt_flag | MNT_RDONLY |
197 MNT_RELOAD | MNT_FORCE | MNT_UPDATE)) {
198 vput(vp);
199 return (EPERM);
200 }
201 mp->mnt_flag |= SCARG(uap, flags) &
202 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
203 /*
204 * Only root, or the user that did the original mount is
205 * permitted to update it.
206 */
207 if ((mp->mnt_flag & MNT_GETARGS) == 0 &&
208 mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
209 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
210 vput(vp);
211 return (error);
212 }
213 /*
214 * Do not allow NFS export by non-root users. For non-root
215 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
216 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
217 */
218 if (p->p_ucred->cr_uid != 0) {
219 if (SCARG(uap, flags) & MNT_EXPORTED) {
220 vput(vp);
221 return (EPERM);
222 }
223 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
224 if (flag & MNT_NOEXEC)
225 SCARG(uap, flags) |= MNT_NOEXEC;
226 }
227 if (vfs_busy(mp, LK_NOWAIT, 0)) {
228 vput(vp);
229 return (EPERM);
230 }
231 goto update;
232 } else {
233 if (securelevel >= 2) {
234 vput(vp);
235 return (EPERM);
236 }
237 }
238 /*
239 * If the user is not root, ensure that they own the directory
240 * onto which we are attempting to mount.
241 */
242 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
243 (va.va_uid != p->p_ucred->cr_uid &&
244 (error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
245 vput(vp);
246 return (error);
247 }
248 /*
249 * Do not allow NFS export by non-root users. For non-root users,
250 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
251 * mount point is already MNT_NOEXEC.
252 */
253 if (p->p_ucred->cr_uid != 0) {
254 if (SCARG(uap, flags) & MNT_EXPORTED) {
255 vput(vp);
256 return (EPERM);
257 }
258 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
259 if (vp->v_mount->mnt_flag & MNT_NOEXEC)
260 SCARG(uap, flags) |= MNT_NOEXEC;
261 }
262 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) {
263 vput(vp);
264 return (error);
265 }
266 if (vp->v_type != VDIR) {
267 vput(vp);
268 return (ENOTDIR);
269 }
270 error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
271 if (error) {
272 #if defined(COMPAT_09) || defined(COMPAT_43)
273 /*
274 * Historically filesystem types were identified by number.
275 * If we get an integer for the filesystem type instead of a
276 * string, we check to see if it matches one of the historic
277 * filesystem types.
278 */
279 u_long fsindex = (u_long)SCARG(uap, type);
280 if (fsindex >= nmountcompatnames ||
281 mountcompatnames[fsindex] == NULL) {
282 vput(vp);
283 return (ENODEV);
284 }
285 strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
286 #else
287 vput(vp);
288 return (error);
289 #endif
290 }
291 #ifdef COMPAT_10
292 /* Accept `ufs' as an alias for `ffs'. */
293 if (!strncmp(fstypename, "ufs", MFSNAMELEN))
294 strncpy(fstypename, "ffs", MFSNAMELEN);
295 #endif
296 if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
297 vput(vp);
298 return (ENODEV);
299 }
300 if (vp->v_mountedhere != NULL) {
301 vput(vp);
302 return (EBUSY);
303 }
304
305 /*
306 * Allocate and initialize the file system.
307 */
308 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
309 M_MOUNT, M_WAITOK);
310 memset((char *)mp, 0, (u_long)sizeof(struct mount));
311 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
312 simple_lock_init(&mp->mnt_slock);
313 (void)vfs_busy(mp, LK_NOWAIT, 0);
314 mp->mnt_op = vfs;
315 vfs->vfs_refcount++;
316 mp->mnt_vnodecovered = vp;
317 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
318 mp->mnt_unmounter = NULL;
319 mp->mnt_leaf = mp;
320
321 /*
322 * The underlying file system may refuse the mount for
323 * various reasons. Allow the user to force it to happen.
324 */
325 mp->mnt_flag |= SCARG(uap, flags) & MNT_FORCE;
326 update:
327 if ((SCARG(uap, flags) & MNT_GETARGS) == 0) {
328 /*
329 * Set the mount level flags.
330 */
331 if (SCARG(uap, flags) & MNT_RDONLY)
332 mp->mnt_flag |= MNT_RDONLY;
333 else if (mp->mnt_flag & MNT_RDONLY)
334 mp->mnt_iflag |= IMNT_WANTRDWR;
335 mp->mnt_flag &=
336 ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
337 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
338 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP);
339 mp->mnt_flag |= SCARG(uap, flags) &
340 (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
341 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
342 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
343 MNT_IGNORE);
344 }
345 /*
346 * Mount the filesystem.
347 */
348 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
349 if (mp->mnt_flag & (MNT_UPDATE | MNT_GETARGS)) {
350 if (mp->mnt_iflag & IMNT_WANTRDWR)
351 mp->mnt_flag &= ~MNT_RDONLY;
352 if (error)
353 mp->mnt_flag = flag;
354 mp->mnt_flag &=~
355 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
356 mp->mnt_iflag &=~ IMNT_WANTRDWR;
357 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
358 if (mp->mnt_syncer == NULL)
359 error = vfs_allocate_syncvnode(mp);
360 } else {
361 if (mp->mnt_syncer != NULL)
362 vfs_deallocate_syncvnode(mp);
363 }
364 vfs_unbusy(mp);
365 VOP_UNLOCK(vp, 0);
366 vrele(vp);
367 return (error);
368 }
369 /*
370 * Put the new filesystem on the mount list after root.
371 */
372 cache_purge(vp);
373 if (!error) {
374 mp->mnt_flag &=~
375 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
376 mp->mnt_iflag &=~ IMNT_WANTRDWR;
377 vp->v_mountedhere = mp;
378 simple_lock(&mountlist_slock);
379 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
380 simple_unlock(&mountlist_slock);
381 checkdirs(vp);
382 VOP_UNLOCK(vp, 0);
383 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
384 error = vfs_allocate_syncvnode(mp);
385 vfs_unbusy(mp);
386 (void) VFS_STATVFS(mp, &mp->mnt_stat, p);
387 if ((error = VFS_START(mp, 0, p)))
388 vrele(vp);
389 } else {
390 vp->v_mountedhere = (struct mount *)0;
391 vfs->vfs_refcount--;
392 vfs_unbusy(mp);
393 free(mp, M_MOUNT);
394 vput(vp);
395 }
396 return (error);
397 }
398
399 /*
400 * Scan all active processes to see if any of them have a current
401 * or root directory onto which the new filesystem has just been
402 * mounted. If so, replace them with the new mount point.
403 */
404 void
405 checkdirs(olddp)
406 struct vnode *olddp;
407 {
408 struct cwdinfo *cwdi;
409 struct vnode *newdp;
410 struct proc *p;
411
412 if (olddp->v_usecount == 1)
413 return;
414 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
415 panic("mount: lost mount");
416 proclist_lock_read();
417 PROCLIST_FOREACH(p, &allproc) {
418 cwdi = p->p_cwdi;
419 if (!cwdi)
420 continue;
421 if (cwdi->cwdi_cdir == olddp) {
422 vrele(cwdi->cwdi_cdir);
423 VREF(newdp);
424 cwdi->cwdi_cdir = newdp;
425 }
426 if (cwdi->cwdi_rdir == olddp) {
427 vrele(cwdi->cwdi_rdir);
428 VREF(newdp);
429 cwdi->cwdi_rdir = newdp;
430 }
431 }
432 proclist_unlock_read();
433 if (rootvnode == olddp) {
434 vrele(rootvnode);
435 VREF(newdp);
436 rootvnode = newdp;
437 }
438 vput(newdp);
439 }
440
441 /*
442 * Unmount a file system.
443 *
444 * Note: unmount takes a path to the vnode mounted on as argument,
445 * not special file (as before).
446 */
447 /* ARGSUSED */
448 int
449 sys_unmount(l, v, retval)
450 struct lwp *l;
451 void *v;
452 register_t *retval;
453 {
454 struct sys_unmount_args /* {
455 syscallarg(const char *) path;
456 syscallarg(int) flags;
457 } */ *uap = v;
458 struct proc *p = l->l_proc;
459 struct vnode *vp;
460 struct mount *mp;
461 int error;
462 struct nameidata nd;
463
464 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
465 SCARG(uap, path), p);
466 if ((error = namei(&nd)) != 0)
467 return (error);
468 vp = nd.ni_vp;
469 mp = vp->v_mount;
470
471 /*
472 * Only root, or the user that did the original mount is
473 * permitted to unmount this filesystem.
474 */
475 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
476 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
477 vput(vp);
478 return (error);
479 }
480
481 /*
482 * Don't allow unmounting the root file system.
483 */
484 if (mp->mnt_flag & MNT_ROOTFS) {
485 vput(vp);
486 return (EINVAL);
487 }
488
489 /*
490 * Must be the root of the filesystem
491 */
492 if ((vp->v_flag & VROOT) == 0) {
493 vput(vp);
494 return (EINVAL);
495 }
496 vput(vp);
497
498 /*
499 * XXX Freeze syncer. Must do this before locking the
500 * mount point. See dounmount() for details.
501 */
502 lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
503
504 if (vfs_busy(mp, 0, 0)) {
505 lockmgr(&syncer_lock, LK_RELEASE, NULL);
506 return (EBUSY);
507 }
508
509 return (dounmount(mp, SCARG(uap, flags), p));
510 }
511
512 /*
513 * Do the actual file system unmount. File system is assumed to have been
514 * marked busy by the caller.
515 */
516 int
517 dounmount(mp, flags, p)
518 struct mount *mp;
519 int flags;
520 struct proc *p;
521 {
522 struct vnode *coveredvp;
523 int error;
524 int async;
525 int used_syncer;
526
527 simple_lock(&mountlist_slock);
528 vfs_unbusy(mp);
529 used_syncer = (mp->mnt_syncer != NULL);
530
531 /*
532 * XXX Syncer must be frozen when we get here. This should really
533 * be done on a per-mountpoint basis, but especially the softdep
534 * code possibly called from the syncer doens't exactly work on a
535 * per-mountpoint basis, so the softdep code would become a maze
536 * of vfs_busy() calls.
537 *
538 * The caller of dounmount() must acquire syncer_lock because
539 * the syncer itself acquires locks in syncer_lock -> vfs_busy
540 * order, and we must preserve that order to avoid deadlock.
541 *
542 * So, if the file system did not use the syncer, now is
543 * the time to release the syncer_lock.
544 */
545 if (used_syncer == 0)
546 lockmgr(&syncer_lock, LK_RELEASE, NULL);
547
548 mp->mnt_iflag |= IMNT_UNMOUNT;
549 mp->mnt_unmounter = p;
550 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
551 vn_start_write(NULL, &mp, V_WAIT);
552
553 if (mp->mnt_flag & MNT_EXPUBLIC)
554 vfs_setpublicfs(NULL, NULL, NULL);
555 async = mp->mnt_flag & MNT_ASYNC;
556 mp->mnt_flag &= ~MNT_ASYNC;
557 cache_purgevfs(mp); /* remove cache entries for this file sys */
558 if (mp->mnt_syncer != NULL)
559 vfs_deallocate_syncvnode(mp);
560 error = 0;
561 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
562 #if NFSS > 0
563 error = fss_umount_hook(mp, (flags & MNT_FORCE));
564 #endif
565 if (error == 0)
566 error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p);
567 }
568 if (error == 0 || (flags & MNT_FORCE))
569 error = VFS_UNMOUNT(mp, flags, p);
570 vn_finished_write(mp, 0);
571 simple_lock(&mountlist_slock);
572 if (error) {
573 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
574 (void) vfs_allocate_syncvnode(mp);
575 mp->mnt_iflag &= ~IMNT_UNMOUNT;
576 mp->mnt_unmounter = NULL;
577 mp->mnt_flag |= async;
578 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
579 &mountlist_slock);
580 if (used_syncer)
581 lockmgr(&syncer_lock, LK_RELEASE, NULL);
582 simple_lock(&mp->mnt_slock);
583 while (mp->mnt_wcnt > 0) {
584 wakeup(mp);
585 ltsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1",
586 0, &mp->mnt_slock);
587 }
588 simple_unlock(&mp->mnt_slock);
589 return (error);
590 }
591 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
592 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
593 coveredvp->v_mountedhere = NULL;
594 vrele(coveredvp);
595 }
596 mp->mnt_op->vfs_refcount--;
597 if (LIST_FIRST(&mp->mnt_vnodelist) != NULL)
598 panic("unmount: dangling vnode");
599 mp->mnt_iflag |= IMNT_GONE;
600 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
601 if (used_syncer)
602 lockmgr(&syncer_lock, LK_RELEASE, NULL);
603 simple_lock(&mp->mnt_slock);
604 while (mp->mnt_wcnt > 0) {
605 wakeup(mp);
606 ltsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0, &mp->mnt_slock);
607 }
608 simple_unlock(&mp->mnt_slock);
609 free(mp, M_MOUNT);
610 return (0);
611 }
612
613 /*
614 * Sync each mounted filesystem.
615 */
616 #ifdef DEBUG
617 int syncprt = 0;
618 struct ctldebug debug0 = { "syncprt", &syncprt };
619 #endif
620
621 /* ARGSUSED */
622 int
623 sys_sync(l, v, retval)
624 struct lwp *l;
625 void *v;
626 register_t *retval;
627 {
628 struct mount *mp, *nmp;
629 int asyncflag;
630 struct proc *p = l == NULL ? &proc0 : l->l_proc;
631
632 simple_lock(&mountlist_slock);
633 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
634 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
635 nmp = mp->mnt_list.cqe_prev;
636 continue;
637 }
638 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
639 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
640 asyncflag = mp->mnt_flag & MNT_ASYNC;
641 mp->mnt_flag &= ~MNT_ASYNC;
642 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
643 if (asyncflag)
644 mp->mnt_flag |= MNT_ASYNC;
645 vn_finished_write(mp, 0);
646 }
647 simple_lock(&mountlist_slock);
648 nmp = mp->mnt_list.cqe_prev;
649 vfs_unbusy(mp);
650
651 }
652 simple_unlock(&mountlist_slock);
653 #ifdef DEBUG
654 if (syncprt)
655 vfs_bufstats();
656 #endif /* DEBUG */
657 return (0);
658 }
659
660 /*
661 * Change filesystem quotas.
662 */
663 /* ARGSUSED */
664 int
665 sys_quotactl(l, v, retval)
666 struct lwp *l;
667 void *v;
668 register_t *retval;
669 {
670 struct sys_quotactl_args /* {
671 syscallarg(const char *) path;
672 syscallarg(int) cmd;
673 syscallarg(int) uid;
674 syscallarg(caddr_t) arg;
675 } */ *uap = v;
676 struct proc *p = l->l_proc;
677 struct mount *mp;
678 int error;
679 struct nameidata nd;
680
681 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
682 if ((error = namei(&nd)) != 0)
683 return (error);
684 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | V_PCATCH);
685 vrele(nd.ni_vp);
686 if (error)
687 return (error);
688 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
689 SCARG(uap, arg), p);
690 vn_finished_write(mp, 0);
691 return (error);
692 }
693
694 int
695 dostatvfs(struct mount *mp, struct statvfs *sp, struct proc *p, int flags,
696 int root)
697 {
698 struct cwdinfo *cwdi = p->p_cwdi;
699 int error = 0;
700
701 /*
702 * If MNT_NOWAIT or MNT_LAZY is specified, do not
703 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
704 * overrides MNT_NOWAIT.
705 */
706 if (flags == MNT_NOWAIT || flags == MNT_LAZY ||
707 (flags != MNT_WAIT && flags != 0)) {
708 memcpy(sp, &mp->mnt_stat, sizeof(*sp));
709 goto done;
710 }
711
712 /* Get the filesystem stats now */
713 memset(sp, 0, sizeof(*sp));
714 if ((error = VFS_STATVFS(mp, sp, p)) != 0) {
715 return error;
716 }
717
718 if (cwdi->cwdi_rdir == NULL)
719 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
720 done:
721 if (cwdi->cwdi_rdir != NULL) {
722 size_t len;
723 char *bp;
724 char *path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
725 if (!path)
726 return ENOMEM;
727
728 bp = path + MAXPATHLEN;
729 *--bp = '\0';
730 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
731 MAXPATHLEN / 2, 0, p);
732 if (error) {
733 free(path, M_TEMP);
734 return error;
735 }
736 len = strlen(bp);
737 /*
738 * for mount points that are below our root, we can see
739 * them, so we fix up the pathname and return them. The
740 * rest we cannot see, so we don't allow viewing the
741 * data.
742 */
743 if (strncmp(bp, sp->f_mntonname, len) == 0) {
744 strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
745 sizeof(sp->f_mntonname));
746 if (sp->f_mntonname[0] == '\0')
747 (void)strlcpy(sp->f_mntonname, "/",
748 sizeof(sp->f_mntonname));
749 } else {
750 if (root)
751 (void)strlcpy(sp->f_mntonname, "/",
752 sizeof(sp->f_mntonname));
753 else
754 error = EPERM;
755 }
756 free(path, M_TEMP);
757 }
758 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
759 return error;
760 }
761
762 /*
763 * Get filesystem statistics.
764 */
765 /* ARGSUSED */
766 int
767 sys_statvfs1(l, v, retval)
768 struct lwp *l;
769 void *v;
770 register_t *retval;
771 {
772 struct sys_statvfs1_args /* {
773 syscallarg(const char *) path;
774 syscallarg(struct statvfs *) buf;
775 syscallarg(int) flags;
776 } */ *uap = v;
777 struct proc *p = l->l_proc;
778 struct mount *mp;
779 struct statvfs sbuf;
780 int error;
781 struct nameidata nd;
782
783 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
784 if ((error = namei(&nd)) != 0)
785 return error;
786 mp = nd.ni_vp->v_mount;
787 vrele(nd.ni_vp);
788 if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0)
789 return error;
790 return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
791 }
792
793 /*
794 * Get filesystem statistics.
795 */
796 /* ARGSUSED */
797 int
798 sys_fstatvfs1(l, v, retval)
799 struct lwp *l;
800 void *v;
801 register_t *retval;
802 {
803 struct sys_fstatvfs1_args /* {
804 syscallarg(int) fd;
805 syscallarg(struct statvfs *) buf;
806 syscallarg(int) flags;
807 } */ *uap = v;
808 struct proc *p = l->l_proc;
809 struct file *fp;
810 struct mount *mp;
811 struct statvfs sbuf;
812 int error;
813
814 /* getvnode() will use the descriptor for us */
815 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
816 return (error);
817 mp = ((struct vnode *)fp->f_data)->v_mount;
818 if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0)
819 goto out;
820 error = copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
821 out:
822 FILE_UNUSE(fp, p);
823 return error;
824 }
825
826
827 /*
828 * Get statistics on all filesystems.
829 */
830 int
831 sys_getvfsstat(l, v, retval)
832 struct lwp *l;
833 void *v;
834 register_t *retval;
835 {
836 struct sys_getvfsstat_args /* {
837 syscallarg(struct statvfs *) buf;
838 syscallarg(size_t) bufsize;
839 syscallarg(int) flags;
840 } */ *uap = v;
841 int root = 0;
842 struct proc *p = l->l_proc;
843 struct mount *mp, *nmp;
844 struct statvfs sbuf;
845 struct statvfs *sfsp;
846 size_t count, maxcount;
847 int error = 0;
848
849 maxcount = SCARG(uap, bufsize) / sizeof(struct statvfs);
850 sfsp = SCARG(uap, buf);
851 simple_lock(&mountlist_slock);
852 count = 0;
853 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
854 mp = nmp) {
855 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
856 nmp = CIRCLEQ_NEXT(mp, mnt_list);
857 continue;
858 }
859 if (sfsp && count < maxcount) {
860 error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 0);
861 if (error) {
862 simple_lock(&mountlist_slock);
863 nmp = CIRCLEQ_NEXT(mp, mnt_list);
864 vfs_unbusy(mp);
865 continue;
866 }
867 error = copyout(&sbuf, sfsp, sizeof(*sfsp));
868 if (error) {
869 vfs_unbusy(mp);
870 return (error);
871 }
872 sfsp++;
873 root |= strcmp(sbuf.f_mntonname, "/") == 0;
874 }
875 count++;
876 simple_lock(&mountlist_slock);
877 nmp = CIRCLEQ_NEXT(mp, mnt_list);
878 vfs_unbusy(mp);
879 }
880 simple_unlock(&mountlist_slock);
881 if (root == 0 && p->p_cwdi->cwdi_rdir) {
882 /*
883 * fake a root entry
884 */
885 if ((error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount, &sbuf, p,
886 SCARG(uap, flags), 1)) != 0)
887 return error;
888 if (sfsp)
889 error = copyout(&sbuf, sfsp, sizeof(*sfsp));
890 count++;
891 }
892 if (sfsp && count > maxcount)
893 *retval = maxcount;
894 else
895 *retval = count;
896 return error;
897 }
898
899 /*
900 * Change current working directory to a given file descriptor.
901 */
902 /* ARGSUSED */
903 int
904 sys_fchdir(l, v, retval)
905 struct lwp *l;
906 void *v;
907 register_t *retval;
908 {
909 struct sys_fchdir_args /* {
910 syscallarg(int) fd;
911 } */ *uap = v;
912 struct proc *p = l->l_proc;
913 struct filedesc *fdp = p->p_fd;
914 struct cwdinfo *cwdi = p->p_cwdi;
915 struct vnode *vp, *tdp;
916 struct mount *mp;
917 struct file *fp;
918 int error;
919
920 /* getvnode() will use the descriptor for us */
921 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
922 return (error);
923 vp = (struct vnode *)fp->f_data;
924
925 VREF(vp);
926 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
927 if (vp->v_type != VDIR)
928 error = ENOTDIR;
929 else
930 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
931 while (!error && (mp = vp->v_mountedhere) != NULL) {
932 if (vfs_busy(mp, 0, 0))
933 continue;
934 error = VFS_ROOT(mp, &tdp);
935 vfs_unbusy(mp);
936 if (error)
937 break;
938 vput(vp);
939 vp = tdp;
940 }
941 if (error) {
942 vput(vp);
943 goto out;
944 }
945 VOP_UNLOCK(vp, 0);
946
947 /*
948 * Disallow changing to a directory not under the process's
949 * current root directory (if there is one).
950 */
951 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
952 vrele(vp);
953 error = EPERM; /* operation not permitted */
954 goto out;
955 }
956
957 vrele(cwdi->cwdi_cdir);
958 cwdi->cwdi_cdir = vp;
959 out:
960 FILE_UNUSE(fp, p);
961 return (error);
962 }
963
964 /*
965 * Change this process's notion of the root directory to a given file descriptor.
966 */
967
968 int
969 sys_fchroot(l, v, retval)
970 struct lwp *l;
971 void *v;
972 register_t *retval;
973 {
974 struct sys_fchroot_args *uap = v;
975 struct proc *p = l->l_proc;
976 struct filedesc *fdp = p->p_fd;
977 struct cwdinfo *cwdi = p->p_cwdi;
978 struct vnode *vp;
979 struct file *fp;
980 int error;
981
982 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
983 return error;
984 /* getvnode() will use the descriptor for us */
985 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
986 return error;
987 vp = (struct vnode *) fp->f_data;
988 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
989 if (vp->v_type != VDIR)
990 error = ENOTDIR;
991 else
992 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
993 VOP_UNLOCK(vp, 0);
994 if (error)
995 goto out;
996 VREF(vp);
997
998 /*
999 * Prevent escaping from chroot by putting the root under
1000 * the working directory. Silently chdir to / if we aren't
1001 * already there.
1002 */
1003 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
1004 /*
1005 * XXX would be more failsafe to change directory to a
1006 * deadfs node here instead
1007 */
1008 vrele(cwdi->cwdi_cdir);
1009 VREF(vp);
1010 cwdi->cwdi_cdir = vp;
1011 }
1012
1013 if (cwdi->cwdi_rdir != NULL)
1014 vrele(cwdi->cwdi_rdir);
1015 cwdi->cwdi_rdir = vp;
1016 out:
1017 FILE_UNUSE(fp, p);
1018 return (error);
1019 }
1020
1021
1022
1023 /*
1024 * Change current working directory (``.'').
1025 */
1026 /* ARGSUSED */
1027 int
1028 sys_chdir(l, v, retval)
1029 struct lwp *l;
1030 void *v;
1031 register_t *retval;
1032 {
1033 struct sys_chdir_args /* {
1034 syscallarg(const char *) path;
1035 } */ *uap = v;
1036 struct proc *p = l->l_proc;
1037 struct cwdinfo *cwdi = p->p_cwdi;
1038 int error;
1039 struct nameidata nd;
1040
1041 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1042 SCARG(uap, path), p);
1043 if ((error = change_dir(&nd, p)) != 0)
1044 return (error);
1045 vrele(cwdi->cwdi_cdir);
1046 cwdi->cwdi_cdir = nd.ni_vp;
1047 return (0);
1048 }
1049
1050 /*
1051 * Change notion of root (``/'') directory.
1052 */
1053 /* ARGSUSED */
1054 int
1055 sys_chroot(l, v, retval)
1056 struct lwp *l;
1057 void *v;
1058 register_t *retval;
1059 {
1060 struct sys_chroot_args /* {
1061 syscallarg(const char *) path;
1062 } */ *uap = v;
1063 struct proc *p = l->l_proc;
1064 struct cwdinfo *cwdi = p->p_cwdi;
1065 struct vnode *vp;
1066 int error;
1067 struct nameidata nd;
1068
1069 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1070 return (error);
1071 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1072 SCARG(uap, path), p);
1073 if ((error = change_dir(&nd, p)) != 0)
1074 return (error);
1075 if (cwdi->cwdi_rdir != NULL)
1076 vrele(cwdi->cwdi_rdir);
1077 vp = nd.ni_vp;
1078 cwdi->cwdi_rdir = vp;
1079
1080 /*
1081 * Prevent escaping from chroot by putting the root under
1082 * the working directory. Silently chdir to / if we aren't
1083 * already there.
1084 */
1085 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
1086 /*
1087 * XXX would be more failsafe to change directory to a
1088 * deadfs node here instead
1089 */
1090 vrele(cwdi->cwdi_cdir);
1091 VREF(vp);
1092 cwdi->cwdi_cdir = vp;
1093 }
1094
1095 return (0);
1096 }
1097
1098 /*
1099 * Common routine for chroot and chdir.
1100 */
1101 static int
1102 change_dir(ndp, p)
1103 struct nameidata *ndp;
1104 struct proc *p;
1105 {
1106 struct vnode *vp;
1107 int error;
1108
1109 if ((error = namei(ndp)) != 0)
1110 return (error);
1111 vp = ndp->ni_vp;
1112 if (vp->v_type != VDIR)
1113 error = ENOTDIR;
1114 else
1115 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
1116
1117 if (error)
1118 vput(vp);
1119 else
1120 VOP_UNLOCK(vp, 0);
1121 return (error);
1122 }
1123
1124 /*
1125 * Check permissions, allocate an open file structure,
1126 * and call the device open routine if any.
1127 */
1128 int
1129 sys_open(l, v, retval)
1130 struct lwp *l;
1131 void *v;
1132 register_t *retval;
1133 {
1134 struct sys_open_args /* {
1135 syscallarg(const char *) path;
1136 syscallarg(int) flags;
1137 syscallarg(int) mode;
1138 } */ *uap = v;
1139 struct proc *p = l->l_proc;
1140 struct cwdinfo *cwdi = p->p_cwdi;
1141 struct filedesc *fdp = p->p_fd;
1142 struct file *fp;
1143 struct vnode *vp;
1144 int flags, cmode;
1145 int type, indx, error;
1146 struct flock lf;
1147 struct nameidata nd;
1148
1149 flags = FFLAGS(SCARG(uap, flags));
1150 if ((flags & (FREAD | FWRITE)) == 0)
1151 return (EINVAL);
1152 /* falloc() will use the file descriptor for us */
1153 if ((error = falloc(p, &fp, &indx)) != 0)
1154 return (error);
1155 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1156 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1157 l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1158 if ((error = vn_open(&nd, flags, cmode)) != 0) {
1159 FILE_UNUSE(fp, p);
1160 fdp->fd_ofiles[indx] = NULL;
1161 ffree(fp);
1162 if ((error == EDUPFD || error == EMOVEFD) &&
1163 l->l_dupfd >= 0 && /* XXX from fdopen */
1164 (error =
1165 dupfdopen(p, indx, l->l_dupfd, flags, error)) == 0) {
1166 *retval = indx;
1167 return (0);
1168 }
1169 if (error == ERESTART)
1170 error = EINTR;
1171 fdremove(fdp, indx);
1172 return (error);
1173 }
1174 l->l_dupfd = 0;
1175 vp = nd.ni_vp;
1176 fp->f_flag = flags & FMASK;
1177 fp->f_type = DTYPE_VNODE;
1178 fp->f_ops = &vnops;
1179 fp->f_data = vp;
1180 if (flags & (O_EXLOCK | O_SHLOCK)) {
1181 lf.l_whence = SEEK_SET;
1182 lf.l_start = 0;
1183 lf.l_len = 0;
1184 if (flags & O_EXLOCK)
1185 lf.l_type = F_WRLCK;
1186 else
1187 lf.l_type = F_RDLCK;
1188 type = F_FLOCK;
1189 if ((flags & FNONBLOCK) == 0)
1190 type |= F_WAIT;
1191 VOP_UNLOCK(vp, 0);
1192 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1193 if (error) {
1194 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1195 FILE_UNUSE(fp, p);
1196 ffree(fp);
1197 fdremove(fdp, indx);
1198 return (error);
1199 }
1200 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1201 fp->f_flag |= FHASLOCK;
1202 }
1203 VOP_UNLOCK(vp, 0);
1204 *retval = indx;
1205 FILE_SET_MATURE(fp);
1206 FILE_UNUSE(fp, p);
1207 return (0);
1208 }
1209
1210 /*
1211 * Get file handle system call
1212 */
1213 int
1214 sys_getfh(l, v, retval)
1215 struct lwp *l;
1216 void *v;
1217 register_t *retval;
1218 {
1219 struct sys_getfh_args /* {
1220 syscallarg(char *) fname;
1221 syscallarg(fhandle_t *) fhp;
1222 } */ *uap = v;
1223 struct proc *p = l->l_proc;
1224 struct vnode *vp;
1225 fhandle_t fh;
1226 int error;
1227 struct nameidata nd;
1228
1229 /*
1230 * Must be super user
1231 */
1232 error = suser(p->p_ucred, &p->p_acflag);
1233 if (error)
1234 return (error);
1235 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1236 SCARG(uap, fname), p);
1237 error = namei(&nd);
1238 if (error)
1239 return (error);
1240 vp = nd.ni_vp;
1241 memset(&fh, 0, sizeof(fh));
1242 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsidx;
1243 error = VFS_VPTOFH(vp, &fh.fh_fid);
1244 vput(vp);
1245 if (error)
1246 return (error);
1247 error = copyout(&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
1248 return (error);
1249 }
1250
1251 /*
1252 * Open a file given a file handle.
1253 *
1254 * Check permissions, allocate an open file structure,
1255 * and call the device open routine if any.
1256 */
1257 int
1258 sys_fhopen(l, v, retval)
1259 struct lwp *l;
1260 void *v;
1261 register_t *retval;
1262 {
1263 struct sys_fhopen_args /* {
1264 syscallarg(const fhandle_t *) fhp;
1265 syscallarg(int) flags;
1266 } */ *uap = v;
1267 struct proc *p = l->l_proc;
1268 struct filedesc *fdp = p->p_fd;
1269 struct file *fp;
1270 struct vnode *vp = NULL;
1271 struct mount *mp;
1272 struct ucred *cred = p->p_ucred;
1273 int flags;
1274 struct file *nfp;
1275 int type, indx, error=0;
1276 struct flock lf;
1277 struct vattr va;
1278 fhandle_t fh;
1279
1280 /*
1281 * Must be super user
1282 */
1283 if ((error = suser(p->p_ucred, &p->p_acflag)))
1284 return (error);
1285
1286 flags = FFLAGS(SCARG(uap, flags));
1287 if ((flags & (FREAD | FWRITE)) == 0)
1288 return (EINVAL);
1289 if ((flags & O_CREAT))
1290 return (EINVAL);
1291 /* falloc() will use the file descriptor for us */
1292 if ((error = falloc(p, &nfp, &indx)) != 0)
1293 return (error);
1294 fp = nfp;
1295 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1296 goto bad;
1297
1298 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
1299 error = ESTALE;
1300 goto bad;
1301 }
1302
1303 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
1304 vp = NULL; /* most likely unnecessary sanity for bad: */
1305 goto bad;
1306 }
1307
1308 /* Now do an effective vn_open */
1309
1310 if (vp->v_type == VSOCK) {
1311 error = EOPNOTSUPP;
1312 goto bad;
1313 }
1314 if (flags & FREAD) {
1315 if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
1316 goto bad;
1317 }
1318 if (flags & (FWRITE | O_TRUNC)) {
1319 if (vp->v_type == VDIR) {
1320 error = EISDIR;
1321 goto bad;
1322 }
1323 if ((error = vn_writechk(vp)) != 0 ||
1324 (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
1325 goto bad;
1326 }
1327 if (flags & O_TRUNC) {
1328 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
1329 goto bad;
1330 VOP_UNLOCK(vp, 0); /* XXX */
1331 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1332 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1333 VATTR_NULL(&va);
1334 va.va_size = 0;
1335 error = VOP_SETATTR(vp, &va, cred, p);
1336 vn_finished_write(mp, 0);
1337 if (error)
1338 goto bad;
1339 }
1340 if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
1341 goto bad;
1342 if (vp->v_type == VREG &&
1343 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
1344 error = EIO;
1345 goto bad;
1346 }
1347 if (flags & FWRITE)
1348 vp->v_writecount++;
1349
1350 /* done with modified vn_open, now finish what sys_open does. */
1351
1352 fp->f_flag = flags & FMASK;
1353 fp->f_type = DTYPE_VNODE;
1354 fp->f_ops = &vnops;
1355 fp->f_data = vp;
1356 if (flags & (O_EXLOCK | O_SHLOCK)) {
1357 lf.l_whence = SEEK_SET;
1358 lf.l_start = 0;
1359 lf.l_len = 0;
1360 if (flags & O_EXLOCK)
1361 lf.l_type = F_WRLCK;
1362 else
1363 lf.l_type = F_RDLCK;
1364 type = F_FLOCK;
1365 if ((flags & FNONBLOCK) == 0)
1366 type |= F_WAIT;
1367 VOP_UNLOCK(vp, 0);
1368 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1369 if (error) {
1370 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1371 FILE_UNUSE(fp, p);
1372 ffree(fp);
1373 fdremove(fdp, indx);
1374 return (error);
1375 }
1376 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1377 fp->f_flag |= FHASLOCK;
1378 }
1379 VOP_UNLOCK(vp, 0);
1380 *retval = indx;
1381 FILE_SET_MATURE(fp);
1382 FILE_UNUSE(fp, p);
1383 return (0);
1384
1385 bad:
1386 FILE_UNUSE(fp, p);
1387 ffree(fp);
1388 fdremove(fdp, indx);
1389 if (vp != NULL)
1390 vput(vp);
1391 return (error);
1392 }
1393
1394 /* ARGSUSED */
1395 int
1396 sys_fhstat(l, v, retval)
1397 struct lwp *l;
1398 void *v;
1399 register_t *retval;
1400 {
1401 struct sys_fhstat_args /* {
1402 syscallarg(const fhandle_t *) fhp;
1403 syscallarg(struct stat *) sb;
1404 } */ *uap = v;
1405 struct proc *p = l->l_proc;
1406 struct stat sb;
1407 int error;
1408 fhandle_t fh;
1409 struct mount *mp;
1410 struct vnode *vp;
1411
1412 /*
1413 * Must be super user
1414 */
1415 if ((error = suser(p->p_ucred, &p->p_acflag)))
1416 return (error);
1417
1418 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1419 return (error);
1420
1421 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1422 return (ESTALE);
1423 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1424 return (error);
1425 error = vn_stat(vp, &sb, p);
1426 vput(vp);
1427 if (error)
1428 return (error);
1429 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
1430 return (error);
1431 }
1432
1433 /* ARGSUSED */
1434 int
1435 sys_fhstatvfs1(l, v, retval)
1436 struct lwp *l;
1437 void *v;
1438 register_t *retval;
1439 {
1440 struct sys_fhstatvfs1_args /*
1441 syscallarg(const fhandle_t *) fhp;
1442 syscallarg(struct statvfs *) buf;
1443 syscallarg(int) flags;
1444 } */ *uap = v;
1445 struct proc *p = l->l_proc;
1446 struct statvfs sbuf;
1447 fhandle_t fh;
1448 struct mount *mp;
1449 struct vnode *vp;
1450 int error;
1451
1452 /*
1453 * Must be super user
1454 */
1455 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1456 return error;
1457
1458 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1459 return error;
1460
1461 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1462 return ESTALE;
1463 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1464 return error;
1465
1466 mp = vp->v_mount;
1467 if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0) {
1468 vput(vp);
1469 return error;
1470 }
1471 vput(vp);
1472 return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
1473 }
1474
1475 /*
1476 * Create a special file.
1477 */
1478 /* ARGSUSED */
1479 int
1480 sys_mknod(l, v, retval)
1481 struct lwp *l;
1482 void *v;
1483 register_t *retval;
1484 {
1485 struct sys_mknod_args /* {
1486 syscallarg(const char *) path;
1487 syscallarg(int) mode;
1488 syscallarg(int) dev;
1489 } */ *uap = v;
1490 struct proc *p = l->l_proc;
1491 struct vnode *vp;
1492 struct mount *mp;
1493 struct vattr vattr;
1494 int error;
1495 int whiteout = 0;
1496 struct nameidata nd;
1497
1498 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1499 return (error);
1500 restart:
1501 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1502 if ((error = namei(&nd)) != 0)
1503 return (error);
1504 vp = nd.ni_vp;
1505 if (vp != NULL)
1506 error = EEXIST;
1507 else {
1508 VATTR_NULL(&vattr);
1509 vattr.va_mode =
1510 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1511 vattr.va_rdev = SCARG(uap, dev);
1512 whiteout = 0;
1513
1514 switch (SCARG(uap, mode) & S_IFMT) {
1515 case S_IFMT: /* used by badsect to flag bad sectors */
1516 vattr.va_type = VBAD;
1517 break;
1518 case S_IFCHR:
1519 vattr.va_type = VCHR;
1520 break;
1521 case S_IFBLK:
1522 vattr.va_type = VBLK;
1523 break;
1524 case S_IFWHT:
1525 whiteout = 1;
1526 break;
1527 default:
1528 error = EINVAL;
1529 break;
1530 }
1531 }
1532 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1533 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1534 if (nd.ni_dvp == vp)
1535 vrele(nd.ni_dvp);
1536 else
1537 vput(nd.ni_dvp);
1538 if (vp)
1539 vrele(vp);
1540 if ((error = vn_start_write(NULL, &mp,
1541 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1542 return (error);
1543 goto restart;
1544 }
1545 if (!error) {
1546 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1547 if (whiteout) {
1548 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1549 if (error)
1550 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1551 vput(nd.ni_dvp);
1552 } else {
1553 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1554 &nd.ni_cnd, &vattr);
1555 if (error == 0)
1556 vput(nd.ni_vp);
1557 }
1558 } else {
1559 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1560 if (nd.ni_dvp == vp)
1561 vrele(nd.ni_dvp);
1562 else
1563 vput(nd.ni_dvp);
1564 if (vp)
1565 vrele(vp);
1566 }
1567 vn_finished_write(mp, 0);
1568 return (error);
1569 }
1570
1571 /*
1572 * Create a named pipe.
1573 */
1574 /* ARGSUSED */
1575 int
1576 sys_mkfifo(l, v, retval)
1577 struct lwp *l;
1578 void *v;
1579 register_t *retval;
1580 {
1581 struct sys_mkfifo_args /* {
1582 syscallarg(const char *) path;
1583 syscallarg(int) mode;
1584 } */ *uap = v;
1585 struct proc *p = l->l_proc;
1586 struct mount *mp;
1587 struct vattr vattr;
1588 int error;
1589 struct nameidata nd;
1590
1591 restart:
1592 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1593 if ((error = namei(&nd)) != 0)
1594 return (error);
1595 if (nd.ni_vp != NULL) {
1596 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1597 if (nd.ni_dvp == nd.ni_vp)
1598 vrele(nd.ni_dvp);
1599 else
1600 vput(nd.ni_dvp);
1601 vrele(nd.ni_vp);
1602 return (EEXIST);
1603 }
1604 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1605 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1606 if (nd.ni_dvp == nd.ni_vp)
1607 vrele(nd.ni_dvp);
1608 else
1609 vput(nd.ni_dvp);
1610 if (nd.ni_vp)
1611 vrele(nd.ni_vp);
1612 if ((error = vn_start_write(NULL, &mp,
1613 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1614 return (error);
1615 goto restart;
1616 }
1617 VATTR_NULL(&vattr);
1618 vattr.va_type = VFIFO;
1619 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1620 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1621 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1622 if (error == 0)
1623 vput(nd.ni_vp);
1624 vn_finished_write(mp, 0);
1625 return (error);
1626 }
1627
1628 /*
1629 * Make a hard file link.
1630 */
1631 /* ARGSUSED */
1632 int
1633 sys_link(l, v, retval)
1634 struct lwp *l;
1635 void *v;
1636 register_t *retval;
1637 {
1638 struct sys_link_args /* {
1639 syscallarg(const char *) path;
1640 syscallarg(const char *) link;
1641 } */ *uap = v;
1642 struct proc *p = l->l_proc;
1643 struct vnode *vp;
1644 struct mount *mp;
1645 struct nameidata nd;
1646 int error;
1647
1648 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1649 if ((error = namei(&nd)) != 0)
1650 return (error);
1651 vp = nd.ni_vp;
1652 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
1653 vrele(vp);
1654 return (error);
1655 }
1656 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1657 if ((error = namei(&nd)) != 0)
1658 goto out;
1659 if (nd.ni_vp) {
1660 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1661 if (nd.ni_dvp == nd.ni_vp)
1662 vrele(nd.ni_dvp);
1663 else
1664 vput(nd.ni_dvp);
1665 vrele(nd.ni_vp);
1666 error = EEXIST;
1667 goto out;
1668 }
1669 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1670 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1671 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1672 out:
1673 vrele(vp);
1674 vn_finished_write(mp, 0);
1675 return (error);
1676 }
1677
1678 /*
1679 * Make a symbolic link.
1680 */
1681 /* ARGSUSED */
1682 int
1683 sys_symlink(l, v, retval)
1684 struct lwp *l;
1685 void *v;
1686 register_t *retval;
1687 {
1688 struct sys_symlink_args /* {
1689 syscallarg(const char *) path;
1690 syscallarg(const char *) link;
1691 } */ *uap = v;
1692 struct proc *p = l->l_proc;
1693 struct mount *mp;
1694 struct vattr vattr;
1695 char *path;
1696 int error;
1697 struct nameidata nd;
1698
1699 path = PNBUF_GET();
1700 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1701 if (error)
1702 goto out;
1703 restart:
1704 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1705 if ((error = namei(&nd)) != 0)
1706 goto out;
1707 if (nd.ni_vp) {
1708 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1709 if (nd.ni_dvp == nd.ni_vp)
1710 vrele(nd.ni_dvp);
1711 else
1712 vput(nd.ni_dvp);
1713 vrele(nd.ni_vp);
1714 error = EEXIST;
1715 goto out;
1716 }
1717 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1718 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1719 if (nd.ni_dvp == nd.ni_vp)
1720 vrele(nd.ni_dvp);
1721 else
1722 vput(nd.ni_dvp);
1723 if ((error = vn_start_write(NULL, &mp,
1724 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1725 return (error);
1726 goto restart;
1727 }
1728 VATTR_NULL(&vattr);
1729 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1730 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1731 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1732 if (error == 0)
1733 vput(nd.ni_vp);
1734 vn_finished_write(mp, 0);
1735 out:
1736 PNBUF_PUT(path);
1737 return (error);
1738 }
1739
1740 /*
1741 * Delete a whiteout from the filesystem.
1742 */
1743 /* ARGSUSED */
1744 int
1745 sys_undelete(l, v, retval)
1746 struct lwp *l;
1747 void *v;
1748 register_t *retval;
1749 {
1750 struct sys_undelete_args /* {
1751 syscallarg(const char *) path;
1752 } */ *uap = v;
1753 struct proc *p = l->l_proc;
1754 int error;
1755 struct mount *mp;
1756 struct nameidata nd;
1757
1758 restart:
1759 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1760 SCARG(uap, path), p);
1761 error = namei(&nd);
1762 if (error)
1763 return (error);
1764
1765 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1766 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1767 if (nd.ni_dvp == nd.ni_vp)
1768 vrele(nd.ni_dvp);
1769 else
1770 vput(nd.ni_dvp);
1771 if (nd.ni_vp)
1772 vrele(nd.ni_vp);
1773 return (EEXIST);
1774 }
1775 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1776 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1777 if (nd.ni_dvp == nd.ni_vp)
1778 vrele(nd.ni_dvp);
1779 else
1780 vput(nd.ni_dvp);
1781 if ((error = vn_start_write(NULL, &mp,
1782 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1783 return (error);
1784 goto restart;
1785 }
1786 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1787 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1788 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1789 vput(nd.ni_dvp);
1790 vn_finished_write(mp, 0);
1791 return (error);
1792 }
1793
1794 /*
1795 * Delete a name from the filesystem.
1796 */
1797 /* ARGSUSED */
1798 int
1799 sys_unlink(l, v, retval)
1800 struct lwp *l;
1801 void *v;
1802 register_t *retval;
1803 {
1804 struct sys_unlink_args /* {
1805 syscallarg(const char *) path;
1806 } */ *uap = v;
1807 struct proc *p = l->l_proc;
1808 struct mount *mp;
1809 struct vnode *vp;
1810 int error;
1811 struct nameidata nd;
1812
1813 restart:
1814 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1815 SCARG(uap, path), p);
1816 if ((error = namei(&nd)) != 0)
1817 return (error);
1818 vp = nd.ni_vp;
1819
1820 /*
1821 * The root of a mounted filesystem cannot be deleted.
1822 */
1823 if (vp->v_flag & VROOT) {
1824 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1825 if (nd.ni_dvp == vp)
1826 vrele(nd.ni_dvp);
1827 else
1828 vput(nd.ni_dvp);
1829 vput(vp);
1830 error = EBUSY;
1831 goto out;
1832 }
1833
1834 /*
1835 * Remove the fingerprint from the list if there was one.
1836 */
1837 #ifdef VERIFIED_EXEC
1838 if ((error = veriexec_removechk(p, vp, nd.ni_dirp)) != 0) {
1839 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1840 if (nd.ni_dvp == vp)
1841 vrele(nd.ni_dvp);
1842 else
1843 vput(nd.ni_dvp);
1844 vput(vp);
1845 goto out;
1846 }
1847 #endif
1848
1849 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1850 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1851 if (nd.ni_dvp == vp)
1852 vrele(nd.ni_dvp);
1853 else
1854 vput(nd.ni_dvp);
1855 vput(vp);
1856 if ((error = vn_start_write(NULL, &mp,
1857 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1858 return (error);
1859 goto restart;
1860 }
1861 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1862 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1863 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1864 vn_finished_write(mp, 0);
1865 out:
1866 return (error);
1867 }
1868
1869 /*
1870 * Reposition read/write file offset.
1871 */
1872 int
1873 sys_lseek(l, v, retval)
1874 struct lwp *l;
1875 void *v;
1876 register_t *retval;
1877 {
1878 struct sys_lseek_args /* {
1879 syscallarg(int) fd;
1880 syscallarg(int) pad;
1881 syscallarg(off_t) offset;
1882 syscallarg(int) whence;
1883 } */ *uap = v;
1884 struct proc *p = l->l_proc;
1885 struct ucred *cred = p->p_ucred;
1886 struct filedesc *fdp = p->p_fd;
1887 struct file *fp;
1888 struct vnode *vp;
1889 struct vattr vattr;
1890 off_t newoff;
1891 int error;
1892
1893 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
1894 return (EBADF);
1895
1896 FILE_USE(fp);
1897
1898 vp = (struct vnode *)fp->f_data;
1899 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1900 error = ESPIPE;
1901 goto out;
1902 }
1903
1904 switch (SCARG(uap, whence)) {
1905 case SEEK_CUR:
1906 newoff = fp->f_offset + SCARG(uap, offset);
1907 break;
1908 case SEEK_END:
1909 error = VOP_GETATTR(vp, &vattr, cred, p);
1910 if (error)
1911 goto out;
1912 newoff = SCARG(uap, offset) + vattr.va_size;
1913 break;
1914 case SEEK_SET:
1915 newoff = SCARG(uap, offset);
1916 break;
1917 default:
1918 error = EINVAL;
1919 goto out;
1920 }
1921 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1922 goto out;
1923
1924 *(off_t *)retval = fp->f_offset = newoff;
1925 out:
1926 FILE_UNUSE(fp, p);
1927 return (error);
1928 }
1929
1930 /*
1931 * Positional read system call.
1932 */
1933 int
1934 sys_pread(l, v, retval)
1935 struct lwp *l;
1936 void *v;
1937 register_t *retval;
1938 {
1939 struct sys_pread_args /* {
1940 syscallarg(int) fd;
1941 syscallarg(void *) buf;
1942 syscallarg(size_t) nbyte;
1943 syscallarg(off_t) offset;
1944 } */ *uap = v;
1945 struct proc *p = l->l_proc;
1946 struct filedesc *fdp = p->p_fd;
1947 struct file *fp;
1948 struct vnode *vp;
1949 off_t offset;
1950 int error, fd = SCARG(uap, fd);
1951
1952 if ((fp = fd_getfile(fdp, fd)) == NULL)
1953 return (EBADF);
1954
1955 if ((fp->f_flag & FREAD) == 0) {
1956 simple_unlock(&fp->f_slock);
1957 return (EBADF);
1958 }
1959
1960 FILE_USE(fp);
1961
1962 vp = (struct vnode *)fp->f_data;
1963 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1964 error = ESPIPE;
1965 goto out;
1966 }
1967
1968 offset = SCARG(uap, offset);
1969
1970 /*
1971 * XXX This works because no file systems actually
1972 * XXX take any action on the seek operation.
1973 */
1974 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1975 goto out;
1976
1977 /* dofileread() will unuse the descriptor for us */
1978 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1979 &offset, 0, retval));
1980
1981 out:
1982 FILE_UNUSE(fp, p);
1983 return (error);
1984 }
1985
1986 /*
1987 * Positional scatter read system call.
1988 */
1989 int
1990 sys_preadv(l, v, retval)
1991 struct lwp *l;
1992 void *v;
1993 register_t *retval;
1994 {
1995 struct sys_preadv_args /* {
1996 syscallarg(int) fd;
1997 syscallarg(const struct iovec *) iovp;
1998 syscallarg(int) iovcnt;
1999 syscallarg(off_t) offset;
2000 } */ *uap = v;
2001 struct proc *p = l->l_proc;
2002 struct filedesc *fdp = p->p_fd;
2003 struct file *fp;
2004 struct vnode *vp;
2005 off_t offset;
2006 int error, fd = SCARG(uap, fd);
2007
2008 if ((fp = fd_getfile(fdp, fd)) == NULL)
2009 return (EBADF);
2010
2011 if ((fp->f_flag & FREAD) == 0) {
2012 simple_unlock(&fp->f_slock);
2013 return (EBADF);
2014 }
2015
2016 FILE_USE(fp);
2017
2018 vp = (struct vnode *)fp->f_data;
2019 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2020 error = ESPIPE;
2021 goto out;
2022 }
2023
2024 offset = SCARG(uap, offset);
2025
2026 /*
2027 * XXX This works because no file systems actually
2028 * XXX take any action on the seek operation.
2029 */
2030 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2031 goto out;
2032
2033 /* dofilereadv() will unuse the descriptor for us */
2034 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
2035 &offset, 0, retval));
2036
2037 out:
2038 FILE_UNUSE(fp, p);
2039 return (error);
2040 }
2041
2042 /*
2043 * Positional write system call.
2044 */
2045 int
2046 sys_pwrite(l, v, retval)
2047 struct lwp *l;
2048 void *v;
2049 register_t *retval;
2050 {
2051 struct sys_pwrite_args /* {
2052 syscallarg(int) fd;
2053 syscallarg(const void *) buf;
2054 syscallarg(size_t) nbyte;
2055 syscallarg(off_t) offset;
2056 } */ *uap = v;
2057 struct proc *p = l->l_proc;
2058 struct filedesc *fdp = p->p_fd;
2059 struct file *fp;
2060 struct vnode *vp;
2061 off_t offset;
2062 int error, fd = SCARG(uap, fd);
2063
2064 if ((fp = fd_getfile(fdp, fd)) == NULL)
2065 return (EBADF);
2066
2067 if ((fp->f_flag & FWRITE) == 0) {
2068 simple_unlock(&fp->f_slock);
2069 return (EBADF);
2070 }
2071
2072 FILE_USE(fp);
2073
2074 vp = (struct vnode *)fp->f_data;
2075 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2076 error = ESPIPE;
2077 goto out;
2078 }
2079
2080 offset = SCARG(uap, offset);
2081
2082 /*
2083 * XXX This works because no file systems actually
2084 * XXX take any action on the seek operation.
2085 */
2086 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2087 goto out;
2088
2089 /* dofilewrite() will unuse the descriptor for us */
2090 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2091 &offset, 0, retval));
2092
2093 out:
2094 FILE_UNUSE(fp, p);
2095 return (error);
2096 }
2097
2098 /*
2099 * Positional gather write system call.
2100 */
2101 int
2102 sys_pwritev(l, v, retval)
2103 struct lwp *l;
2104 void *v;
2105 register_t *retval;
2106 {
2107 struct sys_pwritev_args /* {
2108 syscallarg(int) fd;
2109 syscallarg(const struct iovec *) iovp;
2110 syscallarg(int) iovcnt;
2111 syscallarg(off_t) offset;
2112 } */ *uap = v;
2113 struct proc *p = l->l_proc;
2114 struct filedesc *fdp = p->p_fd;
2115 struct file *fp;
2116 struct vnode *vp;
2117 off_t offset;
2118 int error, fd = SCARG(uap, fd);
2119
2120 if ((fp = fd_getfile(fdp, fd)) == NULL)
2121 return (EBADF);
2122
2123 if ((fp->f_flag & FWRITE) == 0) {
2124 simple_unlock(&fp->f_slock);
2125 return (EBADF);
2126 }
2127
2128 FILE_USE(fp);
2129
2130 vp = (struct vnode *)fp->f_data;
2131 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2132 error = ESPIPE;
2133 goto out;
2134 }
2135
2136 offset = SCARG(uap, offset);
2137
2138 /*
2139 * XXX This works because no file systems actually
2140 * XXX take any action on the seek operation.
2141 */
2142 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2143 goto out;
2144
2145 /* dofilewritev() will unuse the descriptor for us */
2146 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
2147 &offset, 0, retval));
2148
2149 out:
2150 FILE_UNUSE(fp, p);
2151 return (error);
2152 }
2153
2154 /*
2155 * Check access permissions.
2156 */
2157 int
2158 sys_access(l, v, retval)
2159 struct lwp *l;
2160 void *v;
2161 register_t *retval;
2162 {
2163 struct sys_access_args /* {
2164 syscallarg(const char *) path;
2165 syscallarg(int) flags;
2166 } */ *uap = v;
2167 struct proc *p = l->l_proc;
2168 struct ucred *cred;
2169 struct vnode *vp;
2170 int error, flags;
2171 struct nameidata nd;
2172
2173 cred = crdup(p->p_ucred);
2174 cred->cr_uid = p->p_cred->p_ruid;
2175 cred->cr_gid = p->p_cred->p_rgid;
2176 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2177 SCARG(uap, path), p);
2178 /* Override default credentials */
2179 nd.ni_cnd.cn_cred = cred;
2180 if ((error = namei(&nd)) != 0)
2181 goto out;
2182 vp = nd.ni_vp;
2183
2184 /* Flags == 0 means only check for existence. */
2185 if (SCARG(uap, flags)) {
2186 flags = 0;
2187 if (SCARG(uap, flags) & R_OK)
2188 flags |= VREAD;
2189 if (SCARG(uap, flags) & W_OK)
2190 flags |= VWRITE;
2191 if (SCARG(uap, flags) & X_OK)
2192 flags |= VEXEC;
2193
2194 error = VOP_ACCESS(vp, flags, cred, p);
2195 if (!error && (flags & VWRITE))
2196 error = vn_writechk(vp);
2197 }
2198 vput(vp);
2199 out:
2200 crfree(cred);
2201 return (error);
2202 }
2203
2204 /*
2205 * Get file status; this version follows links.
2206 */
2207 /* ARGSUSED */
2208 int
2209 sys___stat13(l, v, retval)
2210 struct lwp *l;
2211 void *v;
2212 register_t *retval;
2213 {
2214 struct sys___stat13_args /* {
2215 syscallarg(const char *) path;
2216 syscallarg(struct stat *) ub;
2217 } */ *uap = v;
2218 struct proc *p = l->l_proc;
2219 struct stat sb;
2220 int error;
2221 struct nameidata nd;
2222
2223 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2224 SCARG(uap, path), p);
2225 if ((error = namei(&nd)) != 0)
2226 return (error);
2227 error = vn_stat(nd.ni_vp, &sb, p);
2228 vput(nd.ni_vp);
2229 if (error)
2230 return (error);
2231 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
2232 return (error);
2233 }
2234
2235 /*
2236 * Get file status; this version does not follow links.
2237 */
2238 /* ARGSUSED */
2239 int
2240 sys___lstat13(l, v, retval)
2241 struct lwp *l;
2242 void *v;
2243 register_t *retval;
2244 {
2245 struct sys___lstat13_args /* {
2246 syscallarg(const char *) path;
2247 syscallarg(struct stat *) ub;
2248 } */ *uap = v;
2249 struct proc *p = l->l_proc;
2250 struct stat sb;
2251 int error;
2252 struct nameidata nd;
2253
2254 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
2255 SCARG(uap, path), p);
2256 if ((error = namei(&nd)) != 0)
2257 return (error);
2258 error = vn_stat(nd.ni_vp, &sb, p);
2259 vput(nd.ni_vp);
2260 if (error)
2261 return (error);
2262 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
2263 return (error);
2264 }
2265
2266 /*
2267 * Get configurable pathname variables.
2268 */
2269 /* ARGSUSED */
2270 int
2271 sys_pathconf(l, v, retval)
2272 struct lwp *l;
2273 void *v;
2274 register_t *retval;
2275 {
2276 struct sys_pathconf_args /* {
2277 syscallarg(const char *) path;
2278 syscallarg(int) name;
2279 } */ *uap = v;
2280 struct proc *p = l->l_proc;
2281 int error;
2282 struct nameidata nd;
2283
2284 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2285 SCARG(uap, path), p);
2286 if ((error = namei(&nd)) != 0)
2287 return (error);
2288 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2289 vput(nd.ni_vp);
2290 return (error);
2291 }
2292
2293 /*
2294 * Return target name of a symbolic link.
2295 */
2296 /* ARGSUSED */
2297 int
2298 sys_readlink(l, v, retval)
2299 struct lwp *l;
2300 void *v;
2301 register_t *retval;
2302 {
2303 struct sys_readlink_args /* {
2304 syscallarg(const char *) path;
2305 syscallarg(char *) buf;
2306 syscallarg(size_t) count;
2307 } */ *uap = v;
2308 struct proc *p = l->l_proc;
2309 struct vnode *vp;
2310 struct iovec aiov;
2311 struct uio auio;
2312 int error;
2313 struct nameidata nd;
2314
2315 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
2316 SCARG(uap, path), p);
2317 if ((error = namei(&nd)) != 0)
2318 return (error);
2319 vp = nd.ni_vp;
2320 if (vp->v_type != VLNK)
2321 error = EINVAL;
2322 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2323 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
2324 aiov.iov_base = SCARG(uap, buf);
2325 aiov.iov_len = SCARG(uap, count);
2326 auio.uio_iov = &aiov;
2327 auio.uio_iovcnt = 1;
2328 auio.uio_offset = 0;
2329 auio.uio_rw = UIO_READ;
2330 auio.uio_segflg = UIO_USERSPACE;
2331 auio.uio_procp = p;
2332 auio.uio_resid = SCARG(uap, count);
2333 error = VOP_READLINK(vp, &auio, p->p_ucred);
2334 }
2335 vput(vp);
2336 *retval = SCARG(uap, count) - auio.uio_resid;
2337 return (error);
2338 }
2339
2340 /*
2341 * Change flags of a file given a path name.
2342 */
2343 /* ARGSUSED */
2344 int
2345 sys_chflags(l, v, retval)
2346 struct lwp *l;
2347 void *v;
2348 register_t *retval;
2349 {
2350 struct sys_chflags_args /* {
2351 syscallarg(const char *) path;
2352 syscallarg(u_long) flags;
2353 } */ *uap = v;
2354 struct proc *p = l->l_proc;
2355 struct vnode *vp;
2356 int error;
2357 struct nameidata nd;
2358
2359 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2360 if ((error = namei(&nd)) != 0)
2361 return (error);
2362 vp = nd.ni_vp;
2363 error = change_flags(vp, SCARG(uap, flags), p);
2364 vput(vp);
2365 return (error);
2366 }
2367
2368 /*
2369 * Change flags of a file given a file descriptor.
2370 */
2371 /* ARGSUSED */
2372 int
2373 sys_fchflags(l, v, retval)
2374 struct lwp *l;
2375 void *v;
2376 register_t *retval;
2377 {
2378 struct sys_fchflags_args /* {
2379 syscallarg(int) fd;
2380 syscallarg(u_long) flags;
2381 } */ *uap = v;
2382 struct proc *p = l->l_proc;
2383 struct vnode *vp;
2384 struct file *fp;
2385 int error;
2386
2387 /* getvnode() will use the descriptor for us */
2388 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2389 return (error);
2390 vp = (struct vnode *)fp->f_data;
2391 error = change_flags(vp, SCARG(uap, flags), p);
2392 VOP_UNLOCK(vp, 0);
2393 FILE_UNUSE(fp, p);
2394 return (error);
2395 }
2396
2397 /*
2398 * Change flags of a file given a path name; this version does
2399 * not follow links.
2400 */
2401 int
2402 sys_lchflags(l, v, retval)
2403 struct lwp *l;
2404 void *v;
2405 register_t *retval;
2406 {
2407 struct sys_lchflags_args /* {
2408 syscallarg(const char *) path;
2409 syscallarg(u_long) flags;
2410 } */ *uap = v;
2411 struct proc *p = l->l_proc;
2412 struct vnode *vp;
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 vp = nd.ni_vp;
2420 error = change_flags(vp, SCARG(uap, flags), p);
2421 vput(vp);
2422 return (error);
2423 }
2424
2425 /*
2426 * Common routine to change flags of a file.
2427 */
2428 int
2429 change_flags(vp, flags, p)
2430 struct vnode *vp;
2431 u_long flags;
2432 struct proc *p;
2433 {
2434 struct mount *mp;
2435 struct vattr vattr;
2436 int error;
2437
2438 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2439 return (error);
2440 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2441 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2442 /*
2443 * Non-superusers cannot change the flags on devices, even if they
2444 * own them.
2445 */
2446 if (suser(p->p_ucred, &p->p_acflag) != 0) {
2447 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2448 goto out;
2449 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2450 error = EINVAL;
2451 goto out;
2452 }
2453 }
2454 VATTR_NULL(&vattr);
2455 vattr.va_flags = flags;
2456 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2457 out:
2458 vn_finished_write(mp, 0);
2459 return (error);
2460 }
2461
2462 /*
2463 * Change mode of a file given path name; this version follows links.
2464 */
2465 /* ARGSUSED */
2466 int
2467 sys_chmod(l, v, retval)
2468 struct lwp *l;
2469 void *v;
2470 register_t *retval;
2471 {
2472 struct sys_chmod_args /* {
2473 syscallarg(const char *) path;
2474 syscallarg(int) mode;
2475 } */ *uap = v;
2476 struct proc *p = l->l_proc;
2477 int error;
2478 struct nameidata nd;
2479
2480 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2481 if ((error = namei(&nd)) != 0)
2482 return (error);
2483
2484 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2485
2486 vrele(nd.ni_vp);
2487 return (error);
2488 }
2489
2490 /*
2491 * Change mode of a file given a file descriptor.
2492 */
2493 /* ARGSUSED */
2494 int
2495 sys_fchmod(l, v, retval)
2496 struct lwp *l;
2497 void *v;
2498 register_t *retval;
2499 {
2500 struct sys_fchmod_args /* {
2501 syscallarg(int) fd;
2502 syscallarg(int) mode;
2503 } */ *uap = v;
2504 struct proc *p = l->l_proc;
2505 struct file *fp;
2506 int error;
2507
2508 /* getvnode() will use the descriptor for us */
2509 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2510 return (error);
2511
2512 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
2513 FILE_UNUSE(fp, p);
2514 return (error);
2515 }
2516
2517 /*
2518 * Change mode of a file given path name; this version does not follow links.
2519 */
2520 /* ARGSUSED */
2521 int
2522 sys_lchmod(l, v, retval)
2523 struct lwp *l;
2524 void *v;
2525 register_t *retval;
2526 {
2527 struct sys_lchmod_args /* {
2528 syscallarg(const char *) path;
2529 syscallarg(int) mode;
2530 } */ *uap = v;
2531 struct proc *p = l->l_proc;
2532 int error;
2533 struct nameidata nd;
2534
2535 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2536 if ((error = namei(&nd)) != 0)
2537 return (error);
2538
2539 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2540
2541 vrele(nd.ni_vp);
2542 return (error);
2543 }
2544
2545 /*
2546 * Common routine to set mode given a vnode.
2547 */
2548 static int
2549 change_mode(vp, mode, p)
2550 struct vnode *vp;
2551 int mode;
2552 struct proc *p;
2553 {
2554 struct mount *mp;
2555 struct vattr vattr;
2556 int error;
2557
2558 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2559 return (error);
2560 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2561 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2562 VATTR_NULL(&vattr);
2563 vattr.va_mode = mode & ALLPERMS;
2564 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2565 VOP_UNLOCK(vp, 0);
2566 vn_finished_write(mp, 0);
2567 return (error);
2568 }
2569
2570 /*
2571 * Set ownership given a path name; this version follows links.
2572 */
2573 /* ARGSUSED */
2574 int
2575 sys_chown(l, v, retval)
2576 struct lwp *l;
2577 void *v;
2578 register_t *retval;
2579 {
2580 struct sys_chown_args /* {
2581 syscallarg(const char *) path;
2582 syscallarg(uid_t) uid;
2583 syscallarg(gid_t) gid;
2584 } */ *uap = v;
2585 struct proc *p = l->l_proc;
2586 int error;
2587 struct nameidata nd;
2588
2589 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2590 if ((error = namei(&nd)) != 0)
2591 return (error);
2592
2593 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2594
2595 vrele(nd.ni_vp);
2596 return (error);
2597 }
2598
2599 /*
2600 * Set ownership given a path name; this version follows links.
2601 * Provides POSIX semantics.
2602 */
2603 /* ARGSUSED */
2604 int
2605 sys___posix_chown(l, v, retval)
2606 struct lwp *l;
2607 void *v;
2608 register_t *retval;
2609 {
2610 struct sys_chown_args /* {
2611 syscallarg(const char *) path;
2612 syscallarg(uid_t) uid;
2613 syscallarg(gid_t) gid;
2614 } */ *uap = v;
2615 struct proc *p = l->l_proc;
2616 int error;
2617 struct nameidata nd;
2618
2619 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2620 if ((error = namei(&nd)) != 0)
2621 return (error);
2622
2623 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2624
2625 vrele(nd.ni_vp);
2626 return (error);
2627 }
2628
2629 /*
2630 * Set ownership given a file descriptor.
2631 */
2632 /* ARGSUSED */
2633 int
2634 sys_fchown(l, v, retval)
2635 struct lwp *l;
2636 void *v;
2637 register_t *retval;
2638 {
2639 struct sys_fchown_args /* {
2640 syscallarg(int) fd;
2641 syscallarg(uid_t) uid;
2642 syscallarg(gid_t) gid;
2643 } */ *uap = v;
2644 struct proc *p = l->l_proc;
2645 int error;
2646 struct file *fp;
2647
2648 /* getvnode() will use the descriptor for us */
2649 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2650 return (error);
2651
2652 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2653 SCARG(uap, gid), p, 0);
2654 FILE_UNUSE(fp, p);
2655 return (error);
2656 }
2657
2658 /*
2659 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2660 */
2661 /* ARGSUSED */
2662 int
2663 sys___posix_fchown(l, v, retval)
2664 struct lwp *l;
2665 void *v;
2666 register_t *retval;
2667 {
2668 struct sys_fchown_args /* {
2669 syscallarg(int) fd;
2670 syscallarg(uid_t) uid;
2671 syscallarg(gid_t) gid;
2672 } */ *uap = v;
2673 struct proc *p = l->l_proc;
2674 int error;
2675 struct file *fp;
2676
2677 /* getvnode() will use the descriptor for us */
2678 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2679 return (error);
2680
2681 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2682 SCARG(uap, gid), p, 1);
2683 FILE_UNUSE(fp, p);
2684 return (error);
2685 }
2686
2687 /*
2688 * Set ownership given a path name; this version does not follow links.
2689 */
2690 /* ARGSUSED */
2691 int
2692 sys_lchown(l, v, retval)
2693 struct lwp *l;
2694 void *v;
2695 register_t *retval;
2696 {
2697 struct sys_lchown_args /* {
2698 syscallarg(const char *) path;
2699 syscallarg(uid_t) uid;
2700 syscallarg(gid_t) gid;
2701 } */ *uap = v;
2702 struct proc *p = l->l_proc;
2703 int error;
2704 struct nameidata nd;
2705
2706 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2707 if ((error = namei(&nd)) != 0)
2708 return (error);
2709
2710 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2711
2712 vrele(nd.ni_vp);
2713 return (error);
2714 }
2715
2716 /*
2717 * Set ownership given a path name; this version does not follow links.
2718 * Provides POSIX/XPG semantics.
2719 */
2720 /* ARGSUSED */
2721 int
2722 sys___posix_lchown(l, v, retval)
2723 struct lwp *l;
2724 void *v;
2725 register_t *retval;
2726 {
2727 struct sys_lchown_args /* {
2728 syscallarg(const char *) path;
2729 syscallarg(uid_t) uid;
2730 syscallarg(gid_t) gid;
2731 } */ *uap = v;
2732 struct proc *p = l->l_proc;
2733 int error;
2734 struct nameidata nd;
2735
2736 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2737 if ((error = namei(&nd)) != 0)
2738 return (error);
2739
2740 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2741
2742 vrele(nd.ni_vp);
2743 return (error);
2744 }
2745
2746 /*
2747 * Common routine to set ownership given a vnode.
2748 */
2749 static int
2750 change_owner(vp, uid, gid, p, posix_semantics)
2751 struct vnode *vp;
2752 uid_t uid;
2753 gid_t gid;
2754 struct proc *p;
2755 int posix_semantics;
2756 {
2757 struct mount *mp;
2758 struct vattr vattr;
2759 mode_t newmode;
2760 int error;
2761
2762 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2763 return (error);
2764 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2765 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2766 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2767 goto out;
2768
2769 #define CHANGED(x) ((int)(x) != -1)
2770 newmode = vattr.va_mode;
2771 if (posix_semantics) {
2772 /*
2773 * POSIX/XPG semantics: if the caller is not the super-user,
2774 * clear set-user-id and set-group-id bits. Both POSIX and
2775 * the XPG consider the behaviour for calls by the super-user
2776 * implementation-defined; we leave the set-user-id and set-
2777 * group-id settings intact in that case.
2778 */
2779 if (suser(p->p_ucred, NULL) != 0)
2780 newmode &= ~(S_ISUID | S_ISGID);
2781 } else {
2782 /*
2783 * NetBSD semantics: when changing owner and/or group,
2784 * clear the respective bit(s).
2785 */
2786 if (CHANGED(uid))
2787 newmode &= ~S_ISUID;
2788 if (CHANGED(gid))
2789 newmode &= ~S_ISGID;
2790 }
2791 /* Update va_mode iff altered. */
2792 if (vattr.va_mode == newmode)
2793 newmode = VNOVAL;
2794
2795 VATTR_NULL(&vattr);
2796 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2797 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2798 vattr.va_mode = newmode;
2799 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2800 #undef CHANGED
2801
2802 out:
2803 VOP_UNLOCK(vp, 0);
2804 vn_finished_write(mp, 0);
2805 return (error);
2806 }
2807
2808 /*
2809 * Set the access and modification times given a path name; this
2810 * version follows links.
2811 */
2812 /* ARGSUSED */
2813 int
2814 sys_utimes(l, v, retval)
2815 struct lwp *l;
2816 void *v;
2817 register_t *retval;
2818 {
2819 struct sys_utimes_args /* {
2820 syscallarg(const char *) path;
2821 syscallarg(const struct timeval *) tptr;
2822 } */ *uap = v;
2823 struct proc *p = l->l_proc;
2824 int error;
2825 struct nameidata nd;
2826
2827 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2828 if ((error = namei(&nd)) != 0)
2829 return (error);
2830
2831 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2832
2833 vrele(nd.ni_vp);
2834 return (error);
2835 }
2836
2837 /*
2838 * Set the access and modification times given a file descriptor.
2839 */
2840 /* ARGSUSED */
2841 int
2842 sys_futimes(l, v, retval)
2843 struct lwp *l;
2844 void *v;
2845 register_t *retval;
2846 {
2847 struct sys_futimes_args /* {
2848 syscallarg(int) fd;
2849 syscallarg(const struct timeval *) tptr;
2850 } */ *uap = v;
2851 struct proc *p = l->l_proc;
2852 int error;
2853 struct file *fp;
2854
2855 /* getvnode() will use the descriptor for us */
2856 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2857 return (error);
2858
2859 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
2860 FILE_UNUSE(fp, p);
2861 return (error);
2862 }
2863
2864 /*
2865 * Set the access and modification times given a path name; this
2866 * version does not follow links.
2867 */
2868 /* ARGSUSED */
2869 int
2870 sys_lutimes(l, v, retval)
2871 struct lwp *l;
2872 void *v;
2873 register_t *retval;
2874 {
2875 struct sys_lutimes_args /* {
2876 syscallarg(const char *) path;
2877 syscallarg(const struct timeval *) tptr;
2878 } */ *uap = v;
2879 struct proc *p = l->l_proc;
2880 int error;
2881 struct nameidata nd;
2882
2883 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2884 if ((error = namei(&nd)) != 0)
2885 return (error);
2886
2887 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2888
2889 vrele(nd.ni_vp);
2890 return (error);
2891 }
2892
2893 /*
2894 * Common routine to set access and modification times given a vnode.
2895 */
2896 static int
2897 change_utimes(vp, tptr, p)
2898 struct vnode *vp;
2899 const struct timeval *tptr;
2900 struct proc *p;
2901 {
2902 struct timeval tv[2];
2903 struct mount *mp;
2904 struct vattr vattr;
2905 int error;
2906
2907 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2908 return (error);
2909 VATTR_NULL(&vattr);
2910 if (tptr == NULL) {
2911 microtime(&tv[0]);
2912 tv[1] = tv[0];
2913 vattr.va_vaflags |= VA_UTIMES_NULL;
2914 } else {
2915 error = copyin(tptr, tv, sizeof(tv));
2916 if (error)
2917 goto out;
2918 }
2919 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2920 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2921 vattr.va_atime.tv_sec = tv[0].tv_sec;
2922 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2923 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2924 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2925 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2926 VOP_UNLOCK(vp, 0);
2927 out:
2928 vn_finished_write(mp, 0);
2929 return (error);
2930 }
2931
2932 /*
2933 * Truncate a file given its path name.
2934 */
2935 /* ARGSUSED */
2936 int
2937 sys_truncate(l, v, retval)
2938 struct lwp *l;
2939 void *v;
2940 register_t *retval;
2941 {
2942 struct sys_truncate_args /* {
2943 syscallarg(const char *) path;
2944 syscallarg(int) pad;
2945 syscallarg(off_t) length;
2946 } */ *uap = v;
2947 struct proc *p = l->l_proc;
2948 struct vnode *vp;
2949 struct mount *mp;
2950 struct vattr vattr;
2951 int error;
2952 struct nameidata nd;
2953
2954 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2955 if ((error = namei(&nd)) != 0)
2956 return (error);
2957 vp = nd.ni_vp;
2958 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
2959 vrele(vp);
2960 return (error);
2961 }
2962 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2963 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2964 if (vp->v_type == VDIR)
2965 error = EISDIR;
2966 else if ((error = vn_writechk(vp)) == 0 &&
2967 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2968 VATTR_NULL(&vattr);
2969 vattr.va_size = SCARG(uap, length);
2970 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2971 }
2972 vput(vp);
2973 vn_finished_write(mp, 0);
2974 return (error);
2975 }
2976
2977 /*
2978 * Truncate a file given a file descriptor.
2979 */
2980 /* ARGSUSED */
2981 int
2982 sys_ftruncate(l, v, retval)
2983 struct lwp *l;
2984 void *v;
2985 register_t *retval;
2986 {
2987 struct sys_ftruncate_args /* {
2988 syscallarg(int) fd;
2989 syscallarg(int) pad;
2990 syscallarg(off_t) length;
2991 } */ *uap = v;
2992 struct proc *p = l->l_proc;
2993 struct mount *mp;
2994 struct vattr vattr;
2995 struct vnode *vp;
2996 struct file *fp;
2997 int error;
2998
2999 /* getvnode() will use the descriptor for us */
3000 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3001 return (error);
3002 if ((fp->f_flag & FWRITE) == 0) {
3003 error = EINVAL;
3004 goto out;
3005 }
3006 vp = (struct vnode *)fp->f_data;
3007 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3008 FILE_UNUSE(fp, p);
3009 return (error);
3010 }
3011 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
3012 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3013 if (vp->v_type == VDIR)
3014 error = EISDIR;
3015 else if ((error = vn_writechk(vp)) == 0) {
3016 VATTR_NULL(&vattr);
3017 vattr.va_size = SCARG(uap, length);
3018 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
3019 }
3020 VOP_UNLOCK(vp, 0);
3021 vn_finished_write(mp, 0);
3022 out:
3023 FILE_UNUSE(fp, p);
3024 return (error);
3025 }
3026
3027 /*
3028 * Sync an open file.
3029 */
3030 /* ARGSUSED */
3031 int
3032 sys_fsync(l, v, retval)
3033 struct lwp *l;
3034 void *v;
3035 register_t *retval;
3036 {
3037 struct sys_fsync_args /* {
3038 syscallarg(int) fd;
3039 } */ *uap = v;
3040 struct proc *p = l->l_proc;
3041 struct vnode *vp;
3042 struct mount *mp;
3043 struct file *fp;
3044 int error;
3045
3046 /* getvnode() will use the descriptor for us */
3047 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3048 return (error);
3049 vp = (struct vnode *)fp->f_data;
3050 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3051 FILE_UNUSE(fp, p);
3052 return (error);
3053 }
3054 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3055 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p);
3056 if (error == 0 && bioops.io_fsync != NULL &&
3057 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3058 (*bioops.io_fsync)(vp, 0);
3059 VOP_UNLOCK(vp, 0);
3060 vn_finished_write(mp, 0);
3061 FILE_UNUSE(fp, p);
3062 return (error);
3063 }
3064
3065 /*
3066 * Sync a range of file data. API modeled after that found in AIX.
3067 *
3068 * FDATASYNC indicates that we need only save enough metadata to be able
3069 * to re-read the written data. Note we duplicate AIX's requirement that
3070 * the file be open for writing.
3071 */
3072 /* ARGSUSED */
3073 int
3074 sys_fsync_range(l, v, retval)
3075 struct lwp *l;
3076 void *v;
3077 register_t *retval;
3078 {
3079 struct sys_fsync_range_args /* {
3080 syscallarg(int) fd;
3081 syscallarg(int) flags;
3082 syscallarg(off_t) start;
3083 syscallarg(int) length;
3084 } */ *uap = v;
3085 struct proc *p = l->l_proc;
3086 struct vnode *vp;
3087 struct file *fp;
3088 int flags, nflags;
3089 off_t s, e, len;
3090 int error;
3091
3092 /* getvnode() will use the descriptor for us */
3093 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3094 return (error);
3095
3096 if ((fp->f_flag & FWRITE) == 0) {
3097 FILE_UNUSE(fp, p);
3098 return (EBADF);
3099 }
3100
3101 flags = SCARG(uap, flags);
3102 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3103 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3104 return (EINVAL);
3105 }
3106 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3107 if (flags & FDATASYNC)
3108 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3109 else
3110 nflags = FSYNC_WAIT;
3111 if (flags & FDISKSYNC)
3112 nflags |= FSYNC_CACHE;
3113
3114 len = SCARG(uap, length);
3115 /* If length == 0, we do the whole file, and s = l = 0 will do that */
3116 if (len) {
3117 s = SCARG(uap, start);
3118 e = s + len;
3119 if (e < s) {
3120 FILE_UNUSE(fp, p);
3121 return (EINVAL);
3122 }
3123 } else {
3124 e = 0;
3125 s = 0;
3126 }
3127
3128 vp = (struct vnode *)fp->f_data;
3129 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3130 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e, p);
3131
3132 if (error == 0 && bioops.io_fsync != NULL &&
3133 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3134 (*bioops.io_fsync)(vp, nflags);
3135
3136 VOP_UNLOCK(vp, 0);
3137 FILE_UNUSE(fp, p);
3138 return (error);
3139 }
3140
3141 /*
3142 * Sync the data of an open file.
3143 */
3144 /* ARGSUSED */
3145 int
3146 sys_fdatasync(l, v, retval)
3147 struct lwp *l;
3148 void *v;
3149 register_t *retval;
3150 {
3151 struct sys_fdatasync_args /* {
3152 syscallarg(int) fd;
3153 } */ *uap = v;
3154 struct proc *p = l->l_proc;
3155 struct vnode *vp;
3156 struct file *fp;
3157 int error;
3158
3159 /* getvnode() will use the descriptor for us */
3160 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3161 return (error);
3162 if ((fp->f_flag & FWRITE) == 0) {
3163 FILE_UNUSE(fp, p);
3164 return (EBADF);
3165 }
3166 vp = (struct vnode *)fp->f_data;
3167 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3168 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);
3169 VOP_UNLOCK(vp, 0);
3170 FILE_UNUSE(fp, p);
3171 return (error);
3172 }
3173
3174 /*
3175 * Rename files, (standard) BSD semantics frontend.
3176 */
3177 /* ARGSUSED */
3178 int
3179 sys_rename(l, v, retval)
3180 struct lwp *l;
3181 void *v;
3182 register_t *retval;
3183 {
3184 struct sys_rename_args /* {
3185 syscallarg(const char *) from;
3186 syscallarg(const char *) to;
3187 } */ *uap = v;
3188 struct proc *p = l->l_proc;
3189
3190 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
3191 }
3192
3193 /*
3194 * Rename files, POSIX semantics frontend.
3195 */
3196 /* ARGSUSED */
3197 int
3198 sys___posix_rename(l, v, retval)
3199 struct lwp *l;
3200 void *v;
3201 register_t *retval;
3202 {
3203 struct sys___posix_rename_args /* {
3204 syscallarg(const char *) from;
3205 syscallarg(const char *) to;
3206 } */ *uap = v;
3207 struct proc *p = l->l_proc;
3208
3209 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
3210 }
3211
3212 /*
3213 * Rename files. Source and destination must either both be directories,
3214 * or both not be directories. If target is a directory, it must be empty.
3215 * If `from' and `to' refer to the same object, the value of the `retain'
3216 * argument is used to determine whether `from' will be
3217 *
3218 * (retain == 0) deleted unless `from' and `to' refer to the same
3219 * object in the file system's name space (BSD).
3220 * (retain == 1) always retained (POSIX).
3221 */
3222 static int
3223 rename_files(from, to, p, retain)
3224 const char *from, *to;
3225 struct proc *p;
3226 int retain;
3227 {
3228 struct mount *mp = NULL;
3229 struct vnode *tvp, *fvp, *tdvp;
3230 struct nameidata fromnd, tond;
3231 int error;
3232
3233 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
3234 from, p);
3235 if ((error = namei(&fromnd)) != 0)
3236 return (error);
3237 fvp = fromnd.ni_vp;
3238 error = vn_start_write(fvp, &mp, V_WAIT | V_PCATCH);
3239 if (error != 0) {
3240 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3241 vrele(fromnd.ni_dvp);
3242 vrele(fvp);
3243 if (fromnd.ni_startdir)
3244 vrele(fromnd.ni_startdir);
3245 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3246 return (error);
3247 }
3248 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3249 (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, p);
3250 if ((error = namei(&tond)) != 0) {
3251 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3252 vrele(fromnd.ni_dvp);
3253 vrele(fvp);
3254 goto out1;
3255 }
3256 tdvp = tond.ni_dvp;
3257 tvp = tond.ni_vp;
3258
3259 if (tvp != NULL) {
3260 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3261 error = ENOTDIR;
3262 goto out;
3263 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3264 error = EISDIR;
3265 goto out;
3266 }
3267 }
3268
3269 if (fvp == tdvp)
3270 error = EINVAL;
3271
3272 /*
3273 * Source and destination refer to the same object.
3274 */
3275 if (fvp == tvp) {
3276 if (retain)
3277 error = -1;
3278 else if (fromnd.ni_dvp == tdvp &&
3279 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3280 !memcmp(fromnd.ni_cnd.cn_nameptr,
3281 tond.ni_cnd.cn_nameptr,
3282 fromnd.ni_cnd.cn_namelen))
3283 error = -1;
3284 }
3285
3286 out:
3287 if (!error) {
3288 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
3289 if (fromnd.ni_dvp != tdvp)
3290 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
3291 if (tvp) {
3292 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
3293 }
3294 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3295 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3296 } else {
3297 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3298 if (tdvp == tvp)
3299 vrele(tdvp);
3300 else
3301 vput(tdvp);
3302 if (tvp)
3303 vput(tvp);
3304 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3305 vrele(fromnd.ni_dvp);
3306 vrele(fvp);
3307 }
3308 vrele(tond.ni_startdir);
3309 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
3310 out1:
3311 vn_finished_write(mp, 0);
3312 if (fromnd.ni_startdir)
3313 vrele(fromnd.ni_startdir);
3314 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3315 return (error == -1 ? 0 : error);
3316 }
3317
3318 /*
3319 * Make a directory file.
3320 */
3321 /* ARGSUSED */
3322 int
3323 sys_mkdir(l, v, retval)
3324 struct lwp *l;
3325 void *v;
3326 register_t *retval;
3327 {
3328 struct sys_mkdir_args /* {
3329 syscallarg(const char *) path;
3330 syscallarg(int) mode;
3331 } */ *uap = v;
3332 struct proc *p = l->l_proc;
3333 struct mount *mp;
3334 struct vnode *vp;
3335 struct vattr vattr;
3336 int error;
3337 struct nameidata nd;
3338
3339 restart:
3340 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR, UIO_USERSPACE,
3341 SCARG(uap, path), p);
3342 if ((error = namei(&nd)) != 0)
3343 return (error);
3344 vp = nd.ni_vp;
3345 if (vp != NULL) {
3346 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3347 if (nd.ni_dvp == vp)
3348 vrele(nd.ni_dvp);
3349 else
3350 vput(nd.ni_dvp);
3351 vrele(vp);
3352 return (EEXIST);
3353 }
3354 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3355 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3356 if (nd.ni_dvp == vp)
3357 vrele(nd.ni_dvp);
3358 else
3359 vput(nd.ni_dvp);
3360 if ((error = vn_start_write(NULL, &mp,
3361 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
3362 return (error);
3363 goto restart;
3364 }
3365 VATTR_NULL(&vattr);
3366 vattr.va_type = VDIR;
3367 vattr.va_mode =
3368 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3369 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
3370 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3371 if (!error)
3372 vput(nd.ni_vp);
3373 vn_finished_write(mp, 0);
3374 return (error);
3375 }
3376
3377 /*
3378 * Remove a directory file.
3379 */
3380 /* ARGSUSED */
3381 int
3382 sys_rmdir(l, v, retval)
3383 struct lwp *l;
3384 void *v;
3385 register_t *retval;
3386 {
3387 struct sys_rmdir_args /* {
3388 syscallarg(const char *) path;
3389 } */ *uap = v;
3390 struct proc *p = l->l_proc;
3391 struct mount *mp;
3392 struct vnode *vp;
3393 int error;
3394 struct nameidata nd;
3395
3396 restart:
3397 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
3398 SCARG(uap, path), p);
3399 if ((error = namei(&nd)) != 0)
3400 return (error);
3401 vp = nd.ni_vp;
3402 if (vp->v_type != VDIR) {
3403 error = ENOTDIR;
3404 goto out;
3405 }
3406 /*
3407 * No rmdir "." please.
3408 */
3409 if (nd.ni_dvp == vp) {
3410 error = EINVAL;
3411 goto out;
3412 }
3413 /*
3414 * The root of a mounted filesystem cannot be deleted.
3415 */
3416 if (vp->v_flag & VROOT) {
3417 error = EBUSY;
3418 goto out;
3419 }
3420 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3421 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3422 if (nd.ni_dvp == vp)
3423 vrele(nd.ni_dvp);
3424 else
3425 vput(nd.ni_dvp);
3426 vput(vp);
3427 if ((error = vn_start_write(NULL, &mp,
3428 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
3429 return (error);
3430 goto restart;
3431 }
3432 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
3433 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
3434 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3435 vn_finished_write(mp, 0);
3436 return (error);
3437
3438 out:
3439 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3440 if (nd.ni_dvp == vp)
3441 vrele(nd.ni_dvp);
3442 else
3443 vput(nd.ni_dvp);
3444 vput(vp);
3445 return (error);
3446 }
3447
3448 /*
3449 * Read a block of directory entries in a file system independent format.
3450 */
3451 int
3452 sys_getdents(l, v, retval)
3453 struct lwp *l;
3454 void *v;
3455 register_t *retval;
3456 {
3457 struct sys_getdents_args /* {
3458 syscallarg(int) fd;
3459 syscallarg(char *) buf;
3460 syscallarg(size_t) count;
3461 } */ *uap = v;
3462 struct proc *p = l->l_proc;
3463 struct file *fp;
3464 int error, done;
3465
3466 /* getvnode() will use the descriptor for us */
3467 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3468 return (error);
3469 if ((fp->f_flag & FREAD) == 0) {
3470 error = EBADF;
3471 goto out;
3472 }
3473 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3474 SCARG(uap, count), &done, p, 0, 0);
3475 #ifdef KTRACE
3476 if (!error && KTRPOINT(p, KTR_GENIO)) {
3477 struct iovec iov;
3478 iov.iov_base = SCARG(uap, buf);
3479 iov.iov_len = done;
3480 ktrgenio(p, SCARG(uap, fd), UIO_READ, &iov, done, 0);
3481 }
3482 #endif
3483 *retval = done;
3484 out:
3485 FILE_UNUSE(fp, p);
3486 return (error);
3487 }
3488
3489 /*
3490 * Set the mode mask for creation of filesystem nodes.
3491 */
3492 int
3493 sys_umask(l, v, retval)
3494 struct lwp *l;
3495 void *v;
3496 register_t *retval;
3497 {
3498 struct sys_umask_args /* {
3499 syscallarg(mode_t) newmask;
3500 } */ *uap = v;
3501 struct proc *p = l->l_proc;
3502 struct cwdinfo *cwdi;
3503
3504 cwdi = p->p_cwdi;
3505 *retval = cwdi->cwdi_cmask;
3506 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3507 return (0);
3508 }
3509
3510 /*
3511 * Void all references to file by ripping underlying filesystem
3512 * away from vnode.
3513 */
3514 /* ARGSUSED */
3515 int
3516 sys_revoke(l, v, retval)
3517 struct lwp *l;
3518 void *v;
3519 register_t *retval;
3520 {
3521 struct sys_revoke_args /* {
3522 syscallarg(const char *) path;
3523 } */ *uap = v;
3524 struct proc *p = l->l_proc;
3525 struct mount *mp;
3526 struct vnode *vp;
3527 struct vattr vattr;
3528 int error;
3529 struct nameidata nd;
3530
3531 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3532 if ((error = namei(&nd)) != 0)
3533 return (error);
3534 vp = nd.ni_vp;
3535 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
3536 goto out;
3537 if (p->p_ucred->cr_uid != vattr.va_uid &&
3538 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
3539 goto out;
3540 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3541 goto out;
3542 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
3543 VOP_REVOKE(vp, REVOKEALL);
3544 vn_finished_write(mp, 0);
3545 out:
3546 vrele(vp);
3547 return (error);
3548 }
3549
3550 /*
3551 * Convert a user file descriptor to a kernel file entry.
3552 */
3553 int
3554 getvnode(fdp, fd, fpp)
3555 struct filedesc *fdp;
3556 int fd;
3557 struct file **fpp;
3558 {
3559 struct vnode *vp;
3560 struct file *fp;
3561
3562 if ((fp = fd_getfile(fdp, fd)) == NULL)
3563 return (EBADF);
3564
3565 FILE_USE(fp);
3566
3567 if (fp->f_type != DTYPE_VNODE) {
3568 FILE_UNUSE(fp, NULL);
3569 return (EINVAL);
3570 }
3571
3572 vp = (struct vnode *)fp->f_data;
3573 if (vp->v_type == VBAD) {
3574 FILE_UNUSE(fp, NULL);
3575 return (EBADF);
3576 }
3577
3578 *fpp = fp;
3579 return (0);
3580 }
3581
3582 /*
3583 * Push extended attribute configuration information into the VFS.
3584 *
3585 * NOTE: Not all file systems that support extended attributes will
3586 * require the use of this system call.
3587 */
3588 int
3589 sys_extattrctl(struct lwp *l, void *v, register_t *retval)
3590 {
3591 struct sys_extattrctl_args /* {
3592 syscallarg(const char *) path;
3593 syscallarg(int) cmd;
3594 syscallarg(const char *) filename;
3595 syscallarg(int) attrnamespace;
3596 syscallarg(const char *) attrname;
3597 } */ *uap = v;
3598 struct proc *p = l->l_proc;
3599 struct vnode *vp;
3600 struct nameidata nd;
3601 struct mount *mp;
3602 char attrname[EXTATTR_MAXNAMELEN];
3603 int error;
3604
3605 if (SCARG(uap, attrname) != NULL) {
3606 error = copyinstr(SCARG(uap, attrname), attrname,
3607 sizeof(attrname), NULL);
3608 if (error)
3609 return (error);
3610 }
3611
3612 vp = NULL;
3613 if (SCARG(uap, filename) != NULL) {
3614 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3615 SCARG(uap, filename), p);
3616 error = namei(&nd);
3617 if (error)
3618 return (error);
3619 vp = nd.ni_vp;
3620 }
3621
3622 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3623 error = namei(&nd);
3624 if (error) {
3625 if (vp != NULL)
3626 vput(vp);
3627 return (error);
3628 }
3629
3630 error = vn_start_write(nd.ni_vp, &mp, V_WAIT | V_PCATCH);
3631 if (error) {
3632 if (vp != NULL)
3633 vput(vp);
3634 return (error);
3635 }
3636
3637 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd), vp,
3638 SCARG(uap, attrnamespace),
3639 SCARG(uap, attrname) != NULL ? attrname : NULL, p);
3640
3641 vn_finished_write(mp, 0);
3642
3643 if (vp != NULL)
3644 vrele(vp);
3645
3646 return (error);
3647 }
3648
3649 /*
3650 * Set a named extended attribute on a file or directory.
3651 */
3652 static int
3653 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
3654 const void *data, size_t nbytes, struct proc *p, register_t *retval)
3655 {
3656 struct mount *mp;
3657 struct uio auio;
3658 struct iovec aiov;
3659 ssize_t cnt;
3660 int error;
3661
3662 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
3663 if (error)
3664 return (error);
3665 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
3666 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3667
3668 aiov.iov_base = __UNCONST(data); /* XXXUNCONST kills const */
3669 aiov.iov_len = nbytes;
3670 auio.uio_iov = &aiov;
3671 auio.uio_iovcnt = 1;
3672 auio.uio_offset = 0;
3673 if (nbytes > INT_MAX) {
3674 error = EINVAL;
3675 goto done;
3676 }
3677 auio.uio_resid = nbytes;
3678 auio.uio_rw = UIO_WRITE;
3679 auio.uio_segflg = UIO_USERSPACE;
3680 auio.uio_procp = p;
3681 cnt = nbytes;
3682
3683 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
3684 p->p_ucred, p);
3685 cnt -= auio.uio_resid;
3686 retval[0] = cnt;
3687
3688 done:
3689 VOP_UNLOCK(vp, 0);
3690 vn_finished_write(mp, 0);
3691 return (error);
3692 }
3693
3694 int
3695 sys_extattr_set_fd(struct lwp *l, void *v, register_t *retval)
3696 {
3697 struct sys_extattr_set_fd_args /* {
3698 syscallarg(int) fd;
3699 syscallarg(int) attrnamespace;
3700 syscallarg(const char *) attrname;
3701 syscallarg(const void *) data;
3702 syscallarg(size_t) nbytes;
3703 } */ *uap = v;
3704 struct proc *p = l->l_proc;
3705 struct file *fp;
3706 struct vnode *vp;
3707 char attrname[EXTATTR_MAXNAMELEN];
3708 int error;
3709
3710 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
3711 NULL);
3712 if (error)
3713 return (error);
3714
3715 error = getvnode(p->p_fd, SCARG(uap, fd), &fp);
3716 if (error)
3717 return (error);
3718 vp = (struct vnode *) fp->f_data;
3719
3720 error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname,
3721 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
3722
3723 FILE_UNUSE(fp, p);
3724 return (error);
3725 }
3726
3727 int
3728 sys_extattr_set_file(struct lwp *l, void *v, register_t *retval)
3729 {
3730 struct sys_extattr_set_file_args /* {
3731 syscallarg(const char *) path;
3732 syscallarg(int) attrnamespace;
3733 syscallarg(const char *) attrname;
3734 syscallarg(const void *) data;
3735 syscallarg(size_t) nbytes;
3736 } */ *uap = v;
3737 struct proc *p = l->l_proc;
3738 struct nameidata nd;
3739 char attrname[EXTATTR_MAXNAMELEN];
3740 int error;
3741
3742 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
3743 NULL);
3744 if (error)
3745 return (error);
3746
3747 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3748 error = namei(&nd);
3749 if (error)
3750 return (error);
3751
3752 error = extattr_set_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
3753 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
3754
3755 vrele(nd.ni_vp);
3756 return (error);
3757 }
3758
3759 int
3760 sys_extattr_set_link(struct lwp *l, void *v, register_t *retval)
3761 {
3762 struct sys_extattr_set_link_args /* {
3763 syscallarg(const char *) path;
3764 syscallarg(int) attrnamespace;
3765 syscallarg(const char *) attrname;
3766 syscallarg(const void *) data;
3767 syscallarg(size_t) nbytes;
3768 } */ *uap = v;
3769 struct proc *p = l->l_proc;
3770 struct nameidata nd;
3771 char attrname[EXTATTR_MAXNAMELEN];
3772 int error;
3773
3774 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
3775 NULL);
3776 if (error)
3777 return (error);
3778
3779 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3780 error = namei(&nd);
3781 if (error)
3782 return (error);
3783
3784 error = extattr_set_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
3785 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
3786
3787 vrele(nd.ni_vp);
3788 return (error);
3789 }
3790
3791 /*
3792 * Get a named extended attribute on a file or directory.
3793 */
3794 static int
3795 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
3796 void *data, size_t nbytes, struct proc *p, register_t *retval)
3797 {
3798 struct uio auio, *auiop;
3799 struct iovec aiov;
3800 ssize_t cnt;
3801 size_t size, *sizep;
3802 int error;
3803
3804 VOP_LEASE(vp, p, p->p_ucred, LEASE_READ);
3805 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3806
3807 /*
3808 * Slightly unusual semantics: if the user provides a NULL data
3809 * pointer, they don't want to receive the data, just the maximum
3810 * read length.
3811 */
3812 auiop = NULL;
3813 sizep = NULL;
3814 cnt = 0;
3815 if (data != NULL) {
3816 aiov.iov_base = data;
3817 aiov.iov_len = nbytes;
3818 auio.uio_iov = &aiov;
3819 auio.uio_offset = 0;
3820 if (nbytes > INT_MAX) {
3821 error = EINVAL;
3822 goto done;
3823 }
3824 auio.uio_resid = nbytes;
3825 auio.uio_rw = UIO_READ;
3826 auio.uio_segflg = UIO_USERSPACE;
3827 auio.uio_procp = p;
3828 auiop = &auio;
3829 cnt = nbytes;
3830 } else
3831 sizep = &size;
3832
3833 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
3834 p->p_ucred, p);
3835
3836 if (auiop != NULL) {
3837 cnt -= auio.uio_resid;
3838 retval[0] = cnt;
3839 } else
3840 retval[0] = size;
3841
3842 done:
3843 VOP_UNLOCK(vp, 0);
3844 return (error);
3845 }
3846
3847 int
3848 sys_extattr_get_fd(struct lwp *l, void *v, register_t *retval)
3849 {
3850 struct sys_extattr_get_fd_args /* {
3851 syscallarg(int) fd;
3852 syscallarg(int) attrnamespace;
3853 syscallarg(const char *) attrname;
3854 syscallarg(void *) data;
3855 syscallarg(size_t) nbytes;
3856 } */ *uap = v;
3857 struct proc *p = l->l_proc;
3858 struct file *fp;
3859 struct vnode *vp;
3860 char attrname[EXTATTR_MAXNAMELEN];
3861 int error;
3862
3863 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
3864 NULL);
3865 if (error)
3866 return (error);
3867
3868 error = getvnode(p->p_fd, SCARG(uap, fd), &fp);
3869 if (error)
3870 return (error);
3871 vp = (struct vnode *) fp->f_data;
3872
3873 error = extattr_get_vp(vp, SCARG(uap, attrnamespace), attrname,
3874 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
3875
3876 FILE_UNUSE(fp, p);
3877 return (error);
3878 }
3879
3880 int
3881 sys_extattr_get_file(struct lwp *l, void *v, register_t *retval)
3882 {
3883 struct sys_extattr_get_file_args /* {
3884 syscallarg(const char *) path;
3885 syscallarg(int) attrnamespace;
3886 syscallarg(const char *) attrname;
3887 syscallarg(void *) data;
3888 syscallarg(size_t) nbytes;
3889 } */ *uap = v;
3890 struct proc *p = l->l_proc;
3891 struct nameidata nd;
3892 char attrname[EXTATTR_MAXNAMELEN];
3893 int error;
3894
3895 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
3896 NULL);
3897 if (error)
3898 return (error);
3899
3900 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3901 error = namei(&nd);
3902 if (error)
3903 return (error);
3904
3905 error = extattr_get_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
3906 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
3907
3908 vrele(nd.ni_vp);
3909 return (error);
3910 }
3911
3912 int
3913 sys_extattr_get_link(struct lwp *l, void *v, register_t *retval)
3914 {
3915 struct sys_extattr_get_link_args /* {
3916 syscallarg(const char *) path;
3917 syscallarg(int) attrnamespace;
3918 syscallarg(const char *) attrname;
3919 syscallarg(void *) data;
3920 syscallarg(size_t) nbytes;
3921 } */ *uap = v;
3922 struct proc *p = l->l_proc;
3923 struct nameidata nd;
3924 char attrname[EXTATTR_MAXNAMELEN];
3925 int error;
3926
3927 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
3928 NULL);
3929 if (error)
3930 return (error);
3931
3932 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3933 error = namei(&nd);
3934 if (error)
3935 return (error);
3936
3937 error = extattr_get_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
3938 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
3939
3940 vrele(nd.ni_vp);
3941 return (error);
3942 }
3943
3944 /*
3945 * Delete a named extended attribute on a file or directory.
3946 */
3947 static int
3948 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
3949 struct proc *p)
3950 {
3951 struct mount *mp;
3952 int error;
3953
3954 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
3955 if (error)
3956 return (error);
3957 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
3958 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3959
3960 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, p->p_ucred, p);
3961 if (error == EOPNOTSUPP)
3962 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
3963 p->p_ucred, p);
3964
3965 VOP_UNLOCK(vp, 0);
3966 vn_finished_write(mp, 0);
3967 return (error);
3968 }
3969
3970 int
3971 sys_extattr_delete_fd(struct lwp *l, void *v, register_t *retval)
3972 {
3973 struct sys_extattr_delete_fd_args /* {
3974 syscallarg(int) fd;
3975 syscallarg(int) attrnamespace;
3976 syscallarg(const char *) attrname;
3977 } */ *uap = v;
3978 struct proc *p = l->l_proc;
3979 struct file *fp;
3980 struct vnode *vp;
3981 char attrname[EXTATTR_MAXNAMELEN];
3982 int error;
3983
3984 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
3985 NULL);
3986 if (error)
3987 return (error);
3988
3989 error = getvnode(p->p_fd, SCARG(uap, fd), &fp);
3990 if (error)
3991 return (error);
3992 vp = (struct vnode *) fp->f_data;
3993
3994 error = extattr_delete_vp(vp, SCARG(uap, attrnamespace), attrname, p);
3995
3996 FILE_UNUSE(fp, p);
3997 return (error);
3998 }
3999
4000 int
4001 sys_extattr_delete_file(struct lwp *l, void *v, register_t *retval)
4002 {
4003 struct sys_extattr_delete_file_args /* {
4004 syscallarg(const char *) path;
4005 syscallarg(int) attrnamespace;
4006 syscallarg(const char *) attrname;
4007 } */ *uap = v;
4008 struct proc *p = l->l_proc;
4009 struct nameidata nd;
4010 char attrname[EXTATTR_MAXNAMELEN];
4011 int error;
4012
4013 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
4014 NULL);
4015 if (error)
4016 return (error);
4017
4018 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
4019 error = namei(&nd);
4020 if (error)
4021 return (error);
4022
4023 error = extattr_delete_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
4024 p);
4025
4026 vrele(nd.ni_vp);
4027 return (error);
4028 }
4029
4030 int
4031 sys_extattr_delete_link(struct lwp *l, void *v, register_t *retval)
4032 {
4033 struct sys_extattr_delete_link_args /* {
4034 syscallarg(const char *) path;
4035 syscallarg(int) attrnamespace;
4036 syscallarg(const char *) attrname;
4037 } */ *uap = v;
4038 struct proc *p = l->l_proc;
4039 struct nameidata nd;
4040 char attrname[EXTATTR_MAXNAMELEN];
4041 int error;
4042
4043 error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
4044 NULL);
4045 if (error)
4046 return (error);
4047
4048 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
4049 error = namei(&nd);
4050 if (error)
4051 return (error);
4052
4053 error = extattr_delete_vp(nd.ni_vp, SCARG(uap, attrnamespace), attrname,
4054 p);
4055
4056 vrele(nd.ni_vp);
4057 return (error);
4058 }
4059
4060 /*
4061 * Retrieve a list of extended attributes on a file or directory.
4062 */
4063 static int
4064 extattr_list_vp(struct vnode *vp, int attrnamespace, void *data, size_t nbytes,
4065 struct proc *p, register_t *retval)
4066 {
4067 struct uio auio, *auiop;
4068 size_t size, *sizep;
4069 struct iovec aiov;
4070 ssize_t cnt;
4071 int error;
4072
4073 VOP_LEASE(vp, p, p->p_ucred, LEASE_READ);
4074 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4075
4076 auiop = NULL;
4077 sizep = NULL;
4078 cnt = 0;
4079 if (data != NULL) {
4080 aiov.iov_base = data;
4081 aiov.iov_len = nbytes;
4082 auio.uio_iov = &aiov;
4083 auio.uio_offset = 0;
4084 if (nbytes > INT_MAX) {
4085 error = EINVAL;
4086 goto done;
4087 }
4088 auio.uio_resid = nbytes;
4089 auio.uio_rw = UIO_READ;
4090 auio.uio_segflg = UIO_USERSPACE;
4091 auio.uio_procp = p;
4092 auiop = &auio;
4093 cnt = nbytes;
4094 } else
4095 sizep = &size;
4096
4097 error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep,
4098 p->p_ucred, p);
4099
4100 if (auiop != NULL) {
4101 cnt -= auio.uio_resid;
4102 retval[0] = cnt;
4103 } else
4104 retval[0] = size;
4105
4106 done:
4107 VOP_UNLOCK(vp, 0);
4108 return (error);
4109 }
4110
4111 int
4112 sys_extattr_list_fd(struct lwp *l, void *v, register_t *retval)
4113 {
4114 struct sys_extattr_list_fd_args /* {
4115 syscallarg(int) fd;
4116 syscallarg(int) attrnamespace;
4117 syscallarg(void *) data;
4118 syscallarg(size_t) nbytes;
4119 } */ *uap = v;
4120 struct proc *p = l->l_proc;
4121 struct file *fp;
4122 struct vnode *vp;
4123 int error;
4124
4125 error = getvnode(p->p_fd, SCARG(uap, fd), &fp);
4126 if (error)
4127 return (error);
4128 vp = (struct vnode *) fp->f_data;
4129
4130 error = extattr_list_vp(vp, SCARG(uap, attrnamespace),
4131 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
4132
4133 FILE_UNUSE(fp, p);
4134 return (error);
4135 }
4136
4137 int
4138 sys_extattr_list_file(struct lwp *l, void *v, register_t *retval)
4139 {
4140 struct sys_extattr_list_file_args /* {
4141 syscallarg(const char *) path;
4142 syscallarg(int) attrnamespace;
4143 syscallarg(void *) data;
4144 syscallarg(size_t) nbytes;
4145 } */ *uap = v;
4146 struct proc *p = l->l_proc;
4147 struct nameidata nd;
4148 int error;
4149
4150 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
4151 error = namei(&nd);
4152 if (error)
4153 return (error);
4154
4155 error = extattr_list_vp(nd.ni_vp, SCARG(uap, attrnamespace),
4156 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
4157
4158 vrele(nd.ni_vp);
4159 return (error);
4160 }
4161
4162 int
4163 sys_extattr_list_link(struct lwp *l, void *v, register_t *retval)
4164 {
4165 struct sys_extattr_list_link_args /* {
4166 syscallarg(const char *) path;
4167 syscallarg(int) attrnamespace;
4168 syscallarg(void *) data;
4169 syscallarg(size_t) nbytes;
4170 } */ *uap = v;
4171 struct proc *p = l->l_proc;
4172 struct nameidata nd;
4173 int error;
4174
4175 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
4176 error = namei(&nd);
4177 if (error)
4178 return (error);
4179
4180 error = extattr_list_vp(nd.ni_vp, SCARG(uap, attrnamespace),
4181 SCARG(uap, data), SCARG(uap, nbytes), p, retval);
4182
4183 vrele(nd.ni_vp);
4184 return (error);
4185 }
4186