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