vfs_syscalls.c revision 1.279.2.1 1 /* $NetBSD: vfs_syscalls.c,v 1.279.2.1 2007/01/03 13:59:32 tron 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.279.2.1 2007/01/03 13:59:32 tron 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 #if defined(COMPAT_30) && defined(NFSSERVER)
368 if (mp->mnt_flag & MNT_UPDATE && error != 0) {
369 int error2;
370
371 /* Update failed; let's try and see if it was an
372 * export request. */
373 error2 = nfs_update_exports_30(mp, SCARG(uap, path),
374 SCARG(uap, data), l);
375
376 /* Only update error code if the export request was
377 * understood but some problem occurred while
378 * processing it. */
379 if (error2 != EJUSTRETURN)
380 error = error2;
381 }
382 #endif
383 if (mp->mnt_iflag & IMNT_WANTRDWR)
384 mp->mnt_flag &= ~MNT_RDONLY;
385 if (error)
386 mp->mnt_flag = flag;
387 mp->mnt_flag &=~
388 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
389 mp->mnt_iflag &=~ IMNT_WANTRDWR;
390 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
391 if (mp->mnt_syncer == NULL)
392 error = vfs_allocate_syncvnode(mp);
393 } else {
394 if (mp->mnt_syncer != NULL)
395 vfs_deallocate_syncvnode(mp);
396 }
397 vfs_unbusy(mp);
398 VOP_UNLOCK(vp, 0);
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 if (mp->mnt_op->vfs_vptofh == NULL) {
1250 return EOPNOTSUPP;
1251 }
1252 fidp = NULL;
1253 if (*fh_size < FHANDLE_SIZE_MIN) {
1254 fidsize = 0;
1255 } else {
1256 fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1257 if (fhp != NULL) {
1258 memset(fhp, 0, *fh_size);
1259 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1260 fidp = &fhp->fh_fid;
1261 }
1262 }
1263 error = VFS_VPTOFH(vp, fidp, &fidsize);
1264 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1265 if (error == 0 && *fh_size < needfhsize) {
1266 error = E2BIG;
1267 }
1268 *fh_size = needfhsize;
1269 return error;
1270 }
1271
1272 int
1273 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1274 {
1275 struct mount *mp;
1276 fhandle_t *fhp;
1277 size_t fhsize;
1278 size_t fidsize;
1279 int error;
1280
1281 *fhpp = NULL;
1282 mp = vp->v_mount;
1283 if (mp->mnt_op->vfs_vptofh == NULL) {
1284 error = EOPNOTSUPP;
1285 goto out;
1286 }
1287 fidsize = 0;
1288 error = VFS_VPTOFH(vp, NULL, &fidsize);
1289 KASSERT(error != 0);
1290 if (error != E2BIG) {
1291 goto out;
1292 }
1293 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1294 fhp = kmem_zalloc(fhsize, KM_SLEEP);
1295 if (fhp == NULL) {
1296 error = ENOMEM;
1297 goto out;
1298 }
1299 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1300 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1301 if (error == 0) {
1302 KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1303 FHANDLE_FILEID(fhp)->fid_len == fidsize));
1304 *fhpp = fhp;
1305 } else {
1306 kmem_free(fhp, fhsize);
1307 }
1308 out:
1309 return error;
1310 }
1311
1312 void
1313 vfs_composefh_free(fhandle_t *fhp)
1314 {
1315
1316 vfs__fhfree(fhp);
1317 }
1318
1319 /*
1320 * vfs_fhtovp: lookup a vnode by a filehandle.
1321 */
1322
1323 int
1324 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1325 {
1326 struct mount *mp;
1327 int error;
1328
1329 *vpp = NULL;
1330 mp = vfs_getvfs(FHANDLE_FSID(fhp));
1331 if (mp == NULL) {
1332 error = ESTALE;
1333 goto out;
1334 }
1335 if (mp->mnt_op->vfs_fhtovp == NULL) {
1336 error = EOPNOTSUPP;
1337 goto out;
1338 }
1339 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1340 out:
1341 return error;
1342 }
1343
1344 /*
1345 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1346 * the needed size.
1347 */
1348
1349 int
1350 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1351 {
1352 fhandle_t *fhp;
1353 int error;
1354
1355 *fhpp = NULL;
1356 if (fhsize > FHANDLE_SIZE_MAX) {
1357 return EINVAL;
1358 }
1359 if (fhsize < FHANDLE_SIZE_MIN) {
1360 return EINVAL;
1361 }
1362 again:
1363 fhp = kmem_alloc(fhsize, KM_SLEEP);
1364 if (fhp == NULL) {
1365 return ENOMEM;
1366 }
1367 error = copyin(ufhp, fhp, fhsize);
1368 if (error == 0) {
1369 /* XXX this check shouldn't be here */
1370 if (FHANDLE_SIZE(fhp) == fhsize) {
1371 *fhpp = fhp;
1372 return 0;
1373 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1374 /*
1375 * a kludge for nfsv2 padded handles.
1376 */
1377 size_t sz;
1378
1379 sz = FHANDLE_SIZE(fhp);
1380 kmem_free(fhp, fhsize);
1381 fhsize = sz;
1382 goto again;
1383 } else {
1384 /*
1385 * userland told us wrong size.
1386 */
1387 error = EINVAL;
1388 }
1389 }
1390 kmem_free(fhp, fhsize);
1391 return error;
1392 }
1393
1394 void
1395 vfs_copyinfh_free(fhandle_t *fhp)
1396 {
1397
1398 vfs__fhfree(fhp);
1399 }
1400
1401 /*
1402 * Get file handle system call
1403 */
1404 int
1405 sys___getfh30(struct lwp *l, void *v, register_t *retval)
1406 {
1407 struct sys___getfh30_args /* {
1408 syscallarg(char *) fname;
1409 syscallarg(fhandle_t *) fhp;
1410 syscallarg(size_t *) fh_size;
1411 } */ *uap = v;
1412 struct vnode *vp;
1413 fhandle_t *fh;
1414 int error;
1415 struct nameidata nd;
1416 size_t sz;
1417 size_t usz;
1418
1419 /*
1420 * Must be super user
1421 */
1422 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1423 0, NULL, NULL, NULL);
1424 if (error)
1425 return (error);
1426 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1427 SCARG(uap, fname), l);
1428 error = namei(&nd);
1429 if (error)
1430 return (error);
1431 vp = nd.ni_vp;
1432 error = vfs_composefh_alloc(vp, &fh);
1433 vput(vp);
1434 if (error != 0) {
1435 goto out;
1436 }
1437 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1438 if (error != 0) {
1439 goto out;
1440 }
1441 sz = FHANDLE_SIZE(fh);
1442 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1443 if (error != 0) {
1444 goto out;
1445 }
1446 if (usz >= sz) {
1447 error = copyout(fh, SCARG(uap, fhp), sz);
1448 } else {
1449 error = E2BIG;
1450 }
1451 out:
1452 vfs_composefh_free(fh);
1453 return (error);
1454 }
1455
1456 /*
1457 * Open a file given a file handle.
1458 *
1459 * Check permissions, allocate an open file structure,
1460 * and call the device open routine if any.
1461 */
1462
1463 int
1464 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1465 register_t *retval)
1466 {
1467 struct filedesc *fdp = l->l_proc->p_fd;
1468 struct file *fp;
1469 struct vnode *vp = NULL;
1470 struct mount *mp;
1471 kauth_cred_t cred = l->l_cred;
1472 struct file *nfp;
1473 int type, indx, error=0;
1474 struct flock lf;
1475 struct vattr va;
1476 fhandle_t *fh;
1477 int flags;
1478
1479 /*
1480 * Must be super user
1481 */
1482 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1483 0, NULL, NULL, NULL)))
1484 return (error);
1485
1486 flags = FFLAGS(oflags);
1487 if ((flags & (FREAD | FWRITE)) == 0)
1488 return (EINVAL);
1489 if ((flags & O_CREAT))
1490 return (EINVAL);
1491 /* falloc() will use the file descriptor for us */
1492 if ((error = falloc(l, &nfp, &indx)) != 0)
1493 return (error);
1494 fp = nfp;
1495 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1496 if (error != 0) {
1497 goto bad;
1498 }
1499 error = vfs_fhtovp(fh, &vp);
1500 if (error != 0) {
1501 goto bad;
1502 }
1503
1504 /* Now do an effective vn_open */
1505
1506 if (vp->v_type == VSOCK) {
1507 error = EOPNOTSUPP;
1508 goto bad;
1509 }
1510 if (flags & FREAD) {
1511 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
1512 goto bad;
1513 }
1514 if (flags & (FWRITE | O_TRUNC)) {
1515 if (vp->v_type == VDIR) {
1516 error = EISDIR;
1517 goto bad;
1518 }
1519 if ((error = vn_writechk(vp)) != 0 ||
1520 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
1521 goto bad;
1522 }
1523 if (flags & O_TRUNC) {
1524 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
1525 goto bad;
1526 VOP_UNLOCK(vp, 0); /* XXX */
1527 VOP_LEASE(vp, l, cred, LEASE_WRITE);
1528 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1529 VATTR_NULL(&va);
1530 va.va_size = 0;
1531 error = VOP_SETATTR(vp, &va, cred, l);
1532 vn_finished_write(mp, 0);
1533 if (error)
1534 goto bad;
1535 }
1536 if ((error = VOP_OPEN(vp, flags, cred, l)) != 0)
1537 goto bad;
1538 if (vp->v_type == VREG &&
1539 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
1540 error = EIO;
1541 goto bad;
1542 }
1543 if (flags & FWRITE)
1544 vp->v_writecount++;
1545
1546 /* done with modified vn_open, now finish what sys_open does. */
1547
1548 fp->f_flag = flags & FMASK;
1549 fp->f_type = DTYPE_VNODE;
1550 fp->f_ops = &vnops;
1551 fp->f_data = vp;
1552 if (flags & (O_EXLOCK | O_SHLOCK)) {
1553 lf.l_whence = SEEK_SET;
1554 lf.l_start = 0;
1555 lf.l_len = 0;
1556 if (flags & O_EXLOCK)
1557 lf.l_type = F_WRLCK;
1558 else
1559 lf.l_type = F_RDLCK;
1560 type = F_FLOCK;
1561 if ((flags & FNONBLOCK) == 0)
1562 type |= F_WAIT;
1563 VOP_UNLOCK(vp, 0);
1564 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1565 if (error) {
1566 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1567 FILE_UNUSE(fp, l);
1568 ffree(fp);
1569 fdremove(fdp, indx);
1570 return (error);
1571 }
1572 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1573 fp->f_flag |= FHASLOCK;
1574 }
1575 VOP_UNLOCK(vp, 0);
1576 *retval = indx;
1577 FILE_SET_MATURE(fp);
1578 FILE_UNUSE(fp, l);
1579 vfs_copyinfh_free(fh);
1580 return (0);
1581
1582 bad:
1583 FILE_UNUSE(fp, l);
1584 ffree(fp);
1585 fdremove(fdp, indx);
1586 if (vp != NULL)
1587 vput(vp);
1588 vfs_copyinfh_free(fh);
1589 return (error);
1590 }
1591
1592 int
1593 sys___fhopen40(struct lwp *l, void *v, register_t *retval)
1594 {
1595 struct sys___fhopen40_args /* {
1596 syscallarg(const void *) fhp;
1597 syscallarg(size_t) fh_size;
1598 syscallarg(int) flags;
1599 } */ *uap = v;
1600
1601 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
1602 SCARG(uap, flags), retval);
1603 }
1604
1605 int
1606 dofhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sbp,
1607 register_t *retval)
1608 {
1609 struct stat sb;
1610 int error;
1611 fhandle_t *fh;
1612 struct vnode *vp;
1613
1614 /*
1615 * Must be super user
1616 */
1617 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1618 0, NULL, NULL, NULL)))
1619 return (error);
1620
1621 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1622 if (error != 0) {
1623 goto bad;
1624 }
1625 error = vfs_fhtovp(fh, &vp);
1626 if (error != 0) {
1627 goto bad;
1628 }
1629 error = vn_stat(vp, &sb, l);
1630 vput(vp);
1631 if (error) {
1632 goto bad;
1633 }
1634 error = copyout(&sb, sbp, sizeof(sb));
1635 bad:
1636 vfs_copyinfh_free(fh);
1637 return error;
1638 }
1639
1640
1641 /* ARGSUSED */
1642 int
1643 sys___fhstat40(struct lwp *l, void *v, register_t *retval)
1644 {
1645 struct sys___fhstat40_args /* {
1646 syscallarg(const void *) fhp;
1647 syscallarg(size_t) fh_size;
1648 syscallarg(struct stat *) sb;
1649 } */ *uap = v;
1650
1651 return dofhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), SCARG(uap, sb),
1652 retval);
1653 }
1654
1655 int
1656 dofhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *buf,
1657 int flags, register_t *retval)
1658 {
1659 struct statvfs *sb = NULL;
1660 fhandle_t *fh;
1661 struct mount *mp;
1662 struct vnode *vp;
1663 int error;
1664
1665 /*
1666 * Must be super user
1667 */
1668 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1669 0, NULL, NULL, NULL)))
1670 return error;
1671
1672 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1673 if (error != 0) {
1674 goto out;
1675 }
1676 error = vfs_fhtovp(fh, &vp);
1677 if (error != 0) {
1678 goto out;
1679 }
1680 mp = vp->v_mount;
1681 sb = STATVFSBUF_GET();
1682 if ((error = dostatvfs(mp, sb, l, flags, 1)) != 0) {
1683 vput(vp);
1684 goto out;
1685 }
1686 vput(vp);
1687 error = copyout(sb, buf, sizeof(*sb));
1688 out:
1689 if (sb != NULL) {
1690 STATVFSBUF_PUT(sb);
1691 }
1692 vfs_copyinfh_free(fh);
1693 return error;
1694 }
1695
1696 /* ARGSUSED */
1697 int
1698 sys___fhstatvfs140(struct lwp *l, void *v, register_t *retval)
1699 {
1700 struct sys___fhstatvfs140_args /* {
1701 syscallarg(const void *) fhp;
1702 syscallarg(size_t) fh_size;
1703 syscallarg(struct statvfs *) buf;
1704 syscallarg(int) flags;
1705 } */ *uap = v;
1706
1707 return dofhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size),
1708 SCARG(uap, buf), SCARG(uap, flags), retval);
1709 }
1710
1711 /*
1712 * Create a special file.
1713 */
1714 /* ARGSUSED */
1715 int
1716 sys_mknod(struct lwp *l, void *v, register_t *retval)
1717 {
1718 struct sys_mknod_args /* {
1719 syscallarg(const char *) path;
1720 syscallarg(int) mode;
1721 syscallarg(int) dev;
1722 } */ *uap = v;
1723 struct proc *p = l->l_proc;
1724 struct vnode *vp;
1725 struct mount *mp;
1726 struct vattr vattr;
1727 int error;
1728 int whiteout = 0;
1729 struct nameidata nd;
1730
1731 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
1732 0, NULL, NULL, NULL)) != 0)
1733 return (error);
1734 restart:
1735 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), l);
1736 if ((error = namei(&nd)) != 0)
1737 return (error);
1738 vp = nd.ni_vp;
1739 if (vp != NULL)
1740 error = EEXIST;
1741 else {
1742 VATTR_NULL(&vattr);
1743 vattr.va_mode =
1744 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1745 vattr.va_rdev = SCARG(uap, dev);
1746 whiteout = 0;
1747
1748 switch (SCARG(uap, mode) & S_IFMT) {
1749 case S_IFMT: /* used by badsect to flag bad sectors */
1750 vattr.va_type = VBAD;
1751 break;
1752 case S_IFCHR:
1753 vattr.va_type = VCHR;
1754 break;
1755 case S_IFBLK:
1756 vattr.va_type = VBLK;
1757 break;
1758 case S_IFWHT:
1759 whiteout = 1;
1760 break;
1761 default:
1762 error = EINVAL;
1763 break;
1764 }
1765 }
1766 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1767 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1768 if (nd.ni_dvp == vp)
1769 vrele(nd.ni_dvp);
1770 else
1771 vput(nd.ni_dvp);
1772 if (vp)
1773 vrele(vp);
1774 if ((error = vn_start_write(NULL, &mp,
1775 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1776 return (error);
1777 goto restart;
1778 }
1779 if (!error) {
1780 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1781 if (whiteout) {
1782 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1783 if (error)
1784 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1785 vput(nd.ni_dvp);
1786 } else {
1787 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1788 &nd.ni_cnd, &vattr);
1789 if (error == 0)
1790 vput(nd.ni_vp);
1791 }
1792 } else {
1793 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1794 if (nd.ni_dvp == vp)
1795 vrele(nd.ni_dvp);
1796 else
1797 vput(nd.ni_dvp);
1798 if (vp)
1799 vrele(vp);
1800 }
1801 vn_finished_write(mp, 0);
1802 return (error);
1803 }
1804
1805 /*
1806 * Create a named pipe.
1807 */
1808 /* ARGSUSED */
1809 int
1810 sys_mkfifo(struct lwp *l, void *v, register_t *retval)
1811 {
1812 struct sys_mkfifo_args /* {
1813 syscallarg(const char *) path;
1814 syscallarg(int) mode;
1815 } */ *uap = v;
1816 struct proc *p = l->l_proc;
1817 struct mount *mp;
1818 struct vattr vattr;
1819 int error;
1820 struct nameidata nd;
1821
1822 restart:
1823 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), l);
1824 if ((error = namei(&nd)) != 0)
1825 return (error);
1826 if (nd.ni_vp != NULL) {
1827 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1828 if (nd.ni_dvp == nd.ni_vp)
1829 vrele(nd.ni_dvp);
1830 else
1831 vput(nd.ni_dvp);
1832 vrele(nd.ni_vp);
1833 return (EEXIST);
1834 }
1835 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1836 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1837 if (nd.ni_dvp == nd.ni_vp)
1838 vrele(nd.ni_dvp);
1839 else
1840 vput(nd.ni_dvp);
1841 if (nd.ni_vp)
1842 vrele(nd.ni_vp);
1843 if ((error = vn_start_write(NULL, &mp,
1844 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1845 return (error);
1846 goto restart;
1847 }
1848 VATTR_NULL(&vattr);
1849 vattr.va_type = VFIFO;
1850 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1851 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1852 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1853 if (error == 0)
1854 vput(nd.ni_vp);
1855 vn_finished_write(mp, 0);
1856 return (error);
1857 }
1858
1859 /*
1860 * Make a hard file link.
1861 */
1862 /* ARGSUSED */
1863 int
1864 sys_link(struct lwp *l, void *v, register_t *retval)
1865 {
1866 struct sys_link_args /* {
1867 syscallarg(const char *) path;
1868 syscallarg(const char *) link;
1869 } */ *uap = v;
1870 struct vnode *vp;
1871 struct mount *mp;
1872 struct nameidata nd;
1873 int error;
1874
1875 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
1876 if ((error = namei(&nd)) != 0)
1877 return (error);
1878 vp = nd.ni_vp;
1879 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
1880 vrele(vp);
1881 return (error);
1882 }
1883 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), l);
1884 if ((error = namei(&nd)) != 0)
1885 goto out;
1886 if (nd.ni_vp) {
1887 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1888 if (nd.ni_dvp == nd.ni_vp)
1889 vrele(nd.ni_dvp);
1890 else
1891 vput(nd.ni_dvp);
1892 vrele(nd.ni_vp);
1893 error = EEXIST;
1894 goto out;
1895 }
1896 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1897 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
1898 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1899 out:
1900 vrele(vp);
1901 vn_finished_write(mp, 0);
1902 return (error);
1903 }
1904
1905 /*
1906 * Make a symbolic link.
1907 */
1908 /* ARGSUSED */
1909 int
1910 sys_symlink(struct lwp *l, void *v, register_t *retval)
1911 {
1912 struct sys_symlink_args /* {
1913 syscallarg(const char *) path;
1914 syscallarg(const char *) link;
1915 } */ *uap = v;
1916 struct proc *p = l->l_proc;
1917 struct mount *mp;
1918 struct vattr vattr;
1919 char *path;
1920 int error;
1921 struct nameidata nd;
1922
1923 path = PNBUF_GET();
1924 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1925 if (error)
1926 goto out;
1927 restart:
1928 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), l);
1929 if ((error = namei(&nd)) != 0)
1930 goto out;
1931 if (nd.ni_vp) {
1932 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1933 if (nd.ni_dvp == nd.ni_vp)
1934 vrele(nd.ni_dvp);
1935 else
1936 vput(nd.ni_dvp);
1937 vrele(nd.ni_vp);
1938 error = EEXIST;
1939 goto out;
1940 }
1941 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1942 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1943 if (nd.ni_dvp == nd.ni_vp)
1944 vrele(nd.ni_dvp);
1945 else
1946 vput(nd.ni_dvp);
1947 if ((error = vn_start_write(NULL, &mp,
1948 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
1949 return (error);
1950 goto restart;
1951 }
1952 VATTR_NULL(&vattr);
1953 vattr.va_type = VLNK;
1954 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1955 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1956 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1957 if (error == 0)
1958 vput(nd.ni_vp);
1959 vn_finished_write(mp, 0);
1960 out:
1961 PNBUF_PUT(path);
1962 return (error);
1963 }
1964
1965 /*
1966 * Delete a whiteout from the filesystem.
1967 */
1968 /* ARGSUSED */
1969 int
1970 sys_undelete(struct lwp *l, void *v, register_t *retval)
1971 {
1972 struct sys_undelete_args /* {
1973 syscallarg(const char *) path;
1974 } */ *uap = v;
1975 int error;
1976 struct mount *mp;
1977 struct nameidata nd;
1978
1979 restart:
1980 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1981 SCARG(uap, path), l);
1982 error = namei(&nd);
1983 if (error)
1984 return (error);
1985
1986 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1987 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1988 if (nd.ni_dvp == nd.ni_vp)
1989 vrele(nd.ni_dvp);
1990 else
1991 vput(nd.ni_dvp);
1992 if (nd.ni_vp)
1993 vrele(nd.ni_vp);
1994 return (EEXIST);
1995 }
1996 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1997 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1998 if (nd.ni_dvp == nd.ni_vp)
1999 vrele(nd.ni_dvp);
2000 else
2001 vput(nd.ni_dvp);
2002 if ((error = vn_start_write(NULL, &mp,
2003 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
2004 return (error);
2005 goto restart;
2006 }
2007 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
2008 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
2009 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2010 vput(nd.ni_dvp);
2011 vn_finished_write(mp, 0);
2012 return (error);
2013 }
2014
2015 /*
2016 * Delete a name from the filesystem.
2017 */
2018 /* ARGSUSED */
2019 int
2020 sys_unlink(struct lwp *l, void *v, register_t *retval)
2021 {
2022 struct sys_unlink_args /* {
2023 syscallarg(const char *) path;
2024 } */ *uap = v;
2025 struct mount *mp;
2026 struct vnode *vp;
2027 int error;
2028 struct nameidata nd;
2029 #if NVERIEXEC > 0
2030 pathname_t pathbuf;
2031 #endif /* NVERIEXEC > 0 */
2032
2033 restart:
2034 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2035 SCARG(uap, path), l);
2036 if ((error = namei(&nd)) != 0)
2037 return (error);
2038 vp = nd.ni_vp;
2039
2040 /*
2041 * The root of a mounted filesystem cannot be deleted.
2042 */
2043 if (vp->v_flag & VROOT) {
2044 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2045 if (nd.ni_dvp == vp)
2046 vrele(nd.ni_dvp);
2047 else
2048 vput(nd.ni_dvp);
2049 vput(vp);
2050 error = EBUSY;
2051 goto out;
2052 }
2053
2054 #if NVERIEXEC > 0
2055 error = pathname_get(nd.ni_dirp, nd.ni_segflg, &pathbuf);
2056
2057 /* Handle remove requests for veriexec entries. */
2058 if (!error) {
2059 error = veriexec_removechk(vp, pathname_path(pathbuf), l);
2060 pathname_put(pathbuf);
2061 }
2062
2063 if (error) {
2064 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2065 if (nd.ni_dvp == vp)
2066 vrele(nd.ni_dvp);
2067 else
2068 vput(nd.ni_dvp);
2069 vput(vp);
2070 goto out;
2071 }
2072 #endif /* NVERIEXEC > 0 */
2073
2074 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
2075 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2076 if (nd.ni_dvp == vp)
2077 vrele(nd.ni_dvp);
2078 else
2079 vput(nd.ni_dvp);
2080 vput(vp);
2081 if ((error = vn_start_write(NULL, &mp,
2082 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
2083 return (error);
2084 goto restart;
2085 }
2086 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
2087 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2088 #ifdef FILEASSOC
2089 (void)fileassoc_file_delete(vp);
2090 #endif /* FILEASSOC */
2091 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2092 vn_finished_write(mp, 0);
2093 out:
2094 return (error);
2095 }
2096
2097 /*
2098 * Reposition read/write file offset.
2099 */
2100 int
2101 sys_lseek(struct lwp *l, void *v, register_t *retval)
2102 {
2103 struct sys_lseek_args /* {
2104 syscallarg(int) fd;
2105 syscallarg(int) pad;
2106 syscallarg(off_t) offset;
2107 syscallarg(int) whence;
2108 } */ *uap = v;
2109 struct proc *p = l->l_proc;
2110 kauth_cred_t cred = l->l_cred;
2111 struct filedesc *fdp = p->p_fd;
2112 struct file *fp;
2113 struct vnode *vp;
2114 struct vattr vattr;
2115 off_t newoff;
2116 int error;
2117
2118 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
2119 return (EBADF);
2120
2121 FILE_USE(fp);
2122
2123 vp = (struct vnode *)fp->f_data;
2124 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2125 error = ESPIPE;
2126 goto out;
2127 }
2128
2129 switch (SCARG(uap, whence)) {
2130 case SEEK_CUR:
2131 newoff = fp->f_offset + SCARG(uap, offset);
2132 break;
2133 case SEEK_END:
2134 error = VOP_GETATTR(vp, &vattr, cred, l);
2135 if (error)
2136 goto out;
2137 newoff = SCARG(uap, offset) + vattr.va_size;
2138 break;
2139 case SEEK_SET:
2140 newoff = SCARG(uap, offset);
2141 break;
2142 default:
2143 error = EINVAL;
2144 goto out;
2145 }
2146 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
2147 goto out;
2148
2149 *(off_t *)retval = fp->f_offset = newoff;
2150 out:
2151 FILE_UNUSE(fp, l);
2152 return (error);
2153 }
2154
2155 /*
2156 * Positional read system call.
2157 */
2158 int
2159 sys_pread(struct lwp *l, void *v, register_t *retval)
2160 {
2161 struct sys_pread_args /* {
2162 syscallarg(int) fd;
2163 syscallarg(void *) buf;
2164 syscallarg(size_t) nbyte;
2165 syscallarg(off_t) offset;
2166 } */ *uap = v;
2167 struct proc *p = l->l_proc;
2168 struct filedesc *fdp = p->p_fd;
2169 struct file *fp;
2170 struct vnode *vp;
2171 off_t offset;
2172 int error, fd = SCARG(uap, fd);
2173
2174 if ((fp = fd_getfile(fdp, fd)) == NULL)
2175 return (EBADF);
2176
2177 if ((fp->f_flag & FREAD) == 0) {
2178 simple_unlock(&fp->f_slock);
2179 return (EBADF);
2180 }
2181
2182 FILE_USE(fp);
2183
2184 vp = (struct vnode *)fp->f_data;
2185 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2186 error = ESPIPE;
2187 goto out;
2188 }
2189
2190 offset = SCARG(uap, offset);
2191
2192 /*
2193 * XXX This works because no file systems actually
2194 * XXX take any action on the seek operation.
2195 */
2196 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2197 goto out;
2198
2199 /* dofileread() will unuse the descriptor for us */
2200 return (dofileread(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2201 &offset, 0, retval));
2202
2203 out:
2204 FILE_UNUSE(fp, l);
2205 return (error);
2206 }
2207
2208 /*
2209 * Positional scatter read system call.
2210 */
2211 int
2212 sys_preadv(struct lwp *l, void *v, register_t *retval)
2213 {
2214 struct sys_preadv_args /* {
2215 syscallarg(int) fd;
2216 syscallarg(const struct iovec *) iovp;
2217 syscallarg(int) iovcnt;
2218 syscallarg(off_t) offset;
2219 } */ *uap = v;
2220 struct proc *p = l->l_proc;
2221 struct filedesc *fdp = p->p_fd;
2222 struct file *fp;
2223 struct vnode *vp;
2224 off_t offset;
2225 int error, fd = SCARG(uap, fd);
2226
2227 if ((fp = fd_getfile(fdp, fd)) == NULL)
2228 return (EBADF);
2229
2230 if ((fp->f_flag & FREAD) == 0) {
2231 simple_unlock(&fp->f_slock);
2232 return (EBADF);
2233 }
2234
2235 FILE_USE(fp);
2236
2237 vp = (struct vnode *)fp->f_data;
2238 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2239 error = ESPIPE;
2240 goto out;
2241 }
2242
2243 offset = SCARG(uap, offset);
2244
2245 /*
2246 * XXX This works because no file systems actually
2247 * XXX take any action on the seek operation.
2248 */
2249 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2250 goto out;
2251
2252 /* dofilereadv() will unuse the descriptor for us */
2253 return (dofilereadv(l, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
2254 &offset, 0, retval));
2255
2256 out:
2257 FILE_UNUSE(fp, l);
2258 return (error);
2259 }
2260
2261 /*
2262 * Positional write system call.
2263 */
2264 int
2265 sys_pwrite(struct lwp *l, void *v, register_t *retval)
2266 {
2267 struct sys_pwrite_args /* {
2268 syscallarg(int) fd;
2269 syscallarg(const void *) buf;
2270 syscallarg(size_t) nbyte;
2271 syscallarg(off_t) offset;
2272 } */ *uap = v;
2273 struct proc *p = l->l_proc;
2274 struct filedesc *fdp = p->p_fd;
2275 struct file *fp;
2276 struct vnode *vp;
2277 off_t offset;
2278 int error, fd = SCARG(uap, fd);
2279
2280 if ((fp = fd_getfile(fdp, fd)) == NULL)
2281 return (EBADF);
2282
2283 if ((fp->f_flag & FWRITE) == 0) {
2284 simple_unlock(&fp->f_slock);
2285 return (EBADF);
2286 }
2287
2288 FILE_USE(fp);
2289
2290 vp = (struct vnode *)fp->f_data;
2291 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2292 error = ESPIPE;
2293 goto out;
2294 }
2295
2296 offset = SCARG(uap, offset);
2297
2298 /*
2299 * XXX This works because no file systems actually
2300 * XXX take any action on the seek operation.
2301 */
2302 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2303 goto out;
2304
2305 /* dofilewrite() will unuse the descriptor for us */
2306 return (dofilewrite(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2307 &offset, 0, retval));
2308
2309 out:
2310 FILE_UNUSE(fp, l);
2311 return (error);
2312 }
2313
2314 /*
2315 * Positional gather write system call.
2316 */
2317 int
2318 sys_pwritev(struct lwp *l, void *v, register_t *retval)
2319 {
2320 struct sys_pwritev_args /* {
2321 syscallarg(int) fd;
2322 syscallarg(const struct iovec *) iovp;
2323 syscallarg(int) iovcnt;
2324 syscallarg(off_t) offset;
2325 } */ *uap = v;
2326 struct proc *p = l->l_proc;
2327 struct filedesc *fdp = p->p_fd;
2328 struct file *fp;
2329 struct vnode *vp;
2330 off_t offset;
2331 int error, fd = SCARG(uap, fd);
2332
2333 if ((fp = fd_getfile(fdp, fd)) == NULL)
2334 return (EBADF);
2335
2336 if ((fp->f_flag & FWRITE) == 0) {
2337 simple_unlock(&fp->f_slock);
2338 return (EBADF);
2339 }
2340
2341 FILE_USE(fp);
2342
2343 vp = (struct vnode *)fp->f_data;
2344 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2345 error = ESPIPE;
2346 goto out;
2347 }
2348
2349 offset = SCARG(uap, offset);
2350
2351 /*
2352 * XXX This works because no file systems actually
2353 * XXX take any action on the seek operation.
2354 */
2355 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2356 goto out;
2357
2358 /* dofilewritev() will unuse the descriptor for us */
2359 return (dofilewritev(l, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
2360 &offset, 0, retval));
2361
2362 out:
2363 FILE_UNUSE(fp, l);
2364 return (error);
2365 }
2366
2367 /*
2368 * Check access permissions.
2369 */
2370 int
2371 sys_access(struct lwp *l, void *v, register_t *retval)
2372 {
2373 struct sys_access_args /* {
2374 syscallarg(const char *) path;
2375 syscallarg(int) flags;
2376 } */ *uap = v;
2377 kauth_cred_t cred;
2378 struct vnode *vp;
2379 int error, flags;
2380 struct nameidata nd;
2381
2382 cred = kauth_cred_dup(l->l_cred);
2383 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2384 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2385 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2386 SCARG(uap, path), l);
2387 /* Override default credentials */
2388 nd.ni_cnd.cn_cred = cred;
2389 if ((error = namei(&nd)) != 0)
2390 goto out;
2391 vp = nd.ni_vp;
2392
2393 /* Flags == 0 means only check for existence. */
2394 if (SCARG(uap, flags)) {
2395 flags = 0;
2396 if (SCARG(uap, flags) & R_OK)
2397 flags |= VREAD;
2398 if (SCARG(uap, flags) & W_OK)
2399 flags |= VWRITE;
2400 if (SCARG(uap, flags) & X_OK)
2401 flags |= VEXEC;
2402
2403 error = VOP_ACCESS(vp, flags, cred, l);
2404 if (!error && (flags & VWRITE))
2405 error = vn_writechk(vp);
2406 }
2407 vput(vp);
2408 out:
2409 kauth_cred_free(cred);
2410 return (error);
2411 }
2412
2413 /*
2414 * Get file status; this version follows links.
2415 */
2416 /* ARGSUSED */
2417 int
2418 sys___stat30(struct lwp *l, void *v, register_t *retval)
2419 {
2420 struct sys___stat30_args /* {
2421 syscallarg(const char *) path;
2422 syscallarg(struct stat *) ub;
2423 } */ *uap = v;
2424 struct stat sb;
2425 int error;
2426 struct nameidata nd;
2427
2428 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2429 SCARG(uap, path), l);
2430 if ((error = namei(&nd)) != 0)
2431 return (error);
2432 error = vn_stat(nd.ni_vp, &sb, l);
2433 vput(nd.ni_vp);
2434 if (error)
2435 return (error);
2436 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
2437 return (error);
2438 }
2439
2440 /*
2441 * Get file status; this version does not follow links.
2442 */
2443 /* ARGSUSED */
2444 int
2445 sys___lstat30(struct lwp *l, void *v, register_t *retval)
2446 {
2447 struct sys___lstat30_args /* {
2448 syscallarg(const char *) path;
2449 syscallarg(struct stat *) ub;
2450 } */ *uap = v;
2451 struct stat sb;
2452 int error;
2453 struct nameidata nd;
2454
2455 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
2456 SCARG(uap, path), l);
2457 if ((error = namei(&nd)) != 0)
2458 return (error);
2459 error = vn_stat(nd.ni_vp, &sb, l);
2460 vput(nd.ni_vp);
2461 if (error)
2462 return (error);
2463 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
2464 return (error);
2465 }
2466
2467 /*
2468 * Get configurable pathname variables.
2469 */
2470 /* ARGSUSED */
2471 int
2472 sys_pathconf(struct lwp *l, void *v, register_t *retval)
2473 {
2474 struct sys_pathconf_args /* {
2475 syscallarg(const char *) path;
2476 syscallarg(int) name;
2477 } */ *uap = v;
2478 int error;
2479 struct nameidata nd;
2480
2481 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
2482 SCARG(uap, path), l);
2483 if ((error = namei(&nd)) != 0)
2484 return (error);
2485 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2486 vput(nd.ni_vp);
2487 return (error);
2488 }
2489
2490 /*
2491 * Return target name of a symbolic link.
2492 */
2493 /* ARGSUSED */
2494 int
2495 sys_readlink(struct lwp *l, void *v, register_t *retval)
2496 {
2497 struct sys_readlink_args /* {
2498 syscallarg(const char *) path;
2499 syscallarg(char *) buf;
2500 syscallarg(size_t) count;
2501 } */ *uap = v;
2502 struct vnode *vp;
2503 struct iovec aiov;
2504 struct uio auio;
2505 int error;
2506 struct nameidata nd;
2507
2508 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
2509 SCARG(uap, path), l);
2510 if ((error = namei(&nd)) != 0)
2511 return (error);
2512 vp = nd.ni_vp;
2513 if (vp->v_type != VLNK)
2514 error = EINVAL;
2515 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2516 (error = VOP_ACCESS(vp, VREAD, l->l_cred, l)) == 0) {
2517 aiov.iov_base = SCARG(uap, buf);
2518 aiov.iov_len = SCARG(uap, count);
2519 auio.uio_iov = &aiov;
2520 auio.uio_iovcnt = 1;
2521 auio.uio_offset = 0;
2522 auio.uio_rw = UIO_READ;
2523 KASSERT(l == curlwp);
2524 auio.uio_vmspace = l->l_proc->p_vmspace;
2525 auio.uio_resid = SCARG(uap, count);
2526 error = VOP_READLINK(vp, &auio, l->l_cred);
2527 }
2528 vput(vp);
2529 *retval = SCARG(uap, count) - auio.uio_resid;
2530 return (error);
2531 }
2532
2533 /*
2534 * Change flags of a file given a path name.
2535 */
2536 /* ARGSUSED */
2537 int
2538 sys_chflags(struct lwp *l, void *v, register_t *retval)
2539 {
2540 struct sys_chflags_args /* {
2541 syscallarg(const char *) path;
2542 syscallarg(u_long) flags;
2543 } */ *uap = v;
2544 struct vnode *vp;
2545 int error;
2546 struct nameidata nd;
2547
2548 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2549 if ((error = namei(&nd)) != 0)
2550 return (error);
2551 vp = nd.ni_vp;
2552 error = change_flags(vp, SCARG(uap, flags), l);
2553 vput(vp);
2554 return (error);
2555 }
2556
2557 /*
2558 * Change flags of a file given a file descriptor.
2559 */
2560 /* ARGSUSED */
2561 int
2562 sys_fchflags(struct lwp *l, void *v, register_t *retval)
2563 {
2564 struct sys_fchflags_args /* {
2565 syscallarg(int) fd;
2566 syscallarg(u_long) flags;
2567 } */ *uap = v;
2568 struct proc *p = l->l_proc;
2569 struct vnode *vp;
2570 struct file *fp;
2571 int error;
2572
2573 /* getvnode() will use the descriptor for us */
2574 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2575 return (error);
2576 vp = (struct vnode *)fp->f_data;
2577 error = change_flags(vp, SCARG(uap, flags), l);
2578 VOP_UNLOCK(vp, 0);
2579 FILE_UNUSE(fp, l);
2580 return (error);
2581 }
2582
2583 /*
2584 * Change flags of a file given a path name; this version does
2585 * not follow links.
2586 */
2587 int
2588 sys_lchflags(struct lwp *l, void *v, register_t *retval)
2589 {
2590 struct sys_lchflags_args /* {
2591 syscallarg(const char *) path;
2592 syscallarg(u_long) flags;
2593 } */ *uap = v;
2594 struct vnode *vp;
2595 int error;
2596 struct nameidata nd;
2597
2598 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2599 if ((error = namei(&nd)) != 0)
2600 return (error);
2601 vp = nd.ni_vp;
2602 error = change_flags(vp, SCARG(uap, flags), l);
2603 vput(vp);
2604 return (error);
2605 }
2606
2607 /*
2608 * Common routine to change flags of a file.
2609 */
2610 int
2611 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
2612 {
2613 struct mount *mp;
2614 struct vattr vattr;
2615 int error;
2616
2617 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2618 return (error);
2619 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2620 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2621 /*
2622 * Non-superusers cannot change the flags on devices, even if they
2623 * own them.
2624 */
2625 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2626 &l->l_acflag) != 0) {
2627 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
2628 goto out;
2629 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2630 error = EINVAL;
2631 goto out;
2632 }
2633 }
2634 VATTR_NULL(&vattr);
2635 vattr.va_flags = flags;
2636 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2637 out:
2638 vn_finished_write(mp, 0);
2639 return (error);
2640 }
2641
2642 /*
2643 * Change mode of a file given path name; this version follows links.
2644 */
2645 /* ARGSUSED */
2646 int
2647 sys_chmod(struct lwp *l, void *v, register_t *retval)
2648 {
2649 struct sys_chmod_args /* {
2650 syscallarg(const char *) path;
2651 syscallarg(int) mode;
2652 } */ *uap = v;
2653 int error;
2654 struct nameidata nd;
2655
2656 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2657 if ((error = namei(&nd)) != 0)
2658 return (error);
2659
2660 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2661
2662 vrele(nd.ni_vp);
2663 return (error);
2664 }
2665
2666 /*
2667 * Change mode of a file given a file descriptor.
2668 */
2669 /* ARGSUSED */
2670 int
2671 sys_fchmod(struct lwp *l, void *v, register_t *retval)
2672 {
2673 struct sys_fchmod_args /* {
2674 syscallarg(int) fd;
2675 syscallarg(int) mode;
2676 } */ *uap = v;
2677 struct proc *p = l->l_proc;
2678 struct file *fp;
2679 int error;
2680
2681 /* getvnode() will use the descriptor for us */
2682 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2683 return (error);
2684
2685 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), l);
2686 FILE_UNUSE(fp, l);
2687 return (error);
2688 }
2689
2690 /*
2691 * Change mode of a file given path name; this version does not follow links.
2692 */
2693 /* ARGSUSED */
2694 int
2695 sys_lchmod(struct lwp *l, void *v, register_t *retval)
2696 {
2697 struct sys_lchmod_args /* {
2698 syscallarg(const char *) path;
2699 syscallarg(int) mode;
2700 } */ *uap = v;
2701 int error;
2702 struct nameidata nd;
2703
2704 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2705 if ((error = namei(&nd)) != 0)
2706 return (error);
2707
2708 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2709
2710 vrele(nd.ni_vp);
2711 return (error);
2712 }
2713
2714 /*
2715 * Common routine to set mode given a vnode.
2716 */
2717 static int
2718 change_mode(struct vnode *vp, int mode, struct lwp *l)
2719 {
2720 struct mount *mp;
2721 struct vattr vattr;
2722 int error;
2723
2724 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2725 return (error);
2726 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2727 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2728 VATTR_NULL(&vattr);
2729 vattr.va_mode = mode & ALLPERMS;
2730 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2731 VOP_UNLOCK(vp, 0);
2732 vn_finished_write(mp, 0);
2733 return (error);
2734 }
2735
2736 /*
2737 * Set ownership given a path name; this version follows links.
2738 */
2739 /* ARGSUSED */
2740 int
2741 sys_chown(struct lwp *l, void *v, register_t *retval)
2742 {
2743 struct sys_chown_args /* {
2744 syscallarg(const char *) path;
2745 syscallarg(uid_t) uid;
2746 syscallarg(gid_t) gid;
2747 } */ *uap = v;
2748 int error;
2749 struct nameidata nd;
2750
2751 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2752 if ((error = namei(&nd)) != 0)
2753 return (error);
2754
2755 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2756
2757 vrele(nd.ni_vp);
2758 return (error);
2759 }
2760
2761 /*
2762 * Set ownership given a path name; this version follows links.
2763 * Provides POSIX semantics.
2764 */
2765 /* ARGSUSED */
2766 int
2767 sys___posix_chown(struct lwp *l, void *v, register_t *retval)
2768 {
2769 struct sys_chown_args /* {
2770 syscallarg(const char *) path;
2771 syscallarg(uid_t) uid;
2772 syscallarg(gid_t) gid;
2773 } */ *uap = v;
2774 int error;
2775 struct nameidata nd;
2776
2777 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2778 if ((error = namei(&nd)) != 0)
2779 return (error);
2780
2781 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2782
2783 vrele(nd.ni_vp);
2784 return (error);
2785 }
2786
2787 /*
2788 * Set ownership given a file descriptor.
2789 */
2790 /* ARGSUSED */
2791 int
2792 sys_fchown(struct lwp *l, void *v, register_t *retval)
2793 {
2794 struct sys_fchown_args /* {
2795 syscallarg(int) fd;
2796 syscallarg(uid_t) uid;
2797 syscallarg(gid_t) gid;
2798 } */ *uap = v;
2799 struct proc *p = l->l_proc;
2800 int error;
2801 struct file *fp;
2802
2803 /* getvnode() will use the descriptor for us */
2804 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2805 return (error);
2806
2807 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2808 SCARG(uap, gid), l, 0);
2809 FILE_UNUSE(fp, l);
2810 return (error);
2811 }
2812
2813 /*
2814 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2815 */
2816 /* ARGSUSED */
2817 int
2818 sys___posix_fchown(struct lwp *l, void *v, register_t *retval)
2819 {
2820 struct sys_fchown_args /* {
2821 syscallarg(int) fd;
2822 syscallarg(uid_t) uid;
2823 syscallarg(gid_t) gid;
2824 } */ *uap = v;
2825 struct proc *p = l->l_proc;
2826 int error;
2827 struct file *fp;
2828
2829 /* getvnode() will use the descriptor for us */
2830 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2831 return (error);
2832
2833 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2834 SCARG(uap, gid), l, 1);
2835 FILE_UNUSE(fp, l);
2836 return (error);
2837 }
2838
2839 /*
2840 * Set ownership given a path name; this version does not follow links.
2841 */
2842 /* ARGSUSED */
2843 int
2844 sys_lchown(struct lwp *l, void *v, register_t *retval)
2845 {
2846 struct sys_lchown_args /* {
2847 syscallarg(const char *) path;
2848 syscallarg(uid_t) uid;
2849 syscallarg(gid_t) gid;
2850 } */ *uap = v;
2851 int error;
2852 struct nameidata nd;
2853
2854 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2855 if ((error = namei(&nd)) != 0)
2856 return (error);
2857
2858 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2859
2860 vrele(nd.ni_vp);
2861 return (error);
2862 }
2863
2864 /*
2865 * Set ownership given a path name; this version does not follow links.
2866 * Provides POSIX/XPG semantics.
2867 */
2868 /* ARGSUSED */
2869 int
2870 sys___posix_lchown(struct lwp *l, void *v, register_t *retval)
2871 {
2872 struct sys_lchown_args /* {
2873 syscallarg(const char *) path;
2874 syscallarg(uid_t) uid;
2875 syscallarg(gid_t) gid;
2876 } */ *uap = v;
2877 int error;
2878 struct nameidata nd;
2879
2880 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2881 if ((error = namei(&nd)) != 0)
2882 return (error);
2883
2884 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2885
2886 vrele(nd.ni_vp);
2887 return (error);
2888 }
2889
2890 /*
2891 * Common routine to set ownership given a vnode.
2892 */
2893 static int
2894 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2895 int posix_semantics)
2896 {
2897 struct mount *mp;
2898 struct vattr vattr;
2899 mode_t newmode;
2900 int error;
2901
2902 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2903 return (error);
2904 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2905 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2906 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
2907 goto out;
2908
2909 #define CHANGED(x) ((int)(x) != -1)
2910 newmode = vattr.va_mode;
2911 if (posix_semantics) {
2912 /*
2913 * POSIX/XPG semantics: if the caller is not the super-user,
2914 * clear set-user-id and set-group-id bits. Both POSIX and
2915 * the XPG consider the behaviour for calls by the super-user
2916 * implementation-defined; we leave the set-user-id and set-
2917 * group-id settings intact in that case.
2918 */
2919 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2920 NULL) != 0)
2921 newmode &= ~(S_ISUID | S_ISGID);
2922 } else {
2923 /*
2924 * NetBSD semantics: when changing owner and/or group,
2925 * clear the respective bit(s).
2926 */
2927 if (CHANGED(uid))
2928 newmode &= ~S_ISUID;
2929 if (CHANGED(gid))
2930 newmode &= ~S_ISGID;
2931 }
2932 /* Update va_mode iff altered. */
2933 if (vattr.va_mode == newmode)
2934 newmode = VNOVAL;
2935
2936 VATTR_NULL(&vattr);
2937 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2938 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2939 vattr.va_mode = newmode;
2940 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2941 #undef CHANGED
2942
2943 out:
2944 VOP_UNLOCK(vp, 0);
2945 vn_finished_write(mp, 0);
2946 return (error);
2947 }
2948
2949 /*
2950 * Set the access and modification times given a path name; this
2951 * version follows links.
2952 */
2953 /* ARGSUSED */
2954 int
2955 sys_utimes(struct lwp *l, void *v, register_t *retval)
2956 {
2957 struct sys_utimes_args /* {
2958 syscallarg(const char *) path;
2959 syscallarg(const struct timeval *) tptr;
2960 } */ *uap = v;
2961 int error;
2962 struct nameidata nd;
2963
2964 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2965 if ((error = namei(&nd)) != 0)
2966 return (error);
2967
2968 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), l);
2969
2970 vrele(nd.ni_vp);
2971 return (error);
2972 }
2973
2974 /*
2975 * Set the access and modification times given a file descriptor.
2976 */
2977 /* ARGSUSED */
2978 int
2979 sys_futimes(struct lwp *l, void *v, register_t *retval)
2980 {
2981 struct sys_futimes_args /* {
2982 syscallarg(int) fd;
2983 syscallarg(const struct timeval *) tptr;
2984 } */ *uap = v;
2985 struct proc *p = l->l_proc;
2986 int error;
2987 struct file *fp;
2988
2989 /* getvnode() will use the descriptor for us */
2990 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2991 return (error);
2992
2993 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), l);
2994 FILE_UNUSE(fp, l);
2995 return (error);
2996 }
2997
2998 /*
2999 * Set the access and modification times given a path name; this
3000 * version does not follow links.
3001 */
3002 /* ARGSUSED */
3003 int
3004 sys_lutimes(struct lwp *l, void *v, register_t *retval)
3005 {
3006 struct sys_lutimes_args /* {
3007 syscallarg(const char *) path;
3008 syscallarg(const struct timeval *) tptr;
3009 } */ *uap = v;
3010 int error;
3011 struct nameidata nd;
3012
3013 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
3014 if ((error = namei(&nd)) != 0)
3015 return (error);
3016
3017 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), l);
3018
3019 vrele(nd.ni_vp);
3020 return (error);
3021 }
3022
3023 /*
3024 * Common routine to set access and modification times given a vnode.
3025 */
3026 static int
3027 change_utimes(struct vnode *vp, const struct timeval *tptr, struct lwp *l)
3028 {
3029 struct mount *mp;
3030 struct vattr vattr;
3031 int error;
3032
3033 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3034 return (error);
3035 VATTR_NULL(&vattr);
3036 if (tptr == NULL) {
3037 nanotime(&vattr.va_atime);
3038 vattr.va_mtime = vattr.va_atime;
3039 vattr.va_vaflags |= VA_UTIMES_NULL;
3040 } else {
3041 struct timeval tv[2];
3042
3043 error = copyin(tptr, tv, sizeof(tv));
3044 if (error)
3045 goto out;
3046 TIMEVAL_TO_TIMESPEC(&tv[0], &vattr.va_atime);
3047 TIMEVAL_TO_TIMESPEC(&tv[1], &vattr.va_mtime);
3048 }
3049 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3050 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3051 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
3052 VOP_UNLOCK(vp, 0);
3053 out:
3054 vn_finished_write(mp, 0);
3055 return (error);
3056 }
3057
3058 /*
3059 * Truncate a file given its path name.
3060 */
3061 /* ARGSUSED */
3062 int
3063 sys_truncate(struct lwp *l, void *v, register_t *retval)
3064 {
3065 struct sys_truncate_args /* {
3066 syscallarg(const char *) path;
3067 syscallarg(int) pad;
3068 syscallarg(off_t) length;
3069 } */ *uap = v;
3070 struct vnode *vp;
3071 struct mount *mp;
3072 struct vattr vattr;
3073 int error;
3074 struct nameidata nd;
3075
3076 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
3077 if ((error = namei(&nd)) != 0)
3078 return (error);
3079 vp = nd.ni_vp;
3080 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3081 vrele(vp);
3082 return (error);
3083 }
3084 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3085 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3086 if (vp->v_type == VDIR)
3087 error = EISDIR;
3088 else if ((error = vn_writechk(vp)) == 0 &&
3089 (error = VOP_ACCESS(vp, VWRITE, l->l_cred, l)) == 0) {
3090 VATTR_NULL(&vattr);
3091 vattr.va_size = SCARG(uap, length);
3092 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
3093 }
3094 vput(vp);
3095 vn_finished_write(mp, 0);
3096 return (error);
3097 }
3098
3099 /*
3100 * Truncate a file given a file descriptor.
3101 */
3102 /* ARGSUSED */
3103 int
3104 sys_ftruncate(struct lwp *l, void *v, register_t *retval)
3105 {
3106 struct sys_ftruncate_args /* {
3107 syscallarg(int) fd;
3108 syscallarg(int) pad;
3109 syscallarg(off_t) length;
3110 } */ *uap = v;
3111 struct proc *p = l->l_proc;
3112 struct mount *mp;
3113 struct vattr vattr;
3114 struct vnode *vp;
3115 struct file *fp;
3116 int error;
3117
3118 /* getvnode() will use the descriptor for us */
3119 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3120 return (error);
3121 if ((fp->f_flag & FWRITE) == 0) {
3122 error = EINVAL;
3123 goto out;
3124 }
3125 vp = (struct vnode *)fp->f_data;
3126 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3127 FILE_UNUSE(fp, l);
3128 return (error);
3129 }
3130 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3131 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3132 if (vp->v_type == VDIR)
3133 error = EISDIR;
3134 else if ((error = vn_writechk(vp)) == 0) {
3135 VATTR_NULL(&vattr);
3136 vattr.va_size = SCARG(uap, length);
3137 error = VOP_SETATTR(vp, &vattr, fp->f_cred, l);
3138 }
3139 VOP_UNLOCK(vp, 0);
3140 vn_finished_write(mp, 0);
3141 out:
3142 FILE_UNUSE(fp, l);
3143 return (error);
3144 }
3145
3146 /*
3147 * Sync an open file.
3148 */
3149 /* ARGSUSED */
3150 int
3151 sys_fsync(struct lwp *l, void *v, register_t *retval)
3152 {
3153 struct sys_fsync_args /* {
3154 syscallarg(int) fd;
3155 } */ *uap = v;
3156 struct proc *p = l->l_proc;
3157 struct vnode *vp;
3158 struct mount *mp;
3159 struct file *fp;
3160 int error;
3161
3162 /* getvnode() will use the descriptor for us */
3163 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3164 return (error);
3165 vp = (struct vnode *)fp->f_data;
3166 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3167 FILE_UNUSE(fp, l);
3168 return (error);
3169 }
3170 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3171 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, l);
3172 if (error == 0 && bioops.io_fsync != NULL &&
3173 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3174 (*bioops.io_fsync)(vp, 0);
3175 VOP_UNLOCK(vp, 0);
3176 vn_finished_write(mp, 0);
3177 FILE_UNUSE(fp, l);
3178 return (error);
3179 }
3180
3181 /*
3182 * Sync a range of file data. API modeled after that found in AIX.
3183 *
3184 * FDATASYNC indicates that we need only save enough metadata to be able
3185 * to re-read the written data. Note we duplicate AIX's requirement that
3186 * the file be open for writing.
3187 */
3188 /* ARGSUSED */
3189 int
3190 sys_fsync_range(struct lwp *l, void *v, register_t *retval)
3191 {
3192 struct sys_fsync_range_args /* {
3193 syscallarg(int) fd;
3194 syscallarg(int) flags;
3195 syscallarg(off_t) start;
3196 syscallarg(off_t) length;
3197 } */ *uap = v;
3198 struct proc *p = l->l_proc;
3199 struct vnode *vp;
3200 struct file *fp;
3201 int flags, nflags;
3202 off_t s, e, len;
3203 int error;
3204
3205 /* getvnode() will use the descriptor for us */
3206 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3207 return (error);
3208
3209 if ((fp->f_flag & FWRITE) == 0) {
3210 FILE_UNUSE(fp, l);
3211 return (EBADF);
3212 }
3213
3214 flags = SCARG(uap, flags);
3215 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3216 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3217 return (EINVAL);
3218 }
3219 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3220 if (flags & FDATASYNC)
3221 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3222 else
3223 nflags = FSYNC_WAIT;
3224 if (flags & FDISKSYNC)
3225 nflags |= FSYNC_CACHE;
3226
3227 len = SCARG(uap, length);
3228 /* If length == 0, we do the whole file, and s = l = 0 will do that */
3229 if (len) {
3230 s = SCARG(uap, start);
3231 e = s + len;
3232 if (e < s) {
3233 FILE_UNUSE(fp, l);
3234 return (EINVAL);
3235 }
3236 } else {
3237 e = 0;
3238 s = 0;
3239 }
3240
3241 vp = (struct vnode *)fp->f_data;
3242 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3243 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e, l);
3244
3245 if (error == 0 && bioops.io_fsync != NULL &&
3246 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3247 (*bioops.io_fsync)(vp, nflags);
3248
3249 VOP_UNLOCK(vp, 0);
3250 FILE_UNUSE(fp, l);
3251 return (error);
3252 }
3253
3254 /*
3255 * Sync the data of an open file.
3256 */
3257 /* ARGSUSED */
3258 int
3259 sys_fdatasync(struct lwp *l, void *v, register_t *retval)
3260 {
3261 struct sys_fdatasync_args /* {
3262 syscallarg(int) fd;
3263 } */ *uap = v;
3264 struct proc *p = l->l_proc;
3265 struct vnode *vp;
3266 struct file *fp;
3267 int error;
3268
3269 /* getvnode() will use the descriptor for us */
3270 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3271 return (error);
3272 if ((fp->f_flag & FWRITE) == 0) {
3273 FILE_UNUSE(fp, l);
3274 return (EBADF);
3275 }
3276 vp = (struct vnode *)fp->f_data;
3277 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3278 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, l);
3279 VOP_UNLOCK(vp, 0);
3280 FILE_UNUSE(fp, l);
3281 return (error);
3282 }
3283
3284 /*
3285 * Rename files, (standard) BSD semantics frontend.
3286 */
3287 /* ARGSUSED */
3288 int
3289 sys_rename(struct lwp *l, void *v, register_t *retval)
3290 {
3291 struct sys_rename_args /* {
3292 syscallarg(const char *) from;
3293 syscallarg(const char *) to;
3294 } */ *uap = v;
3295
3296 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 0));
3297 }
3298
3299 /*
3300 * Rename files, POSIX semantics frontend.
3301 */
3302 /* ARGSUSED */
3303 int
3304 sys___posix_rename(struct lwp *l, void *v, register_t *retval)
3305 {
3306 struct sys___posix_rename_args /* {
3307 syscallarg(const char *) from;
3308 syscallarg(const char *) to;
3309 } */ *uap = v;
3310
3311 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 1));
3312 }
3313
3314 /*
3315 * Rename files. Source and destination must either both be directories,
3316 * or both not be directories. If target is a directory, it must be empty.
3317 * If `from' and `to' refer to the same object, the value of the `retain'
3318 * argument is used to determine whether `from' will be
3319 *
3320 * (retain == 0) deleted unless `from' and `to' refer to the same
3321 * object in the file system's name space (BSD).
3322 * (retain == 1) always retained (POSIX).
3323 */
3324 static int
3325 rename_files(const char *from, const char *to, struct lwp *l, int retain)
3326 {
3327 struct mount *mp = NULL;
3328 struct vnode *tvp, *fvp, *tdvp;
3329 struct nameidata fromnd, tond;
3330 struct proc *p;
3331 int error;
3332
3333 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
3334 from, l);
3335 if ((error = namei(&fromnd)) != 0)
3336 return (error);
3337 fvp = fromnd.ni_vp;
3338 error = vn_start_write(fvp, &mp, V_WAIT | V_PCATCH);
3339 if (error != 0) {
3340 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3341 vrele(fromnd.ni_dvp);
3342 vrele(fvp);
3343 if (fromnd.ni_startdir)
3344 vrele(fromnd.ni_startdir);
3345 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3346 return (error);
3347 }
3348 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3349 (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, l);
3350 if ((error = namei(&tond)) != 0) {
3351 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3352 vrele(fromnd.ni_dvp);
3353 vrele(fvp);
3354 goto out1;
3355 }
3356 tdvp = tond.ni_dvp;
3357 tvp = tond.ni_vp;
3358
3359 if (tvp != NULL) {
3360 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3361 error = ENOTDIR;
3362 goto out;
3363 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3364 error = EISDIR;
3365 goto out;
3366 }
3367 }
3368
3369 if (fvp == tdvp)
3370 error = EINVAL;
3371
3372 /*
3373 * Source and destination refer to the same object.
3374 */
3375 if (fvp == tvp) {
3376 if (retain)
3377 error = -1;
3378 else if (fromnd.ni_dvp == tdvp &&
3379 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3380 !memcmp(fromnd.ni_cnd.cn_nameptr,
3381 tond.ni_cnd.cn_nameptr,
3382 fromnd.ni_cnd.cn_namelen))
3383 error = -1;
3384 }
3385
3386 #if NVERIEXEC > 0
3387 if (!error) {
3388 pathname_t frompath, topath;
3389
3390 error = pathname_get(fromnd.ni_dirp, fromnd.ni_segflg,
3391 &frompath);
3392 if (!error)
3393 error = pathname_get(tond.ni_dirp, tond.ni_segflg,
3394 &topath);
3395 if (!error)
3396 error = veriexec_renamechk(fvp, pathname_path(frompath),
3397 tvp, pathname_path(topath), l);
3398
3399 pathname_put(frompath);
3400 pathname_put(topath);
3401 }
3402 #endif /* NVERIEXEC > 0 */
3403
3404 out:
3405 p = l->l_proc;
3406 if (!error) {
3407 VOP_LEASE(tdvp, l, l->l_cred, LEASE_WRITE);
3408 if (fromnd.ni_dvp != tdvp)
3409 VOP_LEASE(fromnd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3410 if (tvp) {
3411 VOP_LEASE(tvp, l, l->l_cred, LEASE_WRITE);
3412 }
3413 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3414 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3415 } else {
3416 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3417 if (tdvp == tvp)
3418 vrele(tdvp);
3419 else
3420 vput(tdvp);
3421 if (tvp)
3422 vput(tvp);
3423 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3424 vrele(fromnd.ni_dvp);
3425 vrele(fvp);
3426 }
3427 vrele(tond.ni_startdir);
3428 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
3429 out1:
3430 vn_finished_write(mp, 0);
3431 if (fromnd.ni_startdir)
3432 vrele(fromnd.ni_startdir);
3433 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3434 return (error == -1 ? 0 : error);
3435 }
3436
3437 /*
3438 * Make a directory file.
3439 */
3440 /* ARGSUSED */
3441 int
3442 sys_mkdir(struct lwp *l, void *v, register_t *retval)
3443 {
3444 struct sys_mkdir_args /* {
3445 syscallarg(const char *) path;
3446 syscallarg(int) mode;
3447 } */ *uap = v;
3448 struct proc *p = l->l_proc;
3449 struct mount *mp;
3450 struct vnode *vp;
3451 struct vattr vattr;
3452 int error;
3453 struct nameidata nd;
3454
3455 restart:
3456 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR, UIO_USERSPACE,
3457 SCARG(uap, path), l);
3458 if ((error = namei(&nd)) != 0)
3459 return (error);
3460 vp = nd.ni_vp;
3461 if (vp != NULL) {
3462 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3463 if (nd.ni_dvp == vp)
3464 vrele(nd.ni_dvp);
3465 else
3466 vput(nd.ni_dvp);
3467 vrele(vp);
3468 return (EEXIST);
3469 }
3470 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3471 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3472 if (nd.ni_dvp == vp)
3473 vrele(nd.ni_dvp);
3474 else
3475 vput(nd.ni_dvp);
3476 if ((error = vn_start_write(NULL, &mp,
3477 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
3478 return (error);
3479 goto restart;
3480 }
3481 VATTR_NULL(&vattr);
3482 vattr.va_type = VDIR;
3483 vattr.va_mode =
3484 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3485 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3486 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3487 if (!error)
3488 vput(nd.ni_vp);
3489 vn_finished_write(mp, 0);
3490 return (error);
3491 }
3492
3493 /*
3494 * Remove a directory file.
3495 */
3496 /* ARGSUSED */
3497 int
3498 sys_rmdir(struct lwp *l, void *v, register_t *retval)
3499 {
3500 struct sys_rmdir_args /* {
3501 syscallarg(const char *) path;
3502 } */ *uap = v;
3503 struct mount *mp;
3504 struct vnode *vp;
3505 int error;
3506 struct nameidata nd;
3507
3508 restart:
3509 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
3510 SCARG(uap, path), l);
3511 if ((error = namei(&nd)) != 0)
3512 return (error);
3513 vp = nd.ni_vp;
3514 if (vp->v_type != VDIR) {
3515 error = ENOTDIR;
3516 goto out;
3517 }
3518 /*
3519 * No rmdir "." please.
3520 */
3521 if (nd.ni_dvp == vp) {
3522 error = EINVAL;
3523 goto out;
3524 }
3525 /*
3526 * The root of a mounted filesystem cannot be deleted.
3527 */
3528 if (vp->v_flag & VROOT) {
3529 error = EBUSY;
3530 goto out;
3531 }
3532 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3533 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3534 if (nd.ni_dvp == vp)
3535 vrele(nd.ni_dvp);
3536 else
3537 vput(nd.ni_dvp);
3538 vput(vp);
3539 if ((error = vn_start_write(NULL, &mp,
3540 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
3541 return (error);
3542 goto restart;
3543 }
3544 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3545 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3546 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3547 vn_finished_write(mp, 0);
3548 return (error);
3549
3550 out:
3551 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3552 if (nd.ni_dvp == vp)
3553 vrele(nd.ni_dvp);
3554 else
3555 vput(nd.ni_dvp);
3556 vput(vp);
3557 return (error);
3558 }
3559
3560 /*
3561 * Read a block of directory entries in a file system independent format.
3562 */
3563 int
3564 sys___getdents30(struct lwp *l, void *v, register_t *retval)
3565 {
3566 struct sys___getdents30_args /* {
3567 syscallarg(int) fd;
3568 syscallarg(char *) buf;
3569 syscallarg(size_t) count;
3570 } */ *uap = v;
3571 struct proc *p = l->l_proc;
3572 struct file *fp;
3573 int error, done;
3574
3575 /* getvnode() will use the descriptor for us */
3576 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3577 return (error);
3578 if ((fp->f_flag & FREAD) == 0) {
3579 error = EBADF;
3580 goto out;
3581 }
3582 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3583 SCARG(uap, count), &done, l, 0, 0);
3584 #ifdef KTRACE
3585 if (!error && KTRPOINT(p, KTR_GENIO)) {
3586 struct iovec iov;
3587 iov.iov_base = SCARG(uap, buf);
3588 iov.iov_len = done;
3589 ktrgenio(l, SCARG(uap, fd), UIO_READ, &iov, done, 0);
3590 }
3591 #endif
3592 *retval = done;
3593 out:
3594 FILE_UNUSE(fp, l);
3595 return (error);
3596 }
3597
3598 /*
3599 * Set the mode mask for creation of filesystem nodes.
3600 */
3601 int
3602 sys_umask(struct lwp *l, void *v, register_t *retval)
3603 {
3604 struct sys_umask_args /* {
3605 syscallarg(mode_t) newmask;
3606 } */ *uap = v;
3607 struct proc *p = l->l_proc;
3608 struct cwdinfo *cwdi;
3609
3610 cwdi = p->p_cwdi;
3611 *retval = cwdi->cwdi_cmask;
3612 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3613 return (0);
3614 }
3615
3616 /*
3617 * Void all references to file by ripping underlying filesystem
3618 * away from vnode.
3619 */
3620 /* ARGSUSED */
3621 int
3622 sys_revoke(struct lwp *l, void *v, register_t *retval)
3623 {
3624 struct sys_revoke_args /* {
3625 syscallarg(const char *) path;
3626 } */ *uap = v;
3627 struct mount *mp;
3628 struct vnode *vp;
3629 struct vattr vattr;
3630 int error;
3631 struct nameidata nd;
3632
3633 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
3634 if ((error = namei(&nd)) != 0)
3635 return (error);
3636 vp = nd.ni_vp;
3637 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
3638 goto out;
3639 if (kauth_cred_geteuid(l->l_cred) != vattr.va_uid &&
3640 (error = kauth_authorize_generic(l->l_cred,
3641 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
3642 goto out;
3643 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3644 goto out;
3645 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
3646 VOP_REVOKE(vp, REVOKEALL);
3647 vn_finished_write(mp, 0);
3648 out:
3649 vrele(vp);
3650 return (error);
3651 }
3652
3653 /*
3654 * Convert a user file descriptor to a kernel file entry.
3655 */
3656 int
3657 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3658 {
3659 struct vnode *vp;
3660 struct file *fp;
3661
3662 if ((fp = fd_getfile(fdp, fd)) == NULL)
3663 return (EBADF);
3664
3665 FILE_USE(fp);
3666
3667 if (fp->f_type != DTYPE_VNODE) {
3668 FILE_UNUSE(fp, NULL);
3669 return (EINVAL);
3670 }
3671
3672 vp = (struct vnode *)fp->f_data;
3673 if (vp->v_type == VBAD) {
3674 FILE_UNUSE(fp, NULL);
3675 return (EBADF);
3676 }
3677
3678 *fpp = fp;
3679 return (0);
3680 }
3681