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