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