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