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