Annotation of src/sys/kern/vfs_syscalls.c, Revision 1.515
1.515 ! hannken 1: /* $NetBSD: vfs_syscalls.c,v 1.514 2017/05/07 08:25:54 hannken Exp $ */
1.345 ad 2:
3: /*-
1.390 ad 4: * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
1.345 ad 5: * All rights reserved.
6: *
1.390 ad 7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Andrew Doran.
9: *
1.345 ad 10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
1.31 cgd 31:
32: /*
33: * Copyright (c) 1989, 1993
34: * The Regents of the University of California. All rights reserved.
35: * (c) UNIX System Laboratories, Inc.
36: * All or some portions of this file are derived from material licensed
37: * to the University of California by American Telephone and Telegraph
38: * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39: * the permission of UNIX System Laboratories, Inc.
40: *
41: * Redistribution and use in source and binary forms, with or without
42: * modification, are permitted provided that the following conditions
43: * are met:
44: * 1. Redistributions of source code must retain the above copyright
45: * notice, this list of conditions and the following disclaimer.
46: * 2. Redistributions in binary form must reproduce the above copyright
47: * notice, this list of conditions and the following disclaimer in the
48: * documentation and/or other materials provided with the distribution.
1.191 agc 49: * 3. Neither the name of the University nor the names of its contributors
1.31 cgd 50: * may be used to endorse or promote products derived from this software
51: * without specific prior written permission.
52: *
53: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63: * SUCH DAMAGE.
64: *
1.113 fvdl 65: * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
1.31 cgd 66: */
1.173 lukem 67:
1.420 rmind 68: /*
69: * Virtual File System System Calls
70: */
71:
1.173 lukem 72: #include <sys/cdefs.h>
1.515 ! hannken 73: __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.514 2017/05/07 08:25:54 hannken Exp $");
1.111 mrg 74:
1.378 ad 75: #ifdef _KERNEL_OPT
1.258 elad 76: #include "opt_fileassoc.h"
1.261 dogcow 77: #include "veriexec.h"
1.378 ad 78: #endif
1.31 cgd 79:
80: #include <sys/param.h>
81: #include <sys/systm.h>
82: #include <sys/namei.h>
83: #include <sys/filedesc.h>
84: #include <sys/kernel.h>
85: #include <sys/file.h>
1.433 manu 86: #include <sys/fcntl.h>
1.31 cgd 87: #include <sys/stat.h>
88: #include <sys/vnode.h>
89: #include <sys/mount.h>
1.506 hannken 90: #include <sys/fstrans.h>
1.31 cgd 91: #include <sys/proc.h>
92: #include <sys/uio.h>
1.248 yamt 93: #include <sys/kmem.h>
1.31 cgd 94: #include <sys/dirent.h>
1.172 simonb 95: #include <sys/sysctl.h>
1.35 cgd 96: #include <sys/syscallargs.h>
1.306 dsl 97: #include <sys/vfs_syscalls.h>
1.444 dholland 98: #include <sys/quota.h>
99: #include <sys/quotactl.h>
1.192 drochner 100: #include <sys/ktrace.h>
1.251 elad 101: #ifdef FILEASSOC
102: #include <sys/fileassoc.h>
103: #endif /* FILEASSOC */
1.430 manu 104: #include <sys/extattr.h>
1.219 blymn 105: #include <sys/verified_exec.h>
1.242 elad 106: #include <sys/kauth.h>
1.346 ad 107: #include <sys/atomic.h>
1.360 ad 108: #include <sys/module.h>
1.380 pooka 109: #include <sys/buf.h>
1.35 cgd 110:
1.148 fvdl 111: #include <miscfs/genfs/genfs.h>
1.342 ad 112: #include <miscfs/specfs/specdev.h>
1.181 thorpej 113:
1.232 jmmv 114: #include <nfs/rpcv2.h>
115: #include <nfs/nfsproto.h>
116: #include <nfs/nfs.h>
117: #include <nfs/nfs_var.h>
118:
1.488 dholland 119: /* XXX this shouldn't be here */
120: #ifndef OFF_T_MAX
121: #define OFF_T_MAX __type_max(off_t)
122: #endif
123:
1.234 christos 124: static int change_flags(struct vnode *, u_long, struct lwp *);
1.476 njoly 125: static int change_mode(struct vnode *, int, struct lwp *);
1.234 christos 126: static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
1.460 manu 127: static int do_sys_openat(lwp_t *, int, const char *, int, int, int *);
128: static int do_sys_mkdirat(struct lwp *l, int, const char *, mode_t,
129: enum uio_seg);
130: static int do_sys_mkfifoat(struct lwp *, int, const char *, mode_t);
131: static int do_sys_symlinkat(struct lwp *, const char *, int, const char *,
132: enum uio_seg);
133: static int do_sys_renameat(struct lwp *l, int, const char *, int, const char *,
134: enum uio_seg, int);
135: static int do_sys_readlinkat(struct lwp *, int, const char *, char *,
136: size_t, register_t *);
137: static int do_sys_unlinkat(struct lwp *, int, const char *, int, enum uio_seg);
138:
139: static int fd_nameiat(struct lwp *, int, struct nameidata *);
140: static int fd_nameiat_simple_user(struct lwp *, int, const char *,
141: namei_simple_flags_t, struct vnode **);
142:
1.63 christos 143:
1.99 thorpej 144: /*
145: * This table is used to maintain compatibility with 4.3BSD
1.378 ad 146: * and NetBSD 0.9 mount syscalls - and possibly other systems.
147: * Note, the order is important!
1.124 thorpej 148: *
1.167 jdolecek 149: * Do not modify this table. It should only contain filesystems
150: * supported by NetBSD 0.9 and 4.3BSD.
1.99 thorpej 151: */
1.167 jdolecek 152: const char * const mountcompatnames[] = {
1.99 thorpej 153: NULL, /* 0 = MOUNT_NONE */
1.167 jdolecek 154: MOUNT_FFS, /* 1 = MOUNT_UFS */
1.99 thorpej 155: MOUNT_NFS, /* 2 */
156: MOUNT_MFS, /* 3 */
157: MOUNT_MSDOS, /* 4 */
1.167 jdolecek 158: MOUNT_CD9660, /* 5 = MOUNT_ISOFS */
159: MOUNT_FDESC, /* 6 */
160: MOUNT_KERNFS, /* 7 */
161: NULL, /* 8 = MOUNT_DEVFS */
162: MOUNT_AFS, /* 9 */
1.99 thorpej 163: };
1.420 rmind 164:
165: const int nmountcompatnames = __arraycount(mountcompatnames);
1.99 thorpej 166:
1.460 manu 167: static int
168: fd_nameiat(struct lwp *l, int fdat, struct nameidata *ndp)
169: {
170: file_t *dfp;
171: int error;
172:
173: if (fdat != AT_FDCWD) {
174: if ((error = fd_getvnode(fdat, &dfp)) != 0)
175: goto out;
176:
1.491 matt 177: NDAT(ndp, dfp->f_vnode);
1.460 manu 178: }
179:
180: error = namei(ndp);
181:
182: if (fdat != AT_FDCWD)
183: fd_putfile(fdat);
184: out:
185: return error;
186: }
187:
188: static int
189: fd_nameiat_simple_user(struct lwp *l, int fdat, const char *path,
190: namei_simple_flags_t sflags, struct vnode **vp_ret)
191: {
192: file_t *dfp;
193: struct vnode *dvp;
194: int error;
195:
196: if (fdat != AT_FDCWD) {
197: if ((error = fd_getvnode(fdat, &dfp)) != 0)
198: goto out;
199:
1.491 matt 200: dvp = dfp->f_vnode;
1.460 manu 201: } else {
202: dvp = NULL;
203: }
204:
205: error = nameiat_simple_user(dvp, path, sflags, vp_ret);
206:
207: if (fdat != AT_FDCWD)
208: fd_putfile(fdat);
209: out:
210: return error;
211: }
212:
1.285 elad 213: static int
1.422 christos 214: open_setfp(struct lwp *l, file_t *fp, struct vnode *vp, int indx, int flags)
215: {
216: int error;
217:
218: fp->f_flag = flags & FMASK;
219: fp->f_type = DTYPE_VNODE;
220: fp->f_ops = &vnops;
1.491 matt 221: fp->f_vnode = vp;
1.422 christos 222:
223: if (flags & (O_EXLOCK | O_SHLOCK)) {
224: struct flock lf;
225: int type;
226:
227: lf.l_whence = SEEK_SET;
228: lf.l_start = 0;
229: lf.l_len = 0;
230: if (flags & O_EXLOCK)
231: lf.l_type = F_WRLCK;
232: else
233: lf.l_type = F_RDLCK;
234: type = F_FLOCK;
235: if ((flags & FNONBLOCK) == 0)
236: type |= F_WAIT;
237: VOP_UNLOCK(vp);
238: error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
239: if (error) {
240: (void) vn_close(vp, fp->f_flag, fp->f_cred);
241: fd_abort(l->l_proc, fp, indx);
242: return error;
243: }
244: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
245: atomic_or_uint(&fp->f_flag, FHASLOCK);
246: }
247: if (flags & O_CLOEXEC)
248: fd_set_exclose(l, indx, true);
249: return 0;
250: }
251:
252: static int
1.283 elad 253: mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
1.324 pooka 254: void *data, size_t *data_len)
1.56 thorpej 255: {
1.113 fvdl 256: struct mount *mp;
1.283 elad 257: int error = 0, saved_flags;
258:
259: mp = vp->v_mount;
260: saved_flags = mp->mnt_flag;
261:
1.329 ad 262: /* We can operate only on VV_ROOT nodes. */
263: if ((vp->v_vflag & VV_ROOT) == 0) {
264: error = EINVAL;
265: goto out;
266: }
1.31 cgd 267:
1.218 yamt 268: /*
1.283 elad 269: * We only allow the filesystem to be reloaded if it
1.373 ad 270: * is currently mounted read-only. Additionally, we
271: * prevent read-write to read-only downgrades.
1.283 elad 272: */
1.373 ad 273: if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 &&
1.414 pooka 274: (mp->mnt_flag & MNT_RDONLY) == 0 &&
275: (mp->mnt_iflag & IMNT_CAN_RWTORO) == 0) {
1.329 ad 276: error = EOPNOTSUPP; /* Needs translation */
277: goto out;
278: }
1.292 elad 279:
280: error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
281: KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
282: if (error)
1.329 ad 283: goto out;
1.292 elad 284:
1.508 hannken 285: error = vfs_suspend(mp, 0);
286: if (error)
287: goto out;
288:
1.358 ad 289: mutex_enter(&mp->mnt_updating);
290:
1.286 yamt 291: mp->mnt_flag &= ~MNT_OP_FLAGS;
1.441 christos 292: mp->mnt_flag |= flags & MNT_OP_FLAGS;
1.286 yamt 293:
1.283 elad 294: /*
295: * Set the mount level flags.
296: */
1.507 hannken 297: if ((flags & MNT_RDONLY) != (mp->mnt_flag & MNT_RDONLY)) {
298: if ((flags & MNT_RDONLY))
299: mp->mnt_iflag |= IMNT_WANTRDONLY;
300: else
301: mp->mnt_iflag |= IMNT_WANTRDWR;
302: }
1.441 christos 303: mp->mnt_flag &= ~MNT_BASIC_FLAGS;
1.509 hannken 304: mp->mnt_flag |= flags & MNT_BASIC_FLAGS;
305: if ((mp->mnt_iflag & IMNT_WANTRDONLY))
306: mp->mnt_flag &= ~MNT_RDONLY;
307:
1.332 pooka 308: error = VFS_MOUNT(mp, path, data, data_len);
1.283 elad 309:
1.320 dsl 310: if (error && data != NULL) {
1.283 elad 311: int error2;
312:
1.378 ad 313: /*
314: * Update failed; let's try and see if it was an
1.379 ad 315: * export request. For compat with 3.0 and earlier.
1.378 ad 316: */
1.381 ad 317: error2 = vfs_hooks_reexport(mp, path, data);
1.283 elad 318:
1.381 ad 319: /*
320: * Only update error code if the export request was
1.283 elad 321: * understood but some problem occurred while
1.381 ad 322: * processing it.
323: */
1.283 elad 324: if (error2 != EJUSTRETURN)
325: error = error2;
326: }
1.381 ad 327:
1.507 hannken 328: if (error == 0 && (mp->mnt_iflag & IMNT_WANTRDONLY))
329: mp->mnt_flag |= MNT_RDONLY;
1.283 elad 330: if (error)
331: mp->mnt_flag = saved_flags;
1.286 yamt 332: mp->mnt_flag &= ~MNT_OP_FLAGS;
1.507 hannken 333: mp->mnt_iflag &= ~(IMNT_WANTRDONLY | IMNT_WANTRDWR);
1.283 elad 334: if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
1.498 hannken 335: if ((mp->mnt_iflag & IMNT_ONWORKLIST) == 0)
336: vfs_syncer_add_to_worklist(mp);
1.125 tls 337: } else {
1.498 hannken 338: if ((mp->mnt_iflag & IMNT_ONWORKLIST) != 0)
339: vfs_syncer_remove_from_worklist(mp);
1.283 elad 340: }
1.358 ad 341: mutex_exit(&mp->mnt_updating);
1.508 hannken 342: vfs_resume(mp);
1.283 elad 343:
1.430 manu 344: if ((error == 0) && !(saved_flags & MNT_EXTATTR) &&
345: (flags & MNT_EXTATTR)) {
1.452 manu 346: if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_START,
1.430 manu 347: NULL, 0, NULL) != 0) {
348: printf("%s: failed to start extattr, error = %d",
1.452 manu 349: mp->mnt_stat.f_mntonname, error);
1.430 manu 350: mp->mnt_flag &= ~MNT_EXTATTR;
351: }
352: }
353:
354: if ((error == 0) && (saved_flags & MNT_EXTATTR) &&
355: !(flags & MNT_EXTATTR)) {
1.452 manu 356: if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_STOP,
1.430 manu 357: NULL, 0, NULL) != 0) {
358: printf("%s: failed to stop extattr, error = %d",
1.452 manu 359: mp->mnt_stat.f_mntonname, error);
1.430 manu 360: mp->mnt_flag |= MNT_RDONLY;
361: }
362: }
1.329 ad 363: out:
1.283 elad 364: return (error);
365: }
366:
1.285 elad 367: static int
1.501 maxv 368: mount_get_vfsops(const char *fstype, enum uio_seg type_seg,
369: struct vfsops **vfsops)
1.283 elad 370: {
1.322 christos 371: char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
1.283 elad 372: int error;
373:
1.501 maxv 374: if (type_seg == UIO_USERSPACE) {
375: /* Copy file-system type from userspace. */
376: error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
377: } else {
378: error = copystr(fstype, fstypename, sizeof(fstypename), NULL);
1.503 martin 379: KASSERT(error == 0);
1.501 maxv 380: }
381:
1.63 christos 382: if (error) {
1.54 cgd 383: /*
1.227 jmmv 384: * Historically, filesystem types were identified by numbers.
1.54 cgd 385: * If we get an integer for the filesystem type instead of a
386: * string, we check to see if it matches one of the historic
387: * filesystem types.
1.205 junyoung 388: */
1.283 elad 389: u_long fsindex = (u_long)fstype;
1.99 thorpej 390: if (fsindex >= nmountcompatnames ||
1.320 dsl 391: mountcompatnames[fsindex] == NULL)
392: return ENODEV;
1.331 pooka 393: strlcpy(fstypename, mountcompatnames[fsindex],
394: sizeof(fstypename));
1.31 cgd 395: }
1.283 elad 396:
1.378 ad 397: /* Accept `ufs' as an alias for `ffs', for compatibility. */
1.320 dsl 398: if (strcmp(fstypename, "ufs") == 0)
399: fstypename[0] = 'f';
1.285 elad 400:
1.360 ad 401: if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
402: return 0;
403:
404: /* If we can autoload a vfs module, try again */
1.400 martin 405: (void)module_autoload(fstypename, MODULE_CLASS_VFS);
1.360 ad 406:
407: if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
408: return 0;
409:
410: return ENODEV;
1.320 dsl 411: }
412:
413: static int
1.283 elad 414: mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
1.324 pooka 415: void *data, size_t *data_len)
1.283 elad 416: {
417: struct mount *mp;
418: int error;
419:
1.289 elad 420: /* If MNT_GETARGS is specified, it should be the only flag. */
1.320 dsl 421: if (flags & ~MNT_GETARGS)
422: return EINVAL;
1.289 elad 423:
1.283 elad 424: mp = vp->v_mount;
425:
1.292 elad 426: /* XXX: probably some notion of "can see" here if we want isolation. */
427: error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
428: KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
429: if (error)
1.320 dsl 430: return error;
1.292 elad 431:
1.329 ad 432: if ((vp->v_vflag & VV_ROOT) == 0)
1.320 dsl 433: return EINVAL;
1.283 elad 434:
1.512 hannken 435: if (vfs_busy(mp))
1.320 dsl 436: return EPERM;
1.283 elad 437:
1.358 ad 438: mutex_enter(&mp->mnt_updating);
1.286 yamt 439: mp->mnt_flag &= ~MNT_OP_FLAGS;
440: mp->mnt_flag |= MNT_GETARGS;
1.332 pooka 441: error = VFS_MOUNT(mp, path, data, data_len);
1.286 yamt 442: mp->mnt_flag &= ~MNT_OP_FLAGS;
1.358 ad 443: mutex_exit(&mp->mnt_updating);
1.283 elad 444:
1.512 hannken 445: vfs_unbusy(mp);
1.283 elad 446: return (error);
447: }
448:
1.321 dsl 449: int
1.335 dsl 450: sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
1.321 dsl 451: {
1.335 dsl 452: /* {
1.321 dsl 453: syscallarg(const char *) type;
454: syscallarg(const char *) path;
455: syscallarg(int) flags;
456: syscallarg(void *) data;
457: syscallarg(size_t) data_len;
1.335 dsl 458: } */
1.321 dsl 459:
1.501 maxv 460: return do_sys_mount(l, SCARG(uap, type), UIO_USERSPACE, SCARG(uap, path),
1.321 dsl 461: SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
462: SCARG(uap, data_len), retval);
463: }
1.320 dsl 464:
465: int
1.501 maxv 466: do_sys_mount(struct lwp *l, const char *type, enum uio_seg type_seg,
1.320 dsl 467: const char *path, int flags, void *data, enum uio_seg data_seg,
468: size_t data_len, register_t *retval)
469: {
1.502 martin 470: struct vfsops *vfsops = NULL; /* XXX gcc4.8 */
1.283 elad 471: struct vnode *vp;
1.320 dsl 472: void *data_buf = data;
1.403 pooka 473: bool vfsopsrele = false;
1.482 maxv 474: size_t alloc_sz = 0;
1.283 elad 475: int error;
476:
477: /*
478: * Get vnode to be covered
479: */
1.395 dholland 480: error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
1.403 pooka 481: if (error != 0) {
482: vp = NULL;
483: goto done;
484: }
1.283 elad 485:
1.501 maxv 486: if (flags & (MNT_GETARGS | MNT_UPDATE)) {
487: vfsops = vp->v_mount->mnt_op;
488: } else {
489: /* 'type' is userspace */
490: error = mount_get_vfsops(type, type_seg, &vfsops);
491: if (error != 0)
492: goto done;
493: vfsopsrele = true;
1.320 dsl 494: }
495:
1.479 maxv 496: /*
497: * We allow data to be NULL, even for userspace. Some fs's don't need
498: * it. The others will handle NULL.
499: */
1.320 dsl 500: if (data != NULL && data_seg == UIO_USERSPACE) {
501: if (data_len == 0) {
502: /* No length supplied, use default for filesystem */
503: data_len = vfsops->vfs_min_mount_data;
1.478 maxv 504:
1.378 ad 505: /*
506: * Hopefully a longer buffer won't make copyin() fail.
507: * For compatibility with 3.0 and earlier.
508: */
1.320 dsl 509: if (flags & MNT_UPDATE
510: && data_len < sizeof (struct mnt_export_args30))
511: data_len = sizeof (struct mnt_export_args30);
512: }
1.480 maxv 513: if ((data_len == 0) || (data_len > VFS_MAX_MOUNT_DATA)) {
1.478 maxv 514: error = EINVAL;
515: goto done;
516: }
1.482 maxv 517: alloc_sz = data_len;
518: data_buf = kmem_alloc(alloc_sz, KM_SLEEP);
1.283 elad 519:
1.320 dsl 520: /* NFS needs the buffer even for mnt_getargs .... */
521: error = copyin(data, data_buf, data_len);
522: if (error != 0)
523: goto done;
524: }
525:
526: if (flags & MNT_GETARGS) {
527: if (data_len == 0) {
528: error = EINVAL;
529: goto done;
530: }
1.324 pooka 531: error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
1.320 dsl 532: if (error != 0)
533: goto done;
534: if (data_seg == UIO_USERSPACE)
535: error = copyout(data_buf, data, data_len);
536: *retval = data_len;
537: } else if (flags & MNT_UPDATE) {
1.324 pooka 538: error = mount_update(l, vp, path, flags, data_buf, &data_len);
1.283 elad 539: } else {
540: /* Locking is handled internally in mount_domount(). */
1.403 pooka 541: KASSERT(vfsopsrele == true);
1.320 dsl 542: error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
1.405 hannken 543: &data_len);
1.403 pooka 544: vfsopsrele = false;
1.283 elad 545: }
546:
1.320 dsl 547: done:
1.403 pooka 548: if (vfsopsrele)
549: vfs_delref(vfsops);
1.337 ad 550: if (vp != NULL) {
1.405 hannken 551: vrele(vp);
1.337 ad 552: }
1.320 dsl 553: if (data_buf != data)
1.482 maxv 554: kmem_free(data_buf, alloc_sz);
1.31 cgd 555: return (error);
556: }
557:
558: /*
559: * Unmount a file system.
560: *
561: * Note: unmount takes a path to the vnode mounted on as argument,
562: * not special file (as before).
563: */
564: /* ARGSUSED */
1.63 christos 565: int
1.335 dsl 566: sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval)
1.56 thorpej 567: {
1.335 dsl 568: /* {
1.74 cgd 569: syscallarg(const char *) path;
1.35 cgd 570: syscallarg(int) flags;
1.335 dsl 571: } */
1.155 augustss 572: struct vnode *vp;
1.31 cgd 573: struct mount *mp;
574: int error;
1.409 dholland 575: struct pathbuf *pb;
1.31 cgd 576: struct nameidata nd;
577:
1.409 dholland 578: error = pathbuf_copyin(SCARG(uap, path), &pb);
579: if (error) {
580: return error;
581: }
582:
1.499 dholland 583: NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1.409 dholland 584: if ((error = namei(&nd)) != 0) {
585: pathbuf_destroy(pb);
586: return error;
587: }
1.31 cgd 588: vp = nd.ni_vp;
1.409 dholland 589: pathbuf_destroy(pb);
590:
1.43 mycroft 591: mp = vp->v_mount;
1.511 hannken 592: vfs_ref(mp);
1.406 hannken 593: VOP_UNLOCK(vp);
1.31 cgd 594:
1.295 elad 595: error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
596: KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
597: if (error) {
1.345 ad 598: vrele(vp);
1.511 hannken 599: vfs_rele(mp);
1.31 cgd 600: return (error);
601: }
602:
603: /*
1.47 mycroft 604: * Don't allow unmounting the root file system.
605: */
606: if (mp->mnt_flag & MNT_ROOTFS) {
1.345 ad 607: vrele(vp);
1.511 hannken 608: vfs_rele(mp);
1.47 mycroft 609: return (EINVAL);
610: }
611:
612: /*
1.31 cgd 613: * Must be the root of the filesystem
614: */
1.329 ad 615: if ((vp->v_vflag & VV_ROOT) == 0) {
1.345 ad 616: vrele(vp);
1.511 hannken 617: vfs_rele(mp);
1.31 cgd 618: return (EINVAL);
619: }
1.78 fvdl 620:
1.359 ad 621: vrele(vp);
1.358 ad 622: error = dounmount(mp, SCARG(uap, flags), l);
1.511 hannken 623: vfs_rele(mp);
1.358 ad 624: return error;
1.31 cgd 625: }
626:
627: /*
628: * Sync each mounted filesystem.
629: */
630: #ifdef DEBUG
631: int syncprt = 0;
632: struct ctldebug debug0 = { "syncprt", &syncprt };
633: #endif
634:
1.425 dsl 635: void
636: do_sys_sync(struct lwp *l)
1.31 cgd 637: {
1.510 hannken 638: mount_iterator_t *iter;
639: struct mount *mp;
1.31 cgd 640: int asyncflag;
1.257 ad 641:
1.510 hannken 642: mountlist_iterator_init(&iter);
643: while ((mp = mountlist_iterator_next(iter)) != NULL) {
1.358 ad 644: mutex_enter(&mp->mnt_updating);
1.307 hannken 645: if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1.31 cgd 646: asyncflag = mp->mnt_flag & MNT_ASYNC;
647: mp->mnt_flag &= ~MNT_ASYNC;
1.332 pooka 648: VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
1.31 cgd 649: if (asyncflag)
1.113 fvdl 650: mp->mnt_flag |= MNT_ASYNC;
1.31 cgd 651: }
1.358 ad 652: mutex_exit(&mp->mnt_updating);
1.31 cgd 653: }
1.510 hannken 654: mountlist_iterator_destroy(iter);
1.31 cgd 655: #ifdef DEBUG
656: if (syncprt)
657: vfs_bufstats();
658: #endif /* DEBUG */
1.425 dsl 659: }
660:
661: /* ARGSUSED */
662: int
663: sys_sync(struct lwp *l, const void *v, register_t *retval)
664: {
665: do_sys_sync(l);
1.31 cgd 666: return (0);
667: }
668:
1.425 dsl 669:
1.31 cgd 670: /*
1.444 dholland 671: * Access or change filesystem quotas.
672: *
673: * (this is really 14 different calls bundled into one)
1.31 cgd 674: */
1.444 dholland 675:
676: static int
677: do_sys_quotactl_stat(struct mount *mp, struct quotastat *info_u)
678: {
679: struct quotastat info_k;
680: int error;
681:
682: /* ensure any padding bytes are cleared */
683: memset(&info_k, 0, sizeof(info_k));
684:
685: error = vfs_quotactl_stat(mp, &info_k);
686: if (error) {
687: return error;
688: }
689:
690: return copyout(&info_k, info_u, sizeof(info_k));
691: }
692:
693: static int
694: do_sys_quotactl_idtypestat(struct mount *mp, int idtype,
695: struct quotaidtypestat *info_u)
696: {
697: struct quotaidtypestat info_k;
698: int error;
699:
700: /* ensure any padding bytes are cleared */
701: memset(&info_k, 0, sizeof(info_k));
702:
703: error = vfs_quotactl_idtypestat(mp, idtype, &info_k);
704: if (error) {
705: return error;
706: }
707:
708: return copyout(&info_k, info_u, sizeof(info_k));
709: }
710:
711: static int
712: do_sys_quotactl_objtypestat(struct mount *mp, int objtype,
713: struct quotaobjtypestat *info_u)
714: {
715: struct quotaobjtypestat info_k;
716: int error;
717:
718: /* ensure any padding bytes are cleared */
719: memset(&info_k, 0, sizeof(info_k));
720:
721: error = vfs_quotactl_objtypestat(mp, objtype, &info_k);
722: if (error) {
723: return error;
724: }
725:
726: return copyout(&info_k, info_u, sizeof(info_k));
727: }
728:
729: static int
730: do_sys_quotactl_get(struct mount *mp, const struct quotakey *key_u,
731: struct quotaval *val_u)
732: {
733: struct quotakey key_k;
734: struct quotaval val_k;
735: int error;
736:
737: /* ensure any padding bytes are cleared */
738: memset(&val_k, 0, sizeof(val_k));
739:
740: error = copyin(key_u, &key_k, sizeof(key_k));
741: if (error) {
742: return error;
743: }
744:
745: error = vfs_quotactl_get(mp, &key_k, &val_k);
746: if (error) {
747: return error;
748: }
749:
750: return copyout(&val_k, val_u, sizeof(val_k));
751: }
752:
753: static int
754: do_sys_quotactl_put(struct mount *mp, const struct quotakey *key_u,
755: const struct quotaval *val_u)
756: {
757: struct quotakey key_k;
758: struct quotaval val_k;
759: int error;
760:
761: error = copyin(key_u, &key_k, sizeof(key_k));
762: if (error) {
763: return error;
764: }
765:
766: error = copyin(val_u, &val_k, sizeof(val_k));
767: if (error) {
768: return error;
769: }
770:
771: return vfs_quotactl_put(mp, &key_k, &val_k);
772: }
773:
774: static int
1.486 dholland 775: do_sys_quotactl_del(struct mount *mp, const struct quotakey *key_u)
1.444 dholland 776: {
777: struct quotakey key_k;
778: int error;
779:
780: error = copyin(key_u, &key_k, sizeof(key_k));
781: if (error) {
782: return error;
783: }
784:
1.486 dholland 785: return vfs_quotactl_del(mp, &key_k);
1.444 dholland 786: }
787:
788: static int
789: do_sys_quotactl_cursoropen(struct mount *mp, struct quotakcursor *cursor_u)
790: {
791: struct quotakcursor cursor_k;
792: int error;
793:
794: /* ensure any padding bytes are cleared */
795: memset(&cursor_k, 0, sizeof(cursor_k));
796:
797: error = vfs_quotactl_cursoropen(mp, &cursor_k);
798: if (error) {
799: return error;
800: }
801:
802: return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
803: }
804:
805: static int
806: do_sys_quotactl_cursorclose(struct mount *mp, struct quotakcursor *cursor_u)
807: {
808: struct quotakcursor cursor_k;
809: int error;
810:
811: error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
812: if (error) {
813: return error;
814: }
815:
816: return vfs_quotactl_cursorclose(mp, &cursor_k);
817: }
818:
819: static int
820: do_sys_quotactl_cursorskipidtype(struct mount *mp,
821: struct quotakcursor *cursor_u, int idtype)
822: {
823: struct quotakcursor cursor_k;
824: int error;
825:
826: error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
827: if (error) {
828: return error;
829: }
830:
831: error = vfs_quotactl_cursorskipidtype(mp, &cursor_k, idtype);
832: if (error) {
833: return error;
834: }
835:
836: return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
837: }
838:
839: static int
840: do_sys_quotactl_cursorget(struct mount *mp, struct quotakcursor *cursor_u,
841: struct quotakey *keys_u, struct quotaval *vals_u, unsigned maxnum,
1.447 dholland 842: unsigned *ret_u)
1.444 dholland 843: {
844: #define CGET_STACK_MAX 8
845: struct quotakcursor cursor_k;
846: struct quotakey stackkeys[CGET_STACK_MAX];
847: struct quotaval stackvals[CGET_STACK_MAX];
848: struct quotakey *keys_k;
849: struct quotaval *vals_k;
1.447 dholland 850: unsigned ret_k;
1.444 dholland 851: int error;
852:
853: if (maxnum > 128) {
854: maxnum = 128;
855: }
856:
857: error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
858: if (error) {
859: return error;
860: }
861:
862: if (maxnum <= CGET_STACK_MAX) {
863: keys_k = stackkeys;
864: vals_k = stackvals;
865: /* ensure any padding bytes are cleared */
866: memset(keys_k, 0, maxnum * sizeof(keys_k[0]));
867: memset(vals_k, 0, maxnum * sizeof(vals_k[0]));
868: } else {
869: keys_k = kmem_zalloc(maxnum * sizeof(keys_k[0]), KM_SLEEP);
870: vals_k = kmem_zalloc(maxnum * sizeof(vals_k[0]), KM_SLEEP);
871: }
872:
873: error = vfs_quotactl_cursorget(mp, &cursor_k, keys_k, vals_k, maxnum,
874: &ret_k);
875: if (error) {
876: goto fail;
877: }
878:
879: error = copyout(keys_k, keys_u, ret_k * sizeof(keys_k[0]));
880: if (error) {
881: goto fail;
882: }
883:
884: error = copyout(vals_k, vals_u, ret_k * sizeof(vals_k[0]));
885: if (error) {
886: goto fail;
887: }
888:
889: error = copyout(&ret_k, ret_u, sizeof(ret_k));
890: if (error) {
891: goto fail;
892: }
893:
894: /* do last to maximize the chance of being able to recover a failure */
895: error = copyout(&cursor_k, cursor_u, sizeof(cursor_k));
896:
897: fail:
898: if (keys_k != stackkeys) {
899: kmem_free(keys_k, maxnum * sizeof(keys_k[0]));
900: }
901: if (vals_k != stackvals) {
902: kmem_free(vals_k, maxnum * sizeof(vals_k[0]));
903: }
904: return error;
905: }
906:
907: static int
908: do_sys_quotactl_cursoratend(struct mount *mp, struct quotakcursor *cursor_u,
909: int *ret_u)
910: {
911: struct quotakcursor cursor_k;
912: int ret_k;
913: int error;
914:
915: error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
916: if (error) {
917: return error;
918: }
919:
920: error = vfs_quotactl_cursoratend(mp, &cursor_k, &ret_k);
921: if (error) {
922: return error;
923: }
924:
925: error = copyout(&ret_k, ret_u, sizeof(ret_k));
926: if (error) {
927: return error;
928: }
929:
930: return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
931: }
932:
933: static int
934: do_sys_quotactl_cursorrewind(struct mount *mp, struct quotakcursor *cursor_u)
935: {
936: struct quotakcursor cursor_k;
937: int error;
938:
939: error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
940: if (error) {
941: return error;
942: }
943:
944: error = vfs_quotactl_cursorrewind(mp, &cursor_k);
945: if (error) {
946: return error;
947: }
948:
949: return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
950: }
951:
952: static int
953: do_sys_quotactl_quotaon(struct mount *mp, int idtype, const char *path_u)
954: {
955: char *path_k;
956: int error;
957:
958: /* XXX this should probably be a struct pathbuf */
959: path_k = PNBUF_GET();
960: error = copyin(path_u, path_k, PATH_MAX);
961: if (error) {
962: PNBUF_PUT(path_k);
963: return error;
964: }
965:
966: error = vfs_quotactl_quotaon(mp, idtype, path_k);
967:
968: PNBUF_PUT(path_k);
969: return error;
970: }
971:
972: static int
973: do_sys_quotactl_quotaoff(struct mount *mp, int idtype)
974: {
975: return vfs_quotactl_quotaoff(mp, idtype);
976: }
977:
1.63 christos 978: int
1.445 dholland 979: do_sys_quotactl(const char *path_u, const struct quotactl_args *args)
1.56 thorpej 980: {
1.155 augustss 981: struct mount *mp;
1.444 dholland 982: struct vnode *vp;
1.31 cgd 983: int error;
984:
1.445 dholland 985: error = namei_simple_user(path_u, NSM_FOLLOW_TRYEMULROOT, &vp);
1.395 dholland 986: if (error != 0)
1.31 cgd 987: return (error);
1.395 dholland 988: mp = vp->v_mount;
1.444 dholland 989:
1.445 dholland 990: switch (args->qc_op) {
1.444 dholland 991: case QUOTACTL_STAT:
1.446 dholland 992: error = do_sys_quotactl_stat(mp, args->u.stat.qc_info);
1.444 dholland 993: break;
994: case QUOTACTL_IDTYPESTAT:
995: error = do_sys_quotactl_idtypestat(mp,
1.445 dholland 996: args->u.idtypestat.qc_idtype,
997: args->u.idtypestat.qc_info);
1.444 dholland 998: break;
999: case QUOTACTL_OBJTYPESTAT:
1000: error = do_sys_quotactl_objtypestat(mp,
1.445 dholland 1001: args->u.objtypestat.qc_objtype,
1002: args->u.objtypestat.qc_info);
1.444 dholland 1003: break;
1004: case QUOTACTL_GET:
1005: error = do_sys_quotactl_get(mp,
1.445 dholland 1006: args->u.get.qc_key,
1.446 dholland 1007: args->u.get.qc_val);
1.444 dholland 1008: break;
1009: case QUOTACTL_PUT:
1010: error = do_sys_quotactl_put(mp,
1.445 dholland 1011: args->u.put.qc_key,
1012: args->u.put.qc_val);
1.444 dholland 1013: break;
1.486 dholland 1014: case QUOTACTL_DEL:
1015: error = do_sys_quotactl_del(mp, args->u.del.qc_key);
1.444 dholland 1016: break;
1017: case QUOTACTL_CURSOROPEN:
1018: error = do_sys_quotactl_cursoropen(mp,
1.445 dholland 1019: args->u.cursoropen.qc_cursor);
1.444 dholland 1020: break;
1021: case QUOTACTL_CURSORCLOSE:
1022: error = do_sys_quotactl_cursorclose(mp,
1.445 dholland 1023: args->u.cursorclose.qc_cursor);
1.444 dholland 1024: break;
1025: case QUOTACTL_CURSORSKIPIDTYPE:
1026: error = do_sys_quotactl_cursorskipidtype(mp,
1.445 dholland 1027: args->u.cursorskipidtype.qc_cursor,
1028: args->u.cursorskipidtype.qc_idtype);
1.444 dholland 1029: break;
1030: case QUOTACTL_CURSORGET:
1031: error = do_sys_quotactl_cursorget(mp,
1.445 dholland 1032: args->u.cursorget.qc_cursor,
1033: args->u.cursorget.qc_keys,
1034: args->u.cursorget.qc_vals,
1035: args->u.cursorget.qc_maxnum,
1036: args->u.cursorget.qc_ret);
1.444 dholland 1037: break;
1038: case QUOTACTL_CURSORATEND:
1039: error = do_sys_quotactl_cursoratend(mp,
1.445 dholland 1040: args->u.cursoratend.qc_cursor,
1041: args->u.cursoratend.qc_ret);
1.444 dholland 1042: break;
1043: case QUOTACTL_CURSORREWIND:
1044: error = do_sys_quotactl_cursorrewind(mp,
1.445 dholland 1045: args->u.cursorrewind.qc_cursor);
1.444 dholland 1046: break;
1047: case QUOTACTL_QUOTAON:
1048: error = do_sys_quotactl_quotaon(mp,
1.445 dholland 1049: args->u.quotaon.qc_idtype,
1050: args->u.quotaon.qc_quotafile);
1.444 dholland 1051: break;
1052: case QUOTACTL_QUOTAOFF:
1053: error = do_sys_quotactl_quotaoff(mp,
1.445 dholland 1054: args->u.quotaoff.qc_idtype);
1.444 dholland 1055: break;
1056: default:
1057: error = EINVAL;
1058: break;
1059: }
1060:
1.395 dholland 1061: vrele(vp);
1.444 dholland 1062: return error;
1.31 cgd 1063: }
1064:
1.445 dholland 1065: /* ARGSUSED */
1066: int
1067: sys___quotactl(struct lwp *l, const struct sys___quotactl_args *uap,
1068: register_t *retval)
1069: {
1070: /* {
1071: syscallarg(const char *) path;
1072: syscallarg(struct quotactl_args *) args;
1073: } */
1074: struct quotactl_args args;
1075: int error;
1076:
1077: error = copyin(SCARG(uap, args), &args, sizeof(args));
1078: if (error) {
1079: return error;
1080: }
1081:
1082: return do_sys_quotactl(SCARG(uap, path), &args);
1083: }
1084:
1.206 christos 1085: int
1.234 christos 1086: dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
1.185 christos 1087: int root)
1088: {
1.234 christos 1089: struct cwdinfo *cwdi = l->l_proc->p_cwdi;
1.185 christos 1090: int error = 0;
1091:
1092: /*
1093: * If MNT_NOWAIT or MNT_LAZY is specified, do not
1.204 dbj 1094: * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
1.185 christos 1095: * overrides MNT_NOWAIT.
1096: */
1097: if (flags == MNT_NOWAIT || flags == MNT_LAZY ||
1098: (flags != MNT_WAIT && flags != 0)) {
1099: memcpy(sp, &mp->mnt_stat, sizeof(*sp));
1100: goto done;
1101: }
1.205 junyoung 1102:
1.211 jdolecek 1103: /* Get the filesystem stats now */
1104: memset(sp, 0, sizeof(*sp));
1.332 pooka 1105: if ((error = VFS_STATVFS(mp, sp)) != 0) {
1.185 christos 1106: return error;
1107: }
1108:
1109: if (cwdi->cwdi_rdir == NULL)
1110: (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
1111: done:
1112: if (cwdi->cwdi_rdir != NULL) {
1113: size_t len;
1114: char *bp;
1.364 christos 1115: char c;
1.236 yamt 1116: char *path = PNBUF_GET();
1.185 christos 1117:
1118: bp = path + MAXPATHLEN;
1119: *--bp = '\0';
1.328 ad 1120: rw_enter(&cwdi->cwdi_lock, RW_READER);
1.185 christos 1121: error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
1.234 christos 1122: MAXPATHLEN / 2, 0, l);
1.328 ad 1123: rw_exit(&cwdi->cwdi_lock);
1.185 christos 1124: if (error) {
1.236 yamt 1125: PNBUF_PUT(path);
1.185 christos 1126: return error;
1127: }
1128: len = strlen(bp);
1.387 christos 1129: if (len != 1) {
1130: /*
1131: * for mount points that are below our root, we can see
1132: * them, so we fix up the pathname and return them. The
1133: * rest we cannot see, so we don't allow viewing the
1134: * data.
1135: */
1136: if (strncmp(bp, sp->f_mntonname, len) == 0 &&
1137: ((c = sp->f_mntonname[len]) == '/' || c == '\0')) {
1138: (void)strlcpy(sp->f_mntonname,
1.388 enami 1139: c == '\0' ? "/" : &sp->f_mntonname[len],
1.187 itojun 1140: sizeof(sp->f_mntonname));
1.387 christos 1141: } else {
1142: if (root)
1143: (void)strlcpy(sp->f_mntonname, "/",
1144: sizeof(sp->f_mntonname));
1145: else
1146: error = EPERM;
1147: }
1.185 christos 1148: }
1.236 yamt 1149: PNBUF_PUT(path);
1.185 christos 1150: }
1.206 christos 1151: sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1.185 christos 1152: return error;
1153: }
1154:
1.31 cgd 1155: /*
1.311 dsl 1156: * Get filesystem statistics by path.
1.31 cgd 1157: */
1.311 dsl 1158: int
1159: do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
1160: {
1161: struct mount *mp;
1162: int error;
1.395 dholland 1163: struct vnode *vp;
1.311 dsl 1164:
1.395 dholland 1165: error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
1166: if (error != 0)
1.311 dsl 1167: return error;
1.395 dholland 1168: mp = vp->v_mount;
1.311 dsl 1169: error = dostatvfs(mp, sb, l, flags, 1);
1.395 dholland 1170: vrele(vp);
1.311 dsl 1171: return error;
1172: }
1173:
1.31 cgd 1174: /* ARGSUSED */
1.63 christos 1175: int
1.335 dsl 1176: sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval)
1.56 thorpej 1177: {
1.335 dsl 1178: /* {
1.74 cgd 1179: syscallarg(const char *) path;
1.206 christos 1180: syscallarg(struct statvfs *) buf;
1181: syscallarg(int) flags;
1.335 dsl 1182: } */
1.241 yamt 1183: struct statvfs *sb;
1.31 cgd 1184: int error;
1185:
1.241 yamt 1186: sb = STATVFSBUF_GET();
1.311 dsl 1187: error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
1188: if (error == 0)
1.241 yamt 1189: error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1190: STATVFSBUF_PUT(sb);
1191: return error;
1.31 cgd 1192: }
1193:
1194: /*
1.311 dsl 1195: * Get filesystem statistics by fd.
1.31 cgd 1196: */
1.311 dsl 1197: int
1198: do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
1199: {
1.346 ad 1200: file_t *fp;
1.311 dsl 1201: struct mount *mp;
1202: int error;
1203:
1.346 ad 1204: /* fd_getvnode() will use the descriptor for us */
1205: if ((error = fd_getvnode(fd, &fp)) != 0)
1.311 dsl 1206: return (error);
1.491 matt 1207: mp = fp->f_vnode->v_mount;
1.346 ad 1208: error = dostatvfs(mp, sb, curlwp, flags, 1);
1209: fd_putfile(fd);
1.311 dsl 1210: return error;
1211: }
1212:
1.31 cgd 1213: /* ARGSUSED */
1.63 christos 1214: int
1.335 dsl 1215: sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval)
1.56 thorpej 1216: {
1.335 dsl 1217: /* {
1.35 cgd 1218: syscallarg(int) fd;
1.206 christos 1219: syscallarg(struct statvfs *) buf;
1220: syscallarg(int) flags;
1.335 dsl 1221: } */
1.241 yamt 1222: struct statvfs *sb;
1.31 cgd 1223: int error;
1224:
1.241 yamt 1225: sb = STATVFSBUF_GET();
1.311 dsl 1226: error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
1.316 dsl 1227: if (error == 0)
1.311 dsl 1228: error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1.241 yamt 1229: STATVFSBUF_PUT(sb);
1.185 christos 1230: return error;
1.31 cgd 1231: }
1232:
1.185 christos 1233:
1.31 cgd 1234: /*
1235: * Get statistics on all filesystems.
1236: */
1.63 christos 1237: int
1.311 dsl 1238: do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
1239: int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
1240: register_t *retval)
1.56 thorpej 1241: {
1.185 christos 1242: int root = 0;
1.510 hannken 1243: mount_iterator_t *iter;
1.179 thorpej 1244: struct proc *p = l->l_proc;
1.510 hannken 1245: struct mount *mp;
1.241 yamt 1246: struct statvfs *sb;
1.206 christos 1247: size_t count, maxcount;
1248: int error = 0;
1.31 cgd 1249:
1.241 yamt 1250: sb = STATVFSBUF_GET();
1.311 dsl 1251: maxcount = bufsize / entry_sz;
1.113 fvdl 1252: count = 0;
1.510 hannken 1253: mountlist_iterator_init(&iter);
1254: while ((mp = mountlist_iterator_next(iter)) != NULL) {
1.113 fvdl 1255: if (sfsp && count < maxcount) {
1.311 dsl 1256: error = dostatvfs(mp, sb, l, flags, 0);
1.185 christos 1257: if (error) {
1.365 christos 1258: error = 0;
1.31 cgd 1259: continue;
1.113 fvdl 1260: }
1.311 dsl 1261: error = copyfn(sb, sfsp, entry_sz);
1.510 hannken 1262: if (error)
1.241 yamt 1263: goto out;
1.311 dsl 1264: sfsp = (char *)sfsp + entry_sz;
1.241 yamt 1265: root |= strcmp(sb->f_mntonname, "/") == 0;
1.31 cgd 1266: }
1267: count++;
1268: }
1.331 pooka 1269:
1.185 christos 1270: if (root == 0 && p->p_cwdi->cwdi_rdir) {
1271: /*
1272: * fake a root entry
1273: */
1.331 pooka 1274: error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
1275: sb, l, flags, 1);
1.311 dsl 1276: if (error != 0)
1.241 yamt 1277: goto out;
1.365 christos 1278: if (sfsp) {
1.311 dsl 1279: error = copyfn(sb, sfsp, entry_sz);
1.365 christos 1280: if (error != 0)
1281: goto out;
1282: }
1.185 christos 1283: count++;
1284: }
1.31 cgd 1285: if (sfsp && count > maxcount)
1286: *retval = maxcount;
1287: else
1288: *retval = count;
1.241 yamt 1289: out:
1.510 hannken 1290: mountlist_iterator_destroy(iter);
1.241 yamt 1291: STATVFSBUF_PUT(sb);
1.185 christos 1292: return error;
1.31 cgd 1293: }
1294:
1.311 dsl 1295: int
1.335 dsl 1296: sys_getvfsstat(struct lwp *l, const struct sys_getvfsstat_args *uap, register_t *retval)
1.311 dsl 1297: {
1.335 dsl 1298: /* {
1.311 dsl 1299: syscallarg(struct statvfs *) buf;
1300: syscallarg(size_t) bufsize;
1301: syscallarg(int) flags;
1.335 dsl 1302: } */
1.311 dsl 1303:
1304: return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
1305: SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
1306: }
1307:
1.31 cgd 1308: /*
1309: * Change current working directory to a given file descriptor.
1310: */
1311: /* ARGSUSED */
1.63 christos 1312: int
1.335 dsl 1313: sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
1.56 thorpej 1314: {
1.335 dsl 1315: /* {
1.35 cgd 1316: syscallarg(int) fd;
1.335 dsl 1317: } */
1.179 thorpej 1318: struct proc *p = l->l_proc;
1.328 ad 1319: struct cwdinfo *cwdi;
1.43 mycroft 1320: struct vnode *vp, *tdp;
1321: struct mount *mp;
1.346 ad 1322: file_t *fp;
1323: int error, fd;
1.31 cgd 1324:
1.346 ad 1325: /* fd_getvnode() will use the descriptor for us */
1326: fd = SCARG(uap, fd);
1327: if ((error = fd_getvnode(fd, &fp)) != 0)
1.31 cgd 1328: return (error);
1.491 matt 1329: vp = fp->f_vnode;
1.131 sommerfe 1330:
1.402 pooka 1331: vref(vp);
1.113 fvdl 1332: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.31 cgd 1333: if (vp->v_type != VDIR)
1334: error = ENOTDIR;
1335: else
1.332 pooka 1336: error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1.303 pooka 1337: if (error) {
1338: vput(vp);
1339: goto out;
1340: }
1.304 pooka 1341: while ((mp = vp->v_mountedhere) != NULL) {
1.512 hannken 1342: error = vfs_busy(mp);
1.298 chs 1343: vput(vp);
1.357 xtraeme 1344: if (error != 0)
1.356 ad 1345: goto out;
1.189 thorpej 1346: error = VFS_ROOT(mp, &tdp);
1.512 hannken 1347: vfs_unbusy(mp);
1.113 fvdl 1348: if (error)
1.303 pooka 1349: goto out;
1.43 mycroft 1350: vp = tdp;
1351: }
1.406 hannken 1352: VOP_UNLOCK(vp);
1.131 sommerfe 1353:
1354: /*
1355: * Disallow changing to a directory not under the process's
1356: * current root directory (if there is one).
1357: */
1.328 ad 1358: cwdi = p->p_cwdi;
1359: rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1.234 christos 1360: if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
1.131 sommerfe 1361: vrele(vp);
1.135 thorpej 1362: error = EPERM; /* operation not permitted */
1.328 ad 1363: } else {
1364: vrele(cwdi->cwdi_cdir);
1365: cwdi->cwdi_cdir = vp;
1.131 sommerfe 1366: }
1.328 ad 1367: rw_exit(&cwdi->cwdi_lock);
1.205 junyoung 1368:
1.135 thorpej 1369: out:
1.346 ad 1370: fd_putfile(fd);
1.135 thorpej 1371: return (error);
1.31 cgd 1372: }
1373:
1374: /*
1.221 thorpej 1375: * Change this process's notion of the root directory to a given file
1376: * descriptor.
1.131 sommerfe 1377: */
1378: int
1.335 dsl 1379: sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval)
1.131 sommerfe 1380: {
1.179 thorpej 1381: struct proc *p = l->l_proc;
1.131 sommerfe 1382: struct vnode *vp;
1.346 ad 1383: file_t *fp;
1384: int error, fd = SCARG(uap, fd);
1.131 sommerfe 1385:
1.268 elad 1386: if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1387: KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
1.131 sommerfe 1388: return error;
1.346 ad 1389: /* fd_getvnode() will use the descriptor for us */
1.397 bad 1390: if ((error = fd_getvnode(fd, &fp)) != 0)
1.131 sommerfe 1391: return error;
1.491 matt 1392: vp = fp->f_vnode;
1.131 sommerfe 1393: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1394: if (vp->v_type != VDIR)
1395: error = ENOTDIR;
1396: else
1.332 pooka 1397: error = VOP_ACCESS(vp, VEXEC, l->l_cred);
1.406 hannken 1398: VOP_UNLOCK(vp);
1.131 sommerfe 1399: if (error)
1.135 thorpej 1400: goto out;
1.402 pooka 1401: vref(vp);
1.131 sommerfe 1402:
1.397 bad 1403: change_root(p->p_cwdi, vp, l);
1.328 ad 1404:
1.135 thorpej 1405: out:
1.346 ad 1406: fd_putfile(fd);
1.135 thorpej 1407: return (error);
1.131 sommerfe 1408: }
1409:
1410: /*
1.31 cgd 1411: * Change current working directory (``.'').
1412: */
1413: /* ARGSUSED */
1.63 christos 1414: int
1.335 dsl 1415: sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
1.56 thorpej 1416: {
1.335 dsl 1417: /* {
1.74 cgd 1418: syscallarg(const char *) path;
1.335 dsl 1419: } */
1.179 thorpej 1420: struct proc *p = l->l_proc;
1.328 ad 1421: struct cwdinfo *cwdi;
1.31 cgd 1422: int error;
1.397 bad 1423: struct vnode *vp;
1.31 cgd 1424:
1.397 bad 1425: if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
1426: &vp, l)) != 0)
1.31 cgd 1427: return (error);
1.328 ad 1428: cwdi = p->p_cwdi;
1429: rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1.134 thorpej 1430: vrele(cwdi->cwdi_cdir);
1.397 bad 1431: cwdi->cwdi_cdir = vp;
1.328 ad 1432: rw_exit(&cwdi->cwdi_lock);
1.31 cgd 1433: return (0);
1434: }
1435:
1436: /*
1437: * Change notion of root (``/'') directory.
1438: */
1439: /* ARGSUSED */
1.63 christos 1440: int
1.335 dsl 1441: sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval)
1.56 thorpej 1442: {
1.335 dsl 1443: /* {
1.74 cgd 1444: syscallarg(const char *) path;
1.335 dsl 1445: } */
1.179 thorpej 1446: struct proc *p = l->l_proc;
1.397 bad 1447: int error;
1.131 sommerfe 1448: struct vnode *vp;
1.31 cgd 1449:
1.268 elad 1450: if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1451: KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
1.31 cgd 1452: return (error);
1.397 bad 1453: if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
1454: &vp, l)) != 0)
1.31 cgd 1455: return (error);
1.328 ad 1456:
1.397 bad 1457: change_root(p->p_cwdi, vp, l);
1458:
1459: return (0);
1460: }
1461:
1462: /*
1463: * Common routine for chroot and fchroot.
1.398 bad 1464: * NB: callers need to properly authorize the change root operation.
1.397 bad 1465: */
1466: void
1467: change_root(struct cwdinfo *cwdi, struct vnode *vp, struct lwp *l)
1468: {
1.457 cheusov 1469: struct proc *p = l->l_proc;
1470: kauth_cred_t ncred;
1471:
1472: ncred = kauth_cred_alloc();
1.397 bad 1473:
1.328 ad 1474: rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1.134 thorpej 1475: if (cwdi->cwdi_rdir != NULL)
1476: vrele(cwdi->cwdi_rdir);
1477: cwdi->cwdi_rdir = vp;
1.131 sommerfe 1478:
1479: /*
1480: * Prevent escaping from chroot by putting the root under
1481: * the working directory. Silently chdir to / if we aren't
1482: * already there.
1483: */
1.234 christos 1484: if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1.131 sommerfe 1485: /*
1486: * XXX would be more failsafe to change directory to a
1487: * deadfs node here instead
1488: */
1.134 thorpej 1489: vrele(cwdi->cwdi_cdir);
1.402 pooka 1490: vref(vp);
1.134 thorpej 1491: cwdi->cwdi_cdir = vp;
1.131 sommerfe 1492: }
1.328 ad 1493: rw_exit(&cwdi->cwdi_lock);
1.457 cheusov 1494:
1495: /* Get a write lock on the process credential. */
1496: proc_crmod_enter();
1497:
1498: kauth_cred_clone(p->p_cred, ncred);
1499: kauth_proc_chroot(ncred, p->p_cwdi);
1500:
1501: /* Broadcast our credentials to the process and other LWPs. */
1502: proc_crmod_leave(ncred, p->p_cred, true);
1.31 cgd 1503: }
1504:
1505: /*
1506: * Common routine for chroot and chdir.
1.409 dholland 1507: * XXX "where" should be enum uio_seg
1.31 cgd 1508: */
1.397 bad 1509: int
1510: chdir_lookup(const char *path, int where, struct vnode **vpp, struct lwp *l)
1.31 cgd 1511: {
1.409 dholland 1512: struct pathbuf *pb;
1.397 bad 1513: struct nameidata nd;
1.31 cgd 1514: int error;
1515:
1.409 dholland 1516: error = pathbuf_maybe_copyin(path, where, &pb);
1517: if (error) {
1518: return error;
1519: }
1520: NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1521: if ((error = namei(&nd)) != 0) {
1522: pathbuf_destroy(pb);
1523: return error;
1524: }
1.397 bad 1525: *vpp = nd.ni_vp;
1.409 dholland 1526: pathbuf_destroy(pb);
1527:
1.397 bad 1528: if ((*vpp)->v_type != VDIR)
1.31 cgd 1529: error = ENOTDIR;
1530: else
1.397 bad 1531: error = VOP_ACCESS(*vpp, VEXEC, l->l_cred);
1.205 junyoung 1532:
1.31 cgd 1533: if (error)
1.397 bad 1534: vput(*vpp);
1.113 fvdl 1535: else
1.406 hannken 1536: VOP_UNLOCK(*vpp);
1.31 cgd 1537: return (error);
1538: }
1539:
1540: /*
1.448 martin 1541: * Internals of sys_open - path has already been converted into a pathbuf
1542: * (so we can easily reuse this function from other parts of the kernel,
1543: * like posix_spawn post-processing).
1.31 cgd 1544: */
1.474 christos 1545: int
1.460 manu 1546: do_open(lwp_t *l, struct vnode *dvp, struct pathbuf *pb, int open_flags,
1547: int open_mode, int *fd)
1.56 thorpej 1548: {
1.179 thorpej 1549: struct proc *p = l->l_proc;
1.134 thorpej 1550: struct cwdinfo *cwdi = p->p_cwdi;
1.346 ad 1551: file_t *fp;
1.135 thorpej 1552: struct vnode *vp;
1.31 cgd 1553: int flags, cmode;
1.422 christos 1554: int indx, error;
1.31 cgd 1555: struct nameidata nd;
1556:
1.463 dholland 1557: if (open_flags & O_SEARCH) {
1558: open_flags &= ~(int)O_SEARCH;
1559: }
1560:
1.448 martin 1561: flags = FFLAGS(open_flags);
1.104 mycroft 1562: if ((flags & (FREAD | FWRITE)) == 0)
1.448 martin 1563: return EINVAL;
1.409 dholland 1564:
1565: if ((error = fd_allocfile(&fp, &indx)) != 0) {
1566: return error;
1567: }
1.455 rmind 1568:
1.328 ad 1569: /* We're going to read cwdi->cwdi_cmask unlocked here. */
1.448 martin 1570: cmode = ((open_mode &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1.409 dholland 1571: NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, pb);
1.460 manu 1572: if (dvp != NULL)
1573: NDAT(&nd, dvp);
1574:
1.194 jdolecek 1575: l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1.63 christos 1576: if ((error = vn_open(&nd, flags, cmode)) != 0) {
1.346 ad 1577: fd_abort(p, fp, indx);
1.213 christos 1578: if ((error == EDUPFD || error == EMOVEFD) &&
1.194 jdolecek 1579: l->l_dupfd >= 0 && /* XXX from fdopen */
1.45 mycroft 1580: (error =
1.346 ad 1581: fd_dupopen(l->l_dupfd, &indx, flags, error)) == 0) {
1.448 martin 1582: *fd = indx;
1.455 rmind 1583: return 0;
1.45 mycroft 1584: }
1585: if (error == ERESTART)
1.44 mycroft 1586: error = EINTR;
1.448 martin 1587: return error;
1.31 cgd 1588: }
1.331 pooka 1589:
1.194 jdolecek 1590: l->l_dupfd = 0;
1.31 cgd 1591: vp = nd.ni_vp;
1.409 dholland 1592:
1.422 christos 1593: if ((error = open_setfp(l, fp, vp, indx, flags)))
1594: return error;
1595:
1.406 hannken 1596: VOP_UNLOCK(vp);
1.448 martin 1597: *fd = indx;
1.346 ad 1598: fd_affix(p, fp, indx);
1.448 martin 1599: return 0;
1600: }
1601:
1602: int
1603: fd_open(const char *path, int open_flags, int open_mode, int *fd)
1604: {
1605: struct pathbuf *pb;
1.455 rmind 1606: int error, oflags;
1.448 martin 1607:
1.449 martin 1608: oflags = FFLAGS(open_flags);
1609: if ((oflags & (FREAD | FWRITE)) == 0)
1.448 martin 1610: return EINVAL;
1611:
1612: pb = pathbuf_create(path);
1613: if (pb == NULL)
1614: return ENOMEM;
1615:
1.460 manu 1616: error = do_open(curlwp, NULL, pb, open_flags, open_mode, fd);
1.455 rmind 1617: pathbuf_destroy(pb);
1618:
1619: return error;
1.448 martin 1620: }
1621:
1622: /*
1623: * Check permissions, allocate an open file structure,
1624: * and call the device open routine if any.
1625: */
1.460 manu 1626: static int
1627: do_sys_openat(lwp_t *l, int fdat, const char *path, int flags,
1628: int mode, int *fd)
1629: {
1630: file_t *dfp = NULL;
1631: struct vnode *dvp = NULL;
1632: struct pathbuf *pb;
1633: int error;
1634:
1.475 christos 1635: #ifdef COMPAT_10 /* XXX: and perhaps later */
1.477 maxv 1636: if (path == NULL) {
1.475 christos 1637: pb = pathbuf_create(".");
1.477 maxv 1638: if (pb == NULL)
1639: return ENOMEM;
1640: } else
1.475 christos 1641: #endif
1642: {
1643: error = pathbuf_copyin(path, &pb);
1644: if (error)
1645: return error;
1646: }
1.460 manu 1647:
1648: if (fdat != AT_FDCWD) {
1649: /* fd_getvnode() will use the descriptor for us */
1650: if ((error = fd_getvnode(fdat, &dfp)) != 0)
1651: goto out;
1652:
1.491 matt 1653: dvp = dfp->f_vnode;
1.460 manu 1654: }
1655:
1656: error = do_open(l, dvp, pb, flags, mode, fd);
1657:
1658: if (dfp != NULL)
1659: fd_putfile(fdat);
1660: out:
1661: pathbuf_destroy(pb);
1662: return error;
1663: }
1664:
1.448 martin 1665: int
1666: sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval)
1667: {
1668: /* {
1669: syscallarg(const char *) path;
1670: syscallarg(int) flags;
1671: syscallarg(int) mode;
1672: } */
1.460 manu 1673: int error;
1674: int fd;
1.448 martin 1675:
1.460 manu 1676: error = do_sys_openat(l, AT_FDCWD, SCARG(uap, path),
1677: SCARG(uap, flags), SCARG(uap, mode), &fd);
1.448 martin 1678:
1.460 manu 1679: if (error == 0)
1680: *retval = fd;
1.448 martin 1681:
1.455 rmind 1682: return error;
1.137 wrstuden 1683: }
1684:
1.433 manu 1685: int
1686: sys_openat(struct lwp *l, const struct sys_openat_args *uap, register_t *retval)
1687: {
1688: /* {
1689: syscallarg(int) fd;
1690: syscallarg(const char *) path;
1.460 manu 1691: syscallarg(int) oflags;
1.433 manu 1692: syscallarg(int) mode;
1693: } */
1.460 manu 1694: int error;
1695: int fd;
1696:
1697: error = do_sys_openat(l, SCARG(uap, fd), SCARG(uap, path),
1698: SCARG(uap, oflags), SCARG(uap, mode), &fd);
1699:
1700: if (error == 0)
1701: *retval = fd;
1.433 manu 1702:
1.460 manu 1703: return error;
1.433 manu 1704: }
1705:
1.249 yamt 1706: static void
1707: vfs__fhfree(fhandle_t *fhp)
1708: {
1709: size_t fhsize;
1710:
1711: fhsize = FHANDLE_SIZE(fhp);
1712: kmem_free(fhp, fhsize);
1713: }
1714:
1.137 wrstuden 1715: /*
1.243 yamt 1716: * vfs_composefh: compose a filehandle.
1717: */
1718:
1719: int
1.244 martin 1720: vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
1.243 yamt 1721: {
1722: struct mount *mp;
1.250 yamt 1723: struct fid *fidp;
1.243 yamt 1724: int error;
1.250 yamt 1725: size_t needfhsize;
1726: size_t fidsize;
1.243 yamt 1727:
1728: mp = vp->v_mount;
1.250 yamt 1729: fidp = NULL;
1.252 martin 1730: if (*fh_size < FHANDLE_SIZE_MIN) {
1.250 yamt 1731: fidsize = 0;
1.244 martin 1732: } else {
1.250 yamt 1733: fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1734: if (fhp != NULL) {
1735: memset(fhp, 0, *fh_size);
1736: fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1737: fidp = &fhp->fh_fid;
1738: }
1.244 martin 1739: }
1.250 yamt 1740: error = VFS_VPTOFH(vp, fidp, &fidsize);
1741: needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1742: if (error == 0 && *fh_size < needfhsize) {
1743: error = E2BIG;
1744: }
1745: *fh_size = needfhsize;
1.243 yamt 1746: return error;
1747: }
1748:
1.249 yamt 1749: int
1750: vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1751: {
1752: struct mount *mp;
1753: fhandle_t *fhp;
1754: size_t fhsize;
1755: size_t fidsize;
1756: int error;
1757:
1758: mp = vp->v_mount;
1.255 christos 1759: fidsize = 0;
1.249 yamt 1760: error = VFS_VPTOFH(vp, NULL, &fidsize);
1761: KASSERT(error != 0);
1762: if (error != E2BIG) {
1763: goto out;
1764: }
1.250 yamt 1765: fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1.249 yamt 1766: fhp = kmem_zalloc(fhsize, KM_SLEEP);
1767: if (fhp == NULL) {
1768: error = ENOMEM;
1769: goto out;
1770: }
1771: fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1772: error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1773: if (error == 0) {
1774: KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1775: FHANDLE_FILEID(fhp)->fid_len == fidsize));
1776: *fhpp = fhp;
1777: } else {
1778: kmem_free(fhp, fhsize);
1779: }
1780: out:
1781: return error;
1782: }
1783:
1784: void
1785: vfs_composefh_free(fhandle_t *fhp)
1786: {
1787:
1788: vfs__fhfree(fhp);
1789: }
1790:
1.243 yamt 1791: /*
1.248 yamt 1792: * vfs_fhtovp: lookup a vnode by a filehandle.
1793: */
1794:
1795: int
1796: vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1797: {
1798: struct mount *mp;
1799: int error;
1800:
1801: *vpp = NULL;
1802: mp = vfs_getvfs(FHANDLE_FSID(fhp));
1803: if (mp == NULL) {
1804: error = ESTALE;
1805: goto out;
1806: }
1807: if (mp->mnt_op->vfs_fhtovp == NULL) {
1808: error = EOPNOTSUPP;
1809: goto out;
1810: }
1811: error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1812: out:
1813: return error;
1814: }
1815:
1816: /*
1.265 yamt 1817: * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1.263 martin 1818: * the needed size.
1819: */
1820:
1821: int
1.265 yamt 1822: vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1.263 martin 1823: {
1824: fhandle_t *fhp;
1825: int error;
1826:
1.250 yamt 1827: if (fhsize > FHANDLE_SIZE_MAX) {
1828: return EINVAL;
1829: }
1.265 yamt 1830: if (fhsize < FHANDLE_SIZE_MIN) {
1831: return EINVAL;
1832: }
1.267 yamt 1833: again:
1.248 yamt 1834: fhp = kmem_alloc(fhsize, KM_SLEEP);
1835: if (fhp == NULL) {
1836: return ENOMEM;
1837: }
1838: error = copyin(ufhp, fhp, fhsize);
1839: if (error == 0) {
1.265 yamt 1840: /* XXX this check shouldn't be here */
1841: if (FHANDLE_SIZE(fhp) == fhsize) {
1.263 martin 1842: *fhpp = fhp;
1843: return 0;
1.267 yamt 1844: } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1845: /*
1846: * a kludge for nfsv2 padded handles.
1847: */
1848: size_t sz;
1849:
1850: sz = FHANDLE_SIZE(fhp);
1851: kmem_free(fhp, fhsize);
1852: fhsize = sz;
1853: goto again;
1.264 yamt 1854: } else {
1.265 yamt 1855: /*
1856: * userland told us wrong size.
1857: */
1.263 martin 1858: error = EINVAL;
1.264 yamt 1859: }
1.248 yamt 1860: }
1.263 martin 1861: kmem_free(fhp, fhsize);
1.248 yamt 1862: return error;
1863: }
1864:
1865: void
1866: vfs_copyinfh_free(fhandle_t *fhp)
1867: {
1868:
1.249 yamt 1869: vfs__fhfree(fhp);
1.248 yamt 1870: }
1871:
1872: /*
1.137 wrstuden 1873: * Get file handle system call
1874: */
1875: int
1.335 dsl 1876: sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval)
1.137 wrstuden 1877: {
1.335 dsl 1878: /* {
1.137 wrstuden 1879: syscallarg(char *) fname;
1880: syscallarg(fhandle_t *) fhp;
1.244 martin 1881: syscallarg(size_t *) fh_size;
1.335 dsl 1882: } */
1.155 augustss 1883: struct vnode *vp;
1.244 martin 1884: fhandle_t *fh;
1.137 wrstuden 1885: int error;
1.409 dholland 1886: struct pathbuf *pb;
1.137 wrstuden 1887: struct nameidata nd;
1.244 martin 1888: size_t sz;
1.249 yamt 1889: size_t usz;
1.137 wrstuden 1890:
1891: /*
1892: * Must be super user
1893: */
1.268 elad 1894: error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1895: 0, NULL, NULL, NULL);
1.137 wrstuden 1896: if (error)
1897: return (error);
1.409 dholland 1898:
1899: error = pathbuf_copyin(SCARG(uap, fname), &pb);
1900: if (error) {
1901: return error;
1902: }
1903: NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1.137 wrstuden 1904: error = namei(&nd);
1.409 dholland 1905: if (error) {
1906: pathbuf_destroy(pb);
1907: return error;
1908: }
1.137 wrstuden 1909: vp = nd.ni_vp;
1.409 dholland 1910: pathbuf_destroy(pb);
1911:
1.249 yamt 1912: error = vfs_composefh_alloc(vp, &fh);
1.247 yamt 1913: vput(vp);
1.249 yamt 1914: if (error != 0) {
1.485 christos 1915: return error;
1.249 yamt 1916: }
1917: error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1918: if (error != 0) {
1919: goto out;
1920: }
1921: sz = FHANDLE_SIZE(fh);
1922: error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1923: if (error != 0) {
1924: goto out;
1.244 martin 1925: }
1.249 yamt 1926: if (usz >= sz) {
1927: error = copyout(fh, SCARG(uap, fhp), sz);
1928: } else {
1929: error = E2BIG;
1.244 martin 1930: }
1.249 yamt 1931: out:
1932: vfs_composefh_free(fh);
1.137 wrstuden 1933: return (error);
1934: }
1935:
1936: /*
1937: * Open a file given a file handle.
1938: *
1939: * Check permissions, allocate an open file structure,
1940: * and call the device open routine if any.
1941: */
1.265 yamt 1942:
1.137 wrstuden 1943: int
1.265 yamt 1944: dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1945: register_t *retval)
1.137 wrstuden 1946: {
1.346 ad 1947: file_t *fp;
1.139 wrstuden 1948: struct vnode *vp = NULL;
1.257 ad 1949: kauth_cred_t cred = l->l_cred;
1.346 ad 1950: file_t *nfp;
1.500 maxv 1951: int indx, error;
1.137 wrstuden 1952: struct vattr va;
1.248 yamt 1953: fhandle_t *fh;
1.265 yamt 1954: int flags;
1.346 ad 1955: proc_t *p;
1956:
1957: p = curproc;
1.137 wrstuden 1958:
1959: /*
1960: * Must be super user
1961: */
1.269 elad 1962: if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1.270 elad 1963: 0, NULL, NULL, NULL)))
1.137 wrstuden 1964: return (error);
1965:
1.463 dholland 1966: if (oflags & O_SEARCH) {
1967: oflags &= ~(int)O_SEARCH;
1968: }
1969:
1.265 yamt 1970: flags = FFLAGS(oflags);
1.137 wrstuden 1971: if ((flags & (FREAD | FWRITE)) == 0)
1972: return (EINVAL);
1973: if ((flags & O_CREAT))
1974: return (EINVAL);
1.346 ad 1975: if ((error = fd_allocfile(&nfp, &indx)) != 0)
1.137 wrstuden 1976: return (error);
1977: fp = nfp;
1.265 yamt 1978: error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1.248 yamt 1979: if (error != 0) {
1.139 wrstuden 1980: goto bad;
1981: }
1.248 yamt 1982: error = vfs_fhtovp(fh, &vp);
1.481 maxv 1983: vfs_copyinfh_free(fh);
1.248 yamt 1984: if (error != 0) {
1.139 wrstuden 1985: goto bad;
1986: }
1.137 wrstuden 1987:
1988: /* Now do an effective vn_open */
1989:
1990: if (vp->v_type == VSOCK) {
1991: error = EOPNOTSUPP;
1992: goto bad;
1993: }
1.333 yamt 1994: error = vn_openchk(vp, cred, flags);
1995: if (error != 0)
1996: goto bad;
1.137 wrstuden 1997: if (flags & O_TRUNC) {
1.406 hannken 1998: VOP_UNLOCK(vp); /* XXX */
1.137 wrstuden 1999: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1.402 pooka 2000: vattr_null(&va);
1.137 wrstuden 2001: va.va_size = 0;
1.332 pooka 2002: error = VOP_SETATTR(vp, &va, cred);
1.197 hannken 2003: if (error)
1.137 wrstuden 2004: goto bad;
2005: }
1.332 pooka 2006: if ((error = VOP_OPEN(vp, flags, cred)) != 0)
1.137 wrstuden 2007: goto bad;
1.346 ad 2008: if (flags & FWRITE) {
1.429 rmind 2009: mutex_enter(vp->v_interlock);
1.137 wrstuden 2010: vp->v_writecount++;
1.429 rmind 2011: mutex_exit(vp->v_interlock);
1.346 ad 2012: }
1.137 wrstuden 2013:
2014: /* done with modified vn_open, now finish what sys_open does. */
1.422 christos 2015: if ((error = open_setfp(l, fp, vp, indx, flags)))
2016: return error;
1.137 wrstuden 2017:
1.406 hannken 2018: VOP_UNLOCK(vp);
1.137 wrstuden 2019: *retval = indx;
1.346 ad 2020: fd_affix(p, fp, indx);
1.137 wrstuden 2021: return (0);
1.139 wrstuden 2022:
1.137 wrstuden 2023: bad:
1.346 ad 2024: fd_abort(p, fp, indx);
1.139 wrstuden 2025: if (vp != NULL)
2026: vput(vp);
1.137 wrstuden 2027: return (error);
2028: }
2029:
2030: int
1.335 dsl 2031: sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval)
1.137 wrstuden 2032: {
1.335 dsl 2033: /* {
1.263 martin 2034: syscallarg(const void *) fhp;
2035: syscallarg(size_t) fh_size;
1.265 yamt 2036: syscallarg(int) flags;
1.335 dsl 2037: } */
1.265 yamt 2038:
2039: return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
2040: SCARG(uap, flags), retval);
2041: }
2042:
1.306 dsl 2043: int
2044: do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
2045: {
1.137 wrstuden 2046: int error;
1.248 yamt 2047: fhandle_t *fh;
1.137 wrstuden 2048: struct vnode *vp;
2049:
2050: /*
2051: * Must be super user
2052: */
1.268 elad 2053: if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1.270 elad 2054: 0, NULL, NULL, NULL)))
1.137 wrstuden 2055: return (error);
2056:
1.265 yamt 2057: error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1.306 dsl 2058: if (error != 0)
2059: return error;
2060:
1.248 yamt 2061: error = vfs_fhtovp(fh, &vp);
1.306 dsl 2062: vfs_copyinfh_free(fh);
2063: if (error != 0)
2064: return error;
2065:
1.346 ad 2066: error = vn_stat(vp, sb);
1.137 wrstuden 2067: vput(vp);
1.248 yamt 2068: return error;
1.137 wrstuden 2069: }
2070:
1.265 yamt 2071:
1.137 wrstuden 2072: /* ARGSUSED */
2073: int
1.383 christos 2074: sys___fhstat50(struct lwp *l, const struct sys___fhstat50_args *uap, register_t *retval)
1.137 wrstuden 2075: {
1.335 dsl 2076: /* {
1.265 yamt 2077: syscallarg(const void *) fhp;
1.263 martin 2078: syscallarg(size_t) fh_size;
1.265 yamt 2079: syscallarg(struct stat *) sb;
1.335 dsl 2080: } */
1.306 dsl 2081: struct stat sb;
2082: int error;
1.265 yamt 2083:
1.306 dsl 2084: error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
2085: if (error)
2086: return error;
2087: return copyout(&sb, SCARG(uap, sb), sizeof(sb));
1.265 yamt 2088: }
2089:
1.306 dsl 2090: int
2091: do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
2092: int flags)
2093: {
1.248 yamt 2094: fhandle_t *fh;
1.137 wrstuden 2095: struct mount *mp;
2096: struct vnode *vp;
2097: int error;
2098:
2099: /*
2100: * Must be super user
2101: */
1.268 elad 2102: if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1.270 elad 2103: 0, NULL, NULL, NULL)))
1.206 christos 2104: return error;
1.137 wrstuden 2105:
1.265 yamt 2106: error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1.306 dsl 2107: if (error != 0)
2108: return error;
2109:
1.248 yamt 2110: error = vfs_fhtovp(fh, &vp);
1.306 dsl 2111: vfs_copyinfh_free(fh);
2112: if (error != 0)
2113: return error;
2114:
1.137 wrstuden 2115: mp = vp->v_mount;
1.306 dsl 2116: error = dostatvfs(mp, sb, l, flags, 1);
1.137 wrstuden 2117: vput(vp);
1.241 yamt 2118: return error;
1.31 cgd 2119: }
2120:
1.265 yamt 2121: /* ARGSUSED */
2122: int
1.335 dsl 2123: sys___fhstatvfs140(struct lwp *l, const struct sys___fhstatvfs140_args *uap, register_t *retval)
1.265 yamt 2124: {
1.335 dsl 2125: /* {
1.266 yamt 2126: syscallarg(const void *) fhp;
1.265 yamt 2127: syscallarg(size_t) fh_size;
2128: syscallarg(struct statvfs *) buf;
2129: syscallarg(int) flags;
1.335 dsl 2130: } */
1.306 dsl 2131: struct statvfs *sb = STATVFSBUF_GET();
2132: int error;
1.265 yamt 2133:
1.306 dsl 2134: error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
2135: SCARG(uap, flags));
2136: if (error == 0)
2137: error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
2138: STATVFSBUF_PUT(sb);
2139: return error;
1.265 yamt 2140: }
2141:
1.31 cgd 2142: /*
2143: * Create a special file.
2144: */
2145: /* ARGSUSED */
1.63 christos 2146: int
1.383 christos 2147: sys___mknod50(struct lwp *l, const struct sys___mknod50_args *uap,
2148: register_t *retval)
1.56 thorpej 2149: {
1.335 dsl 2150: /* {
1.74 cgd 2151: syscallarg(const char *) path;
1.383 christos 2152: syscallarg(mode_t) mode;
2153: syscallarg(dev_t) dev;
1.335 dsl 2154: } */
1.460 manu 2155: return do_sys_mknodat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap, mode),
1.399 haad 2156: SCARG(uap, dev), retval, UIO_USERSPACE);
1.383 christos 2157: }
2158:
2159: int
1.433 manu 2160: sys_mknodat(struct lwp *l, const struct sys_mknodat_args *uap,
2161: register_t *retval)
2162: {
2163: /* {
2164: syscallarg(int) fd;
2165: syscallarg(const char *) path;
2166: syscallarg(mode_t) mode;
1.468 njoly 2167: syscallarg(int) pad;
2168: syscallarg(dev_t) dev;
1.433 manu 2169: } */
2170:
1.460 manu 2171: return do_sys_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
2172: SCARG(uap, mode), SCARG(uap, dev), retval, UIO_USERSPACE);
1.433 manu 2173: }
2174:
2175: int
1.383 christos 2176: do_sys_mknod(struct lwp *l, const char *pathname, mode_t mode, dev_t dev,
1.399 haad 2177: register_t *retval, enum uio_seg seg)
1.383 christos 2178: {
1.460 manu 2179: return do_sys_mknodat(l, AT_FDCWD, pathname, mode, dev, retval, seg);
2180: }
2181:
2182: int
2183: do_sys_mknodat(struct lwp *l, int fdat, const char *pathname, mode_t mode,
2184: dev_t dev, register_t *retval, enum uio_seg seg)
2185: {
1.179 thorpej 2186: struct proc *p = l->l_proc;
1.155 augustss 2187: struct vnode *vp;
1.31 cgd 2188: struct vattr vattr;
1.301 pooka 2189: int error, optype;
1.409 dholland 2190: struct pathbuf *pb;
1.315 christos 2191: struct nameidata nd;
1.409 dholland 2192: const char *pathstring;
1.31 cgd 2193:
1.268 elad 2194: if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
2195: 0, NULL, NULL, NULL)) != 0)
1.31 cgd 2196: return (error);
1.301 pooka 2197:
2198: optype = VOP_MKNOD_DESCOFFSET;
1.315 christos 2199:
1.409 dholland 2200: error = pathbuf_maybe_copyin(pathname, seg, &pb);
2201: if (error) {
2202: return error;
2203: }
2204: pathstring = pathbuf_stringcopy_get(pb);
2205: if (pathstring == NULL) {
2206: pathbuf_destroy(pb);
2207: return ENOMEM;
2208: }
1.315 christos 2209:
1.409 dholland 2210: NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
1.460 manu 2211:
2212: if ((error = fd_nameiat(l, fdat, &nd)) != 0)
1.314 christos 2213: goto out;
1.31 cgd 2214: vp = nd.ni_vp;
1.409 dholland 2215:
1.43 mycroft 2216: if (vp != NULL)
1.31 cgd 2217: error = EEXIST;
1.43 mycroft 2218: else {
1.402 pooka 2219: vattr_null(&vattr);
1.328 ad 2220: /* We will read cwdi->cwdi_cmask unlocked. */
1.383 christos 2221: vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
2222: vattr.va_rdev = dev;
1.43 mycroft 2223:
1.383 christos 2224: switch (mode & S_IFMT) {
1.43 mycroft 2225: case S_IFMT: /* used by badsect to flag bad sectors */
2226: vattr.va_type = VBAD;
2227: break;
2228: case S_IFCHR:
2229: vattr.va_type = VCHR;
2230: break;
2231: case S_IFBLK:
2232: vattr.va_type = VBLK;
2233: break;
2234: case S_IFWHT:
1.301 pooka 2235: optype = VOP_WHITEOUT_DESCOFFSET;
2236: break;
2237: case S_IFREG:
1.314 christos 2238: #if NVERIEXEC > 0
1.409 dholland 2239: error = veriexec_openchk(l, nd.ni_vp, pathstring,
1.314 christos 2240: O_CREAT);
2241: #endif /* NVERIEXEC > 0 */
1.301 pooka 2242: vattr.va_type = VREG;
1.302 pooka 2243: vattr.va_rdev = VNOVAL;
1.301 pooka 2244: optype = VOP_CREATE_DESCOFFSET;
1.43 mycroft 2245: break;
2246: default:
2247: error = EINVAL;
2248: break;
2249: }
1.31 cgd 2250: }
1.432 martin 2251: if (error == 0 && optype == VOP_MKNOD_DESCOFFSET
2252: && vattr.va_rdev == VNOVAL)
1.431 hannken 2253: error = EINVAL;
1.43 mycroft 2254: if (!error) {
1.301 pooka 2255: switch (optype) {
2256: case VOP_WHITEOUT_DESCOFFSET:
1.43 mycroft 2257: error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
2258: if (error)
2259: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2260: vput(nd.ni_dvp);
1.301 pooka 2261: break;
2262:
2263: case VOP_MKNOD_DESCOFFSET:
1.43 mycroft 2264: error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
2265: &nd.ni_cnd, &vattr);
1.168 assar 2266: if (error == 0)
1.473 hannken 2267: vrele(nd.ni_vp);
1.472 hannken 2268: vput(nd.ni_dvp);
1.301 pooka 2269: break;
2270:
2271: case VOP_CREATE_DESCOFFSET:
2272: error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
2273: &nd.ni_cnd, &vattr);
2274: if (error == 0)
1.473 hannken 2275: vrele(nd.ni_vp);
1.472 hannken 2276: vput(nd.ni_dvp);
1.301 pooka 2277: break;
1.43 mycroft 2278: }
2279: } else {
2280: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2281: if (nd.ni_dvp == vp)
2282: vrele(nd.ni_dvp);
2283: else
2284: vput(nd.ni_dvp);
2285: if (vp)
2286: vrele(vp);
1.31 cgd 2287: }
1.314 christos 2288: out:
1.409 dholland 2289: pathbuf_stringcopy_put(pb, pathstring);
2290: pathbuf_destroy(pb);
1.31 cgd 2291: return (error);
2292: }
2293:
2294: /*
2295: * Create a named pipe.
2296: */
2297: /* ARGSUSED */
1.63 christos 2298: int
1.335 dsl 2299: sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
1.56 thorpej 2300: {
1.335 dsl 2301: /* {
1.74 cgd 2302: syscallarg(const char *) path;
1.35 cgd 2303: syscallarg(int) mode;
1.335 dsl 2304: } */
1.460 manu 2305: return do_sys_mkfifoat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap, mode));
2306: }
2307:
2308: int
2309: sys_mkfifoat(struct lwp *l, const struct sys_mkfifoat_args *uap,
2310: register_t *retval)
2311: {
2312: /* {
2313: syscallarg(int) fd;
2314: syscallarg(const char *) path;
2315: syscallarg(int) mode;
2316: } */
2317:
2318: return do_sys_mkfifoat(l, SCARG(uap, fd), SCARG(uap, path),
2319: SCARG(uap, mode));
2320: }
2321:
2322: static int
2323: do_sys_mkfifoat(struct lwp *l, int fdat, const char *path, mode_t mode)
2324: {
1.179 thorpej 2325: struct proc *p = l->l_proc;
1.31 cgd 2326: struct vattr vattr;
2327: int error;
1.409 dholland 2328: struct pathbuf *pb;
1.31 cgd 2329: struct nameidata nd;
2330:
1.460 manu 2331: error = pathbuf_copyin(path, &pb);
1.409 dholland 2332: if (error) {
2333: return error;
2334: }
2335: NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
1.460 manu 2336:
2337: if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
1.409 dholland 2338: pathbuf_destroy(pb);
2339: return error;
2340: }
1.31 cgd 2341: if (nd.ni_vp != NULL) {
2342: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2343: if (nd.ni_dvp == nd.ni_vp)
2344: vrele(nd.ni_dvp);
2345: else
2346: vput(nd.ni_dvp);
2347: vrele(nd.ni_vp);
1.409 dholland 2348: pathbuf_destroy(pb);
1.31 cgd 2349: return (EEXIST);
2350: }
1.402 pooka 2351: vattr_null(&vattr);
1.31 cgd 2352: vattr.va_type = VFIFO;
1.328 ad 2353: /* We will read cwdi->cwdi_cmask unlocked. */
1.460 manu 2354: vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1.168 assar 2355: error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2356: if (error == 0)
1.473 hannken 2357: vrele(nd.ni_vp);
1.472 hannken 2358: vput(nd.ni_dvp);
1.409 dholland 2359: pathbuf_destroy(pb);
1.168 assar 2360: return (error);
1.31 cgd 2361: }
2362:
2363: /*
2364: * Make a hard file link.
2365: */
2366: /* ARGSUSED */
1.469 chs 2367: int
1.460 manu 2368: do_sys_linkat(struct lwp *l, int fdpath, const char *path, int fdlink,
2369: const char *link, int follow, register_t *retval)
1.56 thorpej 2370: {
1.155 augustss 2371: struct vnode *vp;
1.409 dholland 2372: struct pathbuf *linkpb;
1.31 cgd 2373: struct nameidata nd;
1.460 manu 2374: namei_simple_flags_t ns_flags;
1.31 cgd 2375: int error;
2376:
1.460 manu 2377: if (follow & AT_SYMLINK_FOLLOW)
2378: ns_flags = NSM_FOLLOW_TRYEMULROOT;
1.433 manu 2379: else
1.460 manu 2380: ns_flags = NSM_NOFOLLOW_TRYEMULROOT;
1.433 manu 2381:
1.460 manu 2382: error = fd_nameiat_simple_user(l, fdpath, path, ns_flags, &vp);
1.395 dholland 2383: if (error != 0)
1.31 cgd 2384: return (error);
1.433 manu 2385: error = pathbuf_copyin(link, &linkpb);
1.409 dholland 2386: if (error) {
2387: goto out1;
2388: }
2389: NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
1.460 manu 2390: if ((error = fd_nameiat(l, fdlink, &nd)) != 0)
1.409 dholland 2391: goto out2;
1.66 mycroft 2392: if (nd.ni_vp) {
2393: error = EEXIST;
1.419 yamt 2394: goto abortop;
2395: }
1.423 rmind 2396: /* Prevent hard links on directories. */
2397: if (vp->v_type == VDIR) {
2398: error = EPERM;
2399: goto abortop;
2400: }
2401: /* Prevent cross-mount operation. */
1.419 yamt 2402: if (nd.ni_dvp->v_mount != vp->v_mount) {
2403: error = EXDEV;
2404: goto abortop;
1.31 cgd 2405: }
1.66 mycroft 2406: error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1.496 riastrad 2407: VOP_UNLOCK(nd.ni_dvp);
2408: vrele(nd.ni_dvp);
1.409 dholland 2409: out2:
2410: pathbuf_destroy(linkpb);
2411: out1:
1.31 cgd 2412: vrele(vp);
2413: return (error);
1.419 yamt 2414: abortop:
2415: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2416: if (nd.ni_dvp == nd.ni_vp)
2417: vrele(nd.ni_dvp);
2418: else
2419: vput(nd.ni_dvp);
2420: if (nd.ni_vp != NULL)
2421: vrele(nd.ni_vp);
2422: goto out2;
1.31 cgd 2423: }
2424:
1.63 christos 2425: int
1.433 manu 2426: sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
2427: {
2428: /* {
2429: syscallarg(const char *) path;
2430: syscallarg(const char *) link;
2431: } */
2432: const char *path = SCARG(uap, path);
2433: const char *link = SCARG(uap, link);
2434:
1.460 manu 2435: return do_sys_linkat(l, AT_FDCWD, path, AT_FDCWD, link,
2436: AT_SYMLINK_FOLLOW, retval);
1.433 manu 2437: }
2438:
2439: int
2440: sys_linkat(struct lwp *l, const struct sys_linkat_args *uap,
2441: register_t *retval)
2442: {
2443: /* {
2444: syscallarg(int) fd1;
2445: syscallarg(const char *) name1;
2446: syscallarg(int) fd2;
2447: syscallarg(const char *) name2;
2448: syscallarg(int) flags;
2449: } */
1.460 manu 2450: int fd1 = SCARG(uap, fd1);
1.433 manu 2451: const char *name1 = SCARG(uap, name1);
1.460 manu 2452: int fd2 = SCARG(uap, fd2);
1.433 manu 2453: const char *name2 = SCARG(uap, name2);
2454: int follow;
2455:
2456: follow = SCARG(uap, flags) & AT_SYMLINK_FOLLOW;
2457:
1.460 manu 2458: return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
1.433 manu 2459: }
2460:
2461:
2462: int
1.407 pooka 2463: do_sys_symlink(const char *patharg, const char *link, enum uio_seg seg)
1.56 thorpej 2464: {
1.460 manu 2465: return do_sys_symlinkat(NULL, patharg, AT_FDCWD, link, seg);
2466: }
2467:
2468: static int
2469: do_sys_symlinkat(struct lwp *l, const char *patharg, int fdat,
2470: const char *link, enum uio_seg seg)
2471: {
1.407 pooka 2472: struct proc *p = curproc;
1.31 cgd 2473: struct vattr vattr;
2474: char *path;
2475: int error;
1.409 dholland 2476: struct pathbuf *linkpb;
1.31 cgd 2477: struct nameidata nd;
2478:
1.460 manu 2479: KASSERT(l != NULL || fdat == AT_FDCWD);
2480:
1.161 thorpej 2481: path = PNBUF_GET();
1.407 pooka 2482: if (seg == UIO_USERSPACE) {
2483: if ((error = copyinstr(patharg, path, MAXPATHLEN, NULL)) != 0)
1.409 dholland 2484: goto out1;
2485: if ((error = pathbuf_copyin(link, &linkpb)) != 0)
2486: goto out1;
1.407 pooka 2487: } else {
2488: KASSERT(strlen(patharg) < MAXPATHLEN);
2489: strcpy(path, patharg);
1.409 dholland 2490: linkpb = pathbuf_create(link);
2491: if (linkpb == NULL) {
2492: error = ENOMEM;
2493: goto out1;
2494: }
1.407 pooka 2495: }
1.433 manu 2496: ktrkuser("symlink-target", path, strlen(path));
2497:
1.409 dholland 2498: NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
1.460 manu 2499: if ((error = fd_nameiat(l, fdat, &nd)) != 0)
1.409 dholland 2500: goto out2;
1.31 cgd 2501: if (nd.ni_vp) {
2502: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2503: if (nd.ni_dvp == nd.ni_vp)
2504: vrele(nd.ni_dvp);
2505: else
2506: vput(nd.ni_dvp);
2507: vrele(nd.ni_vp);
2508: error = EEXIST;
1.409 dholland 2509: goto out2;
1.31 cgd 2510: }
1.402 pooka 2511: vattr_null(&vattr);
1.230 jmmv 2512: vattr.va_type = VLNK;
1.328 ad 2513: /* We will read cwdi->cwdi_cmask unlocked. */
1.134 thorpej 2514: vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1.31 cgd 2515: error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1.168 assar 2516: if (error == 0)
1.473 hannken 2517: vrele(nd.ni_vp);
1.472 hannken 2518: vput(nd.ni_dvp);
1.409 dholland 2519: out2:
2520: pathbuf_destroy(linkpb);
2521: out1:
1.161 thorpej 2522: PNBUF_PUT(path);
1.31 cgd 2523: return (error);
2524: }
2525:
2526: /*
1.407 pooka 2527: * Make a symbolic link.
2528: */
2529: /* ARGSUSED */
2530: int
2531: sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
2532: {
2533: /* {
2534: syscallarg(const char *) path;
2535: syscallarg(const char *) link;
2536: } */
2537:
1.460 manu 2538: return do_sys_symlinkat(l, SCARG(uap, path), AT_FDCWD, SCARG(uap, link),
1.407 pooka 2539: UIO_USERSPACE);
2540: }
2541:
1.433 manu 2542: int
2543: sys_symlinkat(struct lwp *l, const struct sys_symlinkat_args *uap,
2544: register_t *retval)
2545: {
2546: /* {
1.460 manu 2547: syscallarg(const char *) path1;
1.433 manu 2548: syscallarg(int) fd;
1.460 manu 2549: syscallarg(const char *) path2;
1.433 manu 2550: } */
2551:
1.460 manu 2552: return do_sys_symlinkat(l, SCARG(uap, path1), SCARG(uap, fd),
2553: SCARG(uap, path2), UIO_USERSPACE);
1.433 manu 2554: }
2555:
1.407 pooka 2556: /*
1.43 mycroft 2557: * Delete a whiteout from the filesystem.
2558: */
2559: /* ARGSUSED */
1.63 christos 2560: int
1.335 dsl 2561: sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
1.56 thorpej 2562: {
1.335 dsl 2563: /* {
1.74 cgd 2564: syscallarg(const char *) path;
1.335 dsl 2565: } */
1.43 mycroft 2566: int error;
1.409 dholland 2567: struct pathbuf *pb;
1.43 mycroft 2568: struct nameidata nd;
2569:
1.409 dholland 2570: error = pathbuf_copyin(SCARG(uap, path), &pb);
2571: if (error) {
2572: return error;
2573: }
2574:
2575: NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, pb);
1.43 mycroft 2576: error = namei(&nd);
1.409 dholland 2577: if (error) {
2578: pathbuf_destroy(pb);
1.43 mycroft 2579: return (error);
1.409 dholland 2580: }
1.43 mycroft 2581:
2582: if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
2583: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2584: if (nd.ni_dvp == nd.ni_vp)
2585: vrele(nd.ni_dvp);
2586: else
2587: vput(nd.ni_dvp);
2588: if (nd.ni_vp)
2589: vrele(nd.ni_vp);
1.409 dholland 2590: pathbuf_destroy(pb);
1.43 mycroft 2591: return (EEXIST);
2592: }
1.63 christos 2593: if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1.43 mycroft 2594: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2595: vput(nd.ni_dvp);
1.409 dholland 2596: pathbuf_destroy(pb);
1.43 mycroft 2597: return (error);
2598: }
2599:
2600: /*
1.31 cgd 2601: * Delete a name from the filesystem.
2602: */
2603: /* ARGSUSED */
1.63 christos 2604: int
1.335 dsl 2605: sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
1.56 thorpej 2606: {
1.335 dsl 2607: /* {
1.74 cgd 2608: syscallarg(const char *) path;
1.335 dsl 2609: } */
1.336 ad 2610:
1.460 manu 2611: return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path), 0, UIO_USERSPACE);
1.336 ad 2612: }
2613:
2614: int
1.433 manu 2615: sys_unlinkat(struct lwp *l, const struct sys_unlinkat_args *uap,
2616: register_t *retval)
2617: {
2618: /* {
2619: syscallarg(int) fd;
2620: syscallarg(const char *) path;
1.460 manu 2621: syscallarg(int) flag;
1.433 manu 2622: } */
2623:
1.460 manu 2624: return do_sys_unlinkat(l, SCARG(uap, fd), SCARG(uap, path),
2625: SCARG(uap, flag), UIO_USERSPACE);
1.433 manu 2626: }
2627:
2628: int
1.336 ad 2629: do_sys_unlink(const char *arg, enum uio_seg seg)
2630: {
1.460 manu 2631: return do_sys_unlinkat(NULL, AT_FDCWD, arg, 0, seg);
2632: }
2633:
2634: static int
2635: do_sys_unlinkat(struct lwp *l, int fdat, const char *arg, int flags,
2636: enum uio_seg seg)
2637: {
1.155 augustss 2638: struct vnode *vp;
1.31 cgd 2639: int error;
1.409 dholland 2640: struct pathbuf *pb;
1.31 cgd 2641: struct nameidata nd;
1.409 dholland 2642: const char *pathstring;
1.31 cgd 2643:
1.460 manu 2644: KASSERT(l != NULL || fdat == AT_FDCWD);
2645:
1.409 dholland 2646: error = pathbuf_maybe_copyin(arg, seg, &pb);
2647: if (error) {
2648: return error;
2649: }
2650: pathstring = pathbuf_stringcopy_get(pb);
2651: if (pathstring == NULL) {
2652: pathbuf_destroy(pb);
2653: return ENOMEM;
2654: }
1.313 elad 2655:
1.409 dholland 2656: NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
1.460 manu 2657: if ((error = fd_nameiat(l, fdat, &nd)) != 0)
1.313 elad 2658: goto out;
1.31 cgd 2659: vp = nd.ni_vp;
2660:
1.66 mycroft 2661: /*
2662: * The root of a mounted filesystem cannot be deleted.
2663: */
1.460 manu 2664: if ((vp->v_vflag & VV_ROOT) != 0) {
2665: error = EBUSY;
2666: goto abort;
2667: }
2668:
2669: if ((vp->v_type == VDIR) && (vp->v_mountedhere != NULL)) {
1.66 mycroft 2670: error = EBUSY;
1.460 manu 2671: goto abort;
2672: }
2673:
2674: /*
2675: * No rmdir "." please.
2676: */
2677: if (nd.ni_dvp == vp) {
2678: error = EINVAL;
2679: goto abort;
2680: }
2681:
2682: /*
2683: * AT_REMOVEDIR is required to remove a directory
2684: */
2685: if (vp->v_type == VDIR) {
2686: if (!(flags & AT_REMOVEDIR)) {
2687: error = EPERM;
2688: goto abort;
2689: } else {
2690: error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1.513 riastrad 2691: vput(nd.ni_dvp);
1.460 manu 2692: goto out;
2693: }
2694: }
2695:
2696: /*
2697: * Starting here we only deal with non directories.
2698: */
2699: if (flags & AT_REMOVEDIR) {
2700: error = ENOTDIR;
2701: goto abort;
1.31 cgd 2702: }
1.219 blymn 2703:
1.256 elad 2704: #if NVERIEXEC > 0
1.222 elad 2705: /* Handle remove requests for veriexec entries. */
1.409 dholland 2706: if ((error = veriexec_removechk(curlwp, nd.ni_vp, pathstring)) != 0) {
1.487 maxv 2707: goto abort;
1.219 blymn 2708: }
1.256 elad 2709: #endif /* NVERIEXEC > 0 */
2710:
1.253 elad 2711: #ifdef FILEASSOC
1.278 elad 2712: (void)fileassoc_file_delete(vp);
1.253 elad 2713: #endif /* FILEASSOC */
1.66 mycroft 2714: error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1.513 riastrad 2715: vput(nd.ni_dvp);
1.460 manu 2716: goto out;
2717:
2718: abort:
2719: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2720: if (nd.ni_dvp == vp)
2721: vrele(nd.ni_dvp);
2722: else
2723: vput(nd.ni_dvp);
2724: vput(vp);
2725:
1.66 mycroft 2726: out:
1.409 dholland 2727: pathbuf_stringcopy_put(pb, pathstring);
2728: pathbuf_destroy(pb);
1.31 cgd 2729: return (error);
2730: }
2731:
2732: /*
2733: * Reposition read/write file offset.
2734: */
1.63 christos 2735: int
1.335 dsl 2736: sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
1.56 thorpej 2737: {
1.335 dsl 2738: /* {
1.35 cgd 2739: syscallarg(int) fd;
2740: syscallarg(int) pad;
2741: syscallarg(off_t) offset;
2742: syscallarg(int) whence;
1.335 dsl 2743: } */
1.257 ad 2744: kauth_cred_t cred = l->l_cred;
1.346 ad 2745: file_t *fp;
1.84 kleink 2746: struct vnode *vp;
1.31 cgd 2747: struct vattr vattr;
1.155 augustss 2748: off_t newoff;
1.346 ad 2749: int error, fd;
2750:
2751: fd = SCARG(uap, fd);
1.31 cgd 2752:
1.346 ad 2753: if ((fp = fd_getfile(fd)) == NULL)
1.31 cgd 2754: return (EBADF);
1.84 kleink 2755:
1.491 matt 2756: vp = fp->f_vnode;
1.135 thorpej 2757: if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2758: error = ESPIPE;
2759: goto out;
2760: }
1.84 kleink 2761:
1.504 dholland 2762: vn_lock(vp, LK_SHARED | LK_RETRY);
2763:
1.35 cgd 2764: switch (SCARG(uap, whence)) {
1.92 kleink 2765: case SEEK_CUR:
1.84 kleink 2766: newoff = fp->f_offset + SCARG(uap, offset);
1.31 cgd 2767: break;
1.92 kleink 2768: case SEEK_END:
1.332 pooka 2769: error = VOP_GETATTR(vp, &vattr, cred);
1.328 ad 2770: if (error) {
1.504 dholland 2771: VOP_UNLOCK(vp);
1.135 thorpej 2772: goto out;
1.328 ad 2773: }
1.84 kleink 2774: newoff = SCARG(uap, offset) + vattr.va_size;
1.31 cgd 2775: break;
1.92 kleink 2776: case SEEK_SET:
1.84 kleink 2777: newoff = SCARG(uap, offset);
1.31 cgd 2778: break;
2779: default:
1.135 thorpej 2780: error = EINVAL;
1.504 dholland 2781: VOP_UNLOCK(vp);
1.135 thorpej 2782: goto out;
1.31 cgd 2783: }
1.504 dholland 2784: VOP_UNLOCK(vp);
1.328 ad 2785: if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
2786: *(off_t *)retval = fp->f_offset = newoff;
2787: }
1.135 thorpej 2788: out:
1.346 ad 2789: fd_putfile(fd);
1.135 thorpej 2790: return (error);
1.120 thorpej 2791: }
2792:
2793: /*
2794: * Positional read system call.
2795: */
2796: int
1.335 dsl 2797: sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
1.120 thorpej 2798: {
1.335 dsl 2799: /* {
1.120 thorpej 2800: syscallarg(int) fd;
2801: syscallarg(void *) buf;
2802: syscallarg(size_t) nbyte;
2803: syscallarg(off_t) offset;
1.335 dsl 2804: } */
1.346 ad 2805: file_t *fp;
1.120 thorpej 2806: struct vnode *vp;
2807: off_t offset;
2808: int error, fd = SCARG(uap, fd);
2809:
1.346 ad 2810: if ((fp = fd_getfile(fd)) == NULL)
1.166 thorpej 2811: return (EBADF);
2812:
1.183 pk 2813: if ((fp->f_flag & FREAD) == 0) {
1.346 ad 2814: fd_putfile(fd);
1.120 thorpej 2815: return (EBADF);
1.183 pk 2816: }
1.120 thorpej 2817:
1.491 matt 2818: vp = fp->f_vnode;
1.135 thorpej 2819: if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2820: error = ESPIPE;
2821: goto out;
2822: }
1.120 thorpej 2823:
2824: offset = SCARG(uap, offset);
2825:
2826: /*
2827: * XXX This works because no file systems actually
2828: * XXX take any action on the seek operation.
2829: */
2830: if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1.135 thorpej 2831: goto out;
1.120 thorpej 2832:
1.135 thorpej 2833: /* dofileread() will unuse the descriptor for us */
1.328 ad 2834: return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1.120 thorpej 2835: &offset, 0, retval));
1.135 thorpej 2836:
2837: out:
1.346 ad 2838: fd_putfile(fd);
1.135 thorpej 2839: return (error);
1.120 thorpej 2840: }
2841:
2842: /*
2843: * Positional scatter read system call.
2844: */
2845: int
1.335 dsl 2846: sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
1.120 thorpej 2847: {
1.335 dsl 2848: /* {
1.120 thorpej 2849: syscallarg(int) fd;
2850: syscallarg(const struct iovec *) iovp;
2851: syscallarg(int) iovcnt;
2852: syscallarg(off_t) offset;
1.335 dsl 2853: } */
2854: off_t offset = SCARG(uap, offset);
1.120 thorpej 2855:
1.328 ad 2856: return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
1.335 dsl 2857: SCARG(uap, iovcnt), &offset, 0, retval);
1.120 thorpej 2858: }
2859:
2860: /*
2861: * Positional write system call.
2862: */
2863: int
1.335 dsl 2864: sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
1.120 thorpej 2865: {
1.335 dsl 2866: /* {
1.120 thorpej 2867: syscallarg(int) fd;
2868: syscallarg(const void *) buf;
2869: syscallarg(size_t) nbyte;
2870: syscallarg(off_t) offset;
1.335 dsl 2871: } */
1.346 ad 2872: file_t *fp;
1.120 thorpej 2873: struct vnode *vp;
2874: off_t offset;
2875: int error, fd = SCARG(uap, fd);
2876:
1.346 ad 2877: if ((fp = fd_getfile(fd)) == NULL)
1.166 thorpej 2878: return (EBADF);
2879:
1.183 pk 2880: if ((fp->f_flag & FWRITE) == 0) {
1.346 ad 2881: fd_putfile(fd);
1.120 thorpej 2882: return (EBADF);
1.183 pk 2883: }
1.120 thorpej 2884:
1.491 matt 2885: vp = fp->f_vnode;
1.135 thorpej 2886: if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2887: error = ESPIPE;
2888: goto out;
2889: }
1.120 thorpej 2890:
2891: offset = SCARG(uap, offset);
2892:
2893: /*
2894: * XXX This works because no file systems actually
2895: * XXX take any action on the seek operation.
2896: */
2897: if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1.135 thorpej 2898: goto out;
1.120 thorpej 2899:
1.135 thorpej 2900: /* dofilewrite() will unuse the descriptor for us */
1.328 ad 2901: return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1.120 thorpej 2902: &offset, 0, retval));
1.135 thorpej 2903:
2904: out:
1.346 ad 2905: fd_putfile(fd);
1.135 thorpej 2906: return (error);
1.120 thorpej 2907: }
2908:
2909: /*
2910: * Positional gather write system call.
2911: */
2912: int
1.335 dsl 2913: sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
1.120 thorpej 2914: {
1.335 dsl 2915: /* {
1.120 thorpej 2916: syscallarg(int) fd;
2917: syscallarg(const struct iovec *) iovp;
2918: syscallarg(int) iovcnt;
2919: syscallarg(off_t) offset;
1.335 dsl 2920: } */
2921: off_t offset = SCARG(uap, offset);
1.120 thorpej 2922:
1.328 ad 2923: return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
1.335 dsl 2924: SCARG(uap, iovcnt), &offset, 0, retval);
1.31 cgd 2925: }
2926:
2927: /*
2928: * Check access permissions.
2929: */
1.63 christos 2930: int
1.335 dsl 2931: sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
1.56 thorpej 2932: {
1.335 dsl 2933: /* {
1.74 cgd 2934: syscallarg(const char *) path;
1.35 cgd 2935: syscallarg(int) flags;
1.335 dsl 2936: } */
1.460 manu 2937:
2938: return do_sys_accessat(l, AT_FDCWD, SCARG(uap, path),
2939: SCARG(uap, flags), 0);
2940: }
2941:
1.469 chs 2942: int
1.460 manu 2943: do_sys_accessat(struct lwp *l, int fdat, const char *path,
2944: int mode, int flags)
2945: {
1.242 elad 2946: kauth_cred_t cred;
1.155 augustss 2947: struct vnode *vp;
1.460 manu 2948: int error, nd_flag, vmode;
1.409 dholland 2949: struct pathbuf *pb;
1.31 cgd 2950: struct nameidata nd;
2951:
1.417 dholland 2952: CTASSERT(F_OK == 0);
1.460 manu 2953: if ((mode & ~(R_OK | W_OK | X_OK)) != 0) {
2954: /* nonsense mode */
1.415 dholland 2955: return EINVAL;
2956: }
2957:
1.460 manu 2958: nd_flag = FOLLOW | LOCKLEAF | TRYEMULROOT;
2959: if (flags & AT_SYMLINK_NOFOLLOW)
2960: nd_flag &= ~FOLLOW;
2961:
2962: error = pathbuf_copyin(path, &pb);
2963: if (error)
1.409 dholland 2964: return error;
1.460 manu 2965:
2966: NDINIT(&nd, LOOKUP, nd_flag, pb);
1.409 dholland 2967:
2968: /* Override default credentials */
1.257 ad 2969: cred = kauth_cred_dup(l->l_cred);
1.460 manu 2970: if (!(flags & AT_EACCESS)) {
2971: kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2972: kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2973: }
1.169 christos 2974: nd.ni_cnd.cn_cred = cred;
1.409 dholland 2975:
1.460 manu 2976: if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
1.409 dholland 2977: pathbuf_destroy(pb);
1.169 christos 2978: goto out;
1.409 dholland 2979: }
1.31 cgd 2980: vp = nd.ni_vp;
1.409 dholland 2981: pathbuf_destroy(pb);
1.31 cgd 2982:
2983: /* Flags == 0 means only check for existence. */
1.460 manu 2984: if (mode) {
2985: vmode = 0;
2986: if (mode & R_OK)
2987: vmode |= VREAD;
2988: if (mode & W_OK)
2989: vmode |= VWRITE;
2990: if (mode & X_OK)
2991: vmode |= VEXEC;
1.138 is 2992:
1.460 manu 2993: error = VOP_ACCESS(vp, vmode, cred);
2994: if (!error && (vmode & VWRITE))
1.138 is 2995: error = vn_writechk(vp);
1.31 cgd 2996: }
2997: vput(vp);
1.169 christos 2998: out:
1.242 elad 2999: kauth_cred_free(cred);
1.31 cgd 3000: return (error);
3001: }
3002:
1.433 manu 3003: int
3004: sys_faccessat(struct lwp *l, const struct sys_faccessat_args *uap,
3005: register_t *retval)
3006: {
3007: /* {
3008: syscallarg(int) fd;
3009: syscallarg(const char *) path;
3010: syscallarg(int) amode;
3011: syscallarg(int) flag;
3012: } */
3013:
1.460 manu 3014: return do_sys_accessat(l, SCARG(uap, fd), SCARG(uap, path),
3015: SCARG(uap, amode), SCARG(uap, flag));
1.433 manu 3016: }
3017:
1.31 cgd 3018: /*
1.306 dsl 3019: * Common code for all sys_stat functions, including compat versions.
3020: */
3021: int
1.460 manu 3022: do_sys_stat(const char *userpath, unsigned int nd_flag,
3023: struct stat *sb)
3024: {
3025: return do_sys_statat(NULL, AT_FDCWD, userpath, nd_flag, sb);
3026: }
3027:
1.465 matt 3028: int
1.460 manu 3029: do_sys_statat(struct lwp *l, int fdat, const char *userpath,
3030: unsigned int nd_flag, struct stat *sb)
1.306 dsl 3031: {
3032: int error;
1.409 dholland 3033: struct pathbuf *pb;
1.306 dsl 3034: struct nameidata nd;
3035:
1.460 manu 3036: KASSERT(l != NULL || fdat == AT_FDCWD);
3037:
1.409 dholland 3038: error = pathbuf_copyin(userpath, &pb);
3039: if (error) {
3040: return error;
3041: }
1.460 manu 3042:
3043: NDINIT(&nd, LOOKUP, nd_flag | LOCKLEAF | TRYEMULROOT, pb);
3044:
3045: error = fd_nameiat(l, fdat, &nd);
1.409 dholland 3046: if (error != 0) {
3047: pathbuf_destroy(pb);
1.306 dsl 3048: return error;
1.409 dholland 3049: }
1.346 ad 3050: error = vn_stat(nd.ni_vp, sb);
1.306 dsl 3051: vput(nd.ni_vp);
1.409 dholland 3052: pathbuf_destroy(pb);
1.306 dsl 3053: return error;
3054: }
3055:
3056: /*
1.31 cgd 3057: * Get file status; this version follows links.
3058: */
3059: /* ARGSUSED */
1.63 christos 3060: int
1.383 christos 3061: sys___stat50(struct lwp *l, const struct sys___stat50_args *uap, register_t *retval)
1.56 thorpej 3062: {
1.335 dsl 3063: /* {
1.74 cgd 3064: syscallarg(const char *) path;
1.35 cgd 3065: syscallarg(struct stat *) ub;
1.335 dsl 3066: } */
1.31 cgd 3067: struct stat sb;
3068: int error;
3069:
1.460 manu 3070: error = do_sys_statat(l, AT_FDCWD, SCARG(uap, path), FOLLOW, &sb);
1.31 cgd 3071: if (error)
1.306 dsl 3072: return error;
3073: return copyout(&sb, SCARG(uap, ub), sizeof(sb));
1.31 cgd 3074: }
3075:
3076: /*
3077: * Get file status; this version does not follow links.
3078: */
3079: /* ARGSUSED */
1.63 christos 3080: int
1.383 christos 3081: sys___lstat50(struct lwp *l, const struct sys___lstat50_args *uap, register_t *retval)
1.56 thorpej 3082: {
1.335 dsl 3083: /* {
1.74 cgd 3084: syscallarg(const char *) path;
1.35 cgd 3085: syscallarg(struct stat *) ub;
1.335 dsl 3086: } */
1.65 mycroft 3087: struct stat sb;
1.31 cgd 3088: int error;
3089:
1.460 manu 3090: error = do_sys_statat(l, AT_FDCWD, SCARG(uap, path), NOFOLLOW, &sb);
1.64 jtc 3091: if (error)
1.306 dsl 3092: return error;
3093: return copyout(&sb, SCARG(uap, ub), sizeof(sb));
1.31 cgd 3094: }
3095:
1.433 manu 3096: int
3097: sys_fstatat(struct lwp *l, const struct sys_fstatat_args *uap,
3098: register_t *retval)
3099: {
3100: /* {
3101: syscallarg(int) fd;
3102: syscallarg(const char *) path;
1.460 manu 3103: syscallarg(struct stat *) buf;
1.433 manu 3104: syscallarg(int) flag;
3105: } */
1.460 manu 3106: unsigned int nd_flag;
1.461 martin 3107: struct stat sb;
3108: int error;
1.433 manu 3109:
1.460 manu 3110: if (SCARG(uap, flag) & AT_SYMLINK_NOFOLLOW)
3111: nd_flag = NOFOLLOW;
3112: else
3113: nd_flag = FOLLOW;
3114:
1.461 martin 3115: error = do_sys_statat(l, SCARG(uap, fd), SCARG(uap, path), nd_flag,
3116: &sb);
3117: if (error)
3118: return error;
3119: return copyout(&sb, SCARG(uap, buf), sizeof(sb));
1.433 manu 3120: }
1.460 manu 3121:
1.31 cgd 3122: /*
3123: * Get configurable pathname variables.
3124: */
3125: /* ARGSUSED */
1.63 christos 3126: int
1.335 dsl 3127: sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval)
1.56 thorpej 3128: {
1.335 dsl 3129: /* {
1.74 cgd 3130: syscallarg(const char *) path;
1.35 cgd 3131: syscallarg(int) name;
1.335 dsl 3132: } */
1.31 cgd 3133: int error;
1.409 dholland 3134: struct pathbuf *pb;
1.31 cgd 3135: struct nameidata nd;
3136:
1.409 dholland 3137: error = pathbuf_copyin(SCARG(uap, path), &pb);
3138: if (error) {
3139: return error;
3140: }
3141: NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
3142: if ((error = namei(&nd)) != 0) {
3143: pathbuf_destroy(pb);
1.31 cgd 3144: return (error);
1.409 dholland 3145: }
1.35 cgd 3146: error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1.31 cgd 3147: vput(nd.ni_vp);
1.409 dholland 3148: pathbuf_destroy(pb);
1.31 cgd 3149: return (error);
3150: }
3151:
3152: /*
3153: * Return target name of a symbolic link.
3154: */
3155: /* ARGSUSED */
1.63 christos 3156: int
1.460 manu 3157: sys_readlink(struct lwp *l, const struct sys_readlink_args *uap,
3158: register_t *retval)
1.56 thorpej 3159: {
1.335 dsl 3160: /* {
1.74 cgd 3161: syscallarg(const char *) path;
1.35 cgd 3162: syscallarg(char *) buf;
1.115 kleink 3163: syscallarg(size_t) count;
1.335 dsl 3164: } */
1.460 manu 3165: return do_sys_readlinkat(l, AT_FDCWD, SCARG(uap, path),
3166: SCARG(uap, buf), SCARG(uap, count), retval);
3167: }
3168:
3169: static int
3170: do_sys_readlinkat(struct lwp *l, int fdat, const char *path, char *buf,
3171: size_t count, register_t *retval)
3172: {
1.155 augustss 3173: struct vnode *vp;
1.31 cgd 3174: struct iovec aiov;
3175: struct uio auio;
3176: int error;
1.409 dholland 3177: struct pathbuf *pb;
1.31 cgd 3178: struct nameidata nd;
3179:
1.460 manu 3180: error = pathbuf_copyin(path, &pb);
1.409 dholland 3181: if (error) {
3182: return error;
3183: }
3184: NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1.460 manu 3185: if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
1.409 dholland 3186: pathbuf_destroy(pb);
3187: return error;
3188: }
1.31 cgd 3189: vp = nd.ni_vp;
1.409 dholland 3190: pathbuf_destroy(pb);
1.31 cgd 3191: if (vp->v_type != VLNK)
3192: error = EINVAL;
1.106 enami 3193: else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
1.332 pooka 3194: (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
1.460 manu 3195: aiov.iov_base = buf;
3196: aiov.iov_len = count;
1.31 cgd 3197: auio.uio_iov = &aiov;
3198: auio.uio_iovcnt = 1;
3199: auio.uio_offset = 0;
3200: auio.uio_rw = UIO_READ;
1.238 yamt 3201: KASSERT(l == curlwp);
3202: auio.uio_vmspace = l->l_proc->p_vmspace;
1.460 manu 3203: auio.uio_resid = count;
1.464 christos 3204: if ((error = VOP_READLINK(vp, &auio, l->l_cred)) == 0)
3205: *retval = count - auio.uio_resid;
1.31 cgd 3206: }
3207: vput(vp);
3208: return (error);
3209: }
3210:
1.433 manu 3211: int
3212: sys_readlinkat(struct lwp *l, const struct sys_readlinkat_args *uap,
3213: register_t *retval)
3214: {
3215: /* {
3216: syscallarg(int) fd;
3217: syscallarg(const char *) path;
3218: syscallarg(char *) buf;
1.460 manu 3219: syscallarg(size_t) bufsize;
1.433 manu 3220: } */
3221:
1.460 manu 3222: return do_sys_readlinkat(l, SCARG(uap, fd), SCARG(uap, path),
3223: SCARG(uap, buf), SCARG(uap, bufsize), retval);
1.433 manu 3224: }
3225:
1.31 cgd 3226: /*
3227: * Change flags of a file given a path name.
3228: */
3229: /* ARGSUSED */
1.63 christos 3230: int
1.335 dsl 3231: sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
1.56 thorpej 3232: {
1.335 dsl 3233: /* {
1.74 cgd 3234: syscallarg(const char *) path;
3235: syscallarg(u_long) flags;
1.335 dsl 3236: } */
1.155 augustss 3237: struct vnode *vp;
1.31 cgd 3238: int error;
3239:
1.395 dholland 3240: error = namei_simple_user(SCARG(uap, path),
3241: NSM_FOLLOW_TRYEMULROOT, &vp);
3242: if (error != 0)
1.31 cgd 3243: return (error);
1.234 christos 3244: error = change_flags(vp, SCARG(uap, flags), l);
1.31 cgd 3245: vput(vp);
3246: return (error);
3247: }
3248:
3249: /*
3250: * Change flags of a file given a file descriptor.
3251: */
3252: /* ARGSUSED */
1.63 christos 3253: int
1.335 dsl 3254: sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
1.56 thorpej 3255: {
1.335 dsl 3256: /* {
1.35 cgd 3257: syscallarg(int) fd;
1.74 cgd 3258: syscallarg(u_long) flags;
1.335 dsl 3259: } */
1.31 cgd 3260: struct vnode *vp;
1.346 ad 3261: file_t *fp;
1.31 cgd 3262: int error;
3263:
1.346 ad 3264: /* fd_getvnode() will use the descriptor for us */
3265: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.31 cgd 3266: return (error);
1.491 matt 3267: vp = fp->f_vnode;
1.234 christos 3268: error = change_flags(vp, SCARG(uap, flags), l);
1.406 hannken 3269: VOP_UNLOCK(vp);
1.346 ad 3270: fd_putfile(SCARG(uap, fd));
1.156 mrg 3271: return (error);
3272: }
3273:
3274: /*
1.163 enami 3275: * Change flags of a file given a path name; this version does
1.156 mrg 3276: * not follow links.
3277: */
3278: int
1.335 dsl 3279: sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
1.156 mrg 3280: {
1.335 dsl 3281: /* {
1.156 mrg 3282: syscallarg(const char *) path;
3283: syscallarg(u_long) flags;
1.335 dsl 3284: } */
1.163 enami 3285: struct vnode *vp;
1.156 mrg 3286: int error;
3287:
1.395 dholland 3288: error = namei_simple_user(SCARG(uap, path),
3289: NSM_NOFOLLOW_TRYEMULROOT, &vp);
3290: if (error != 0)
1.156 mrg 3291: return (error);
1.234 christos 3292: error = change_flags(vp, SCARG(uap, flags), l);
1.163 enami 3293: vput(vp);
3294: return (error);
3295: }
3296:
3297: /*
3298: * Common routine to change flags of a file.
3299: */
3300: int
1.234 christos 3301: change_flags(struct vnode *vp, u_long flags, struct lwp *l)
1.163 enami 3302: {
3303: struct vattr vattr;
3304: int error;
3305:
1.156 mrg 3306: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.450 elad 3307:
1.402 pooka 3308: vattr_null(&vattr);
1.163 enami 3309: vattr.va_flags = flags;
1.332 pooka 3310: error = VOP_SETATTR(vp, &vattr, l->l_cred);
1.450 elad 3311:
1.31 cgd 3312: return (error);
3313: }
3314:
3315: /*
1.98 enami 3316: * Change mode of a file given path name; this version follows links.
1.31 cgd 3317: */
3318: /* ARGSUSED */
1.63 christos 3319: int
1.335 dsl 3320: sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
1.56 thorpej 3321: {
1.335 dsl 3322: /* {
1.74 cgd 3323: syscallarg(const char *) path;
1.35 cgd 3324: syscallarg(int) mode;
1.335 dsl 3325: } */
1.460 manu 3326: return do_sys_chmodat(l, AT_FDCWD, SCARG(uap, path),
3327: SCARG(uap, mode), 0);
3328: }
3329:
1.469 chs 3330: int
1.460 manu 3331: do_sys_chmodat(struct lwp *l, int fdat, const char *path, int mode, int flags)
3332: {
1.31 cgd 3333: int error;
1.395 dholland 3334: struct vnode *vp;
1.460 manu 3335: namei_simple_flags_t ns_flag;
3336:
3337: if (flags & AT_SYMLINK_NOFOLLOW)
3338: ns_flag = NSM_NOFOLLOW_TRYEMULROOT;
3339: else
3340: ns_flag = NSM_FOLLOW_TRYEMULROOT;
1.31 cgd 3341:
1.460 manu 3342: error = fd_nameiat_simple_user(l, fdat, path, ns_flag, &vp);
1.395 dholland 3343: if (error != 0)
1.460 manu 3344: return error;
1.97 enami 3345:
1.460 manu 3346: error = change_mode(vp, mode, l);
1.97 enami 3347:
1.395 dholland 3348: vrele(vp);
1.460 manu 3349:
1.31 cgd 3350: return (error);
3351: }
3352:
3353: /*
3354: * Change mode of a file given a file descriptor.
3355: */
3356: /* ARGSUSED */
1.63 christos 3357: int
1.335 dsl 3358: sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
1.56 thorpej 3359: {
1.335 dsl 3360: /* {
1.35 cgd 3361: syscallarg(int) fd;
3362: syscallarg(int) mode;
1.335 dsl 3363: } */
1.346 ad 3364: file_t *fp;
1.31 cgd 3365: int error;
3366:
1.346 ad 3367: /* fd_getvnode() will use the descriptor for us */
3368: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.31 cgd 3369: return (error);
1.491 matt 3370: error = change_mode(fp->f_vnode, SCARG(uap, mode), l);
1.346 ad 3371: fd_putfile(SCARG(uap, fd));
1.135 thorpej 3372: return (error);
1.97 enami 3373: }
3374:
1.433 manu 3375: int
3376: sys_fchmodat(struct lwp *l, const struct sys_fchmodat_args *uap,
3377: register_t *retval)
3378: {
3379: /* {
3380: syscallarg(int) fd;
3381: syscallarg(const char *) path;
3382: syscallarg(int) mode;
3383: syscallarg(int) flag;
3384: } */
3385:
1.460 manu 3386: return do_sys_chmodat(l, SCARG(uap, fd), SCARG(uap, path),
3387: SCARG(uap, mode), SCARG(uap, flag));
1.433 manu 3388: }
3389:
1.97 enami 3390: /*
1.98 enami 3391: * Change mode of a file given path name; this version does not follow links.
3392: */
3393: /* ARGSUSED */
3394: int
1.335 dsl 3395: sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
1.98 enami 3396: {
1.335 dsl 3397: /* {
1.98 enami 3398: syscallarg(const char *) path;
3399: syscallarg(int) mode;
1.335 dsl 3400: } */
1.98 enami 3401: int error;
1.395 dholland 3402: struct vnode *vp;
1.98 enami 3403:
1.395 dholland 3404: error = namei_simple_user(SCARG(uap, path),
3405: NSM_NOFOLLOW_TRYEMULROOT, &vp);
3406: if (error != 0)
1.98 enami 3407: return (error);
3408:
1.395 dholland 3409: error = change_mode(vp, SCARG(uap, mode), l);
1.98 enami 3410:
1.395 dholland 3411: vrele(vp);
1.98 enami 3412: return (error);
3413: }
3414:
3415: /*
1.97 enami 3416: * Common routine to set mode given a vnode.
3417: */
3418: static int
1.234 christos 3419: change_mode(struct vnode *vp, int mode, struct lwp *l)
1.97 enami 3420: {
3421: struct vattr vattr;
3422: int error;
3423:
1.113 fvdl 3424: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.402 pooka 3425: vattr_null(&vattr);
1.113 fvdl 3426: vattr.va_mode = mode & ALLPERMS;
1.332 pooka 3427: error = VOP_SETATTR(vp, &vattr, l->l_cred);
1.406 hannken 3428: VOP_UNLOCK(vp);
1.31 cgd 3429: return (error);
3430: }
3431:
3432: /*
1.98 enami 3433: * Set ownership given a path name; this version follows links.
1.31 cgd 3434: */
3435: /* ARGSUSED */
1.63 christos 3436: int
1.335 dsl 3437: sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
1.56 thorpej 3438: {
1.335 dsl 3439: /* {
1.74 cgd 3440: syscallarg(const char *) path;
3441: syscallarg(uid_t) uid;
3442: syscallarg(gid_t) gid;
1.335 dsl 3443: } */
1.460 manu 3444: return do_sys_chownat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap,uid),
3445: SCARG(uap, gid), 0);
3446: }
3447:
1.469 chs 3448: int
1.460 manu 3449: do_sys_chownat(struct lwp *l, int fdat, const char *path, uid_t uid,
3450: gid_t gid, int flags)
3451: {
1.31 cgd 3452: int error;
1.395 dholland 3453: struct vnode *vp;
1.460 manu 3454: namei_simple_flags_t ns_flag;
3455:
3456: if (flags & AT_SYMLINK_NOFOLLOW)
3457: ns_flag = NSM_NOFOLLOW_TRYEMULROOT;
3458: else
3459: ns_flag = NSM_FOLLOW_TRYEMULROOT;
1.31 cgd 3460:
1.460 manu 3461: error = fd_nameiat_simple_user(l, fdat, path, ns_flag, &vp);
1.395 dholland 3462: if (error != 0)
1.460 manu 3463: return error;
1.86 kleink 3464:
1.460 manu 3465: error = change_owner(vp, uid, gid, l, 0);
1.112 kleink 3466:
1.395 dholland 3467: vrele(vp);
1.460 manu 3468:
1.112 kleink 3469: return (error);
3470: }
3471:
3472: /*
3473: * Set ownership given a path name; this version follows links.
3474: * Provides POSIX semantics.
3475: */
3476: /* ARGSUSED */
3477: int
1.335 dsl 3478: sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
1.112 kleink 3479: {
1.335 dsl 3480: /* {
1.112 kleink 3481: syscallarg(const char *) path;
3482: syscallarg(uid_t) uid;
3483: syscallarg(gid_t) gid;
1.335 dsl 3484: } */
1.112 kleink 3485: int error;
1.395 dholland 3486: struct vnode *vp;
1.112 kleink 3487:
1.395 dholland 3488: error = namei_simple_user(SCARG(uap, path),
3489: NSM_FOLLOW_TRYEMULROOT, &vp);
3490: if (error != 0)
1.112 kleink 3491: return (error);
3492:
1.395 dholland 3493: error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
1.86 kleink 3494:
1.395 dholland 3495: vrele(vp);
1.31 cgd 3496: return (error);
3497: }
3498:
3499: /*
3500: * Set ownership given a file descriptor.
3501: */
3502: /* ARGSUSED */
1.63 christos 3503: int
1.335 dsl 3504: sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
1.56 thorpej 3505: {
1.335 dsl 3506: /* {
1.35 cgd 3507: syscallarg(int) fd;
1.74 cgd 3508: syscallarg(uid_t) uid;
3509: syscallarg(gid_t) gid;
1.335 dsl 3510: } */
1.71 mycroft 3511: int error;
1.346 ad 3512: file_t *fp;
1.31 cgd 3513:
1.346 ad 3514: /* fd_getvnode() will use the descriptor for us */
3515: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.31 cgd 3516: return (error);
1.491 matt 3517: error = change_owner(fp->f_vnode, SCARG(uap, uid), SCARG(uap, gid),
1.346 ad 3518: l, 0);
3519: fd_putfile(SCARG(uap, fd));
1.135 thorpej 3520: return (error);
1.112 kleink 3521: }
3522:
1.433 manu 3523: int
3524: sys_fchownat(struct lwp *l, const struct sys_fchownat_args *uap,
3525: register_t *retval)
3526: {
3527: /* {
3528: syscallarg(int) fd;
3529: syscallarg(const char *) path;
1.460 manu 3530: syscallarg(uid_t) owner;
3531: syscallarg(gid_t) group;
1.433 manu 3532: syscallarg(int) flag;
3533: } */
3534:
1.460 manu 3535: return do_sys_chownat(l, SCARG(uap, fd), SCARG(uap, path),
3536: SCARG(uap, owner), SCARG(uap, group),
3537: SCARG(uap, flag));
1.433 manu 3538: }
3539:
1.112 kleink 3540: /*
3541: * Set ownership given a file descriptor, providing POSIX/XPG semantics.
3542: */
3543: /* ARGSUSED */
3544: int
1.335 dsl 3545: sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
1.112 kleink 3546: {
1.335 dsl 3547: /* {
1.112 kleink 3548: syscallarg(int) fd;
3549: syscallarg(uid_t) uid;
3550: syscallarg(gid_t) gid;
1.335 dsl 3551: } */
1.112 kleink 3552: int error;
1.346 ad 3553: file_t *fp;
1.112 kleink 3554:
1.346 ad 3555: /* fd_getvnode() will use the descriptor for us */
3556: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.112 kleink 3557: return (error);
1.491 matt 3558: error = change_owner(fp->f_vnode, SCARG(uap, uid), SCARG(uap, gid),
1.346 ad 3559: l, 1);
3560: fd_putfile(SCARG(uap, fd));
1.135 thorpej 3561: return (error);
1.86 kleink 3562: }
3563:
3564: /*
1.98 enami 3565: * Set ownership given a path name; this version does not follow links.
3566: */
3567: /* ARGSUSED */
3568: int
1.335 dsl 3569: sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
1.98 enami 3570: {
1.335 dsl 3571: /* {
1.98 enami 3572: syscallarg(const char *) path;
3573: syscallarg(uid_t) uid;
3574: syscallarg(gid_t) gid;
1.335 dsl 3575: } */
1.98 enami 3576: int error;
1.395 dholland 3577: struct vnode *vp;
1.98 enami 3578:
1.395 dholland 3579: error = namei_simple_user(SCARG(uap, path),
3580: NSM_NOFOLLOW_TRYEMULROOT, &vp);
3581: if (error != 0)
1.98 enami 3582: return (error);
3583:
1.395 dholland 3584: error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
1.112 kleink 3585:
1.395 dholland 3586: vrele(vp);
1.112 kleink 3587: return (error);
3588: }
3589:
3590: /*
3591: * Set ownership given a path name; this version does not follow links.
3592: * Provides POSIX/XPG semantics.
3593: */
3594: /* ARGSUSED */
3595: int
1.335 dsl 3596: sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
1.112 kleink 3597: {
1.335 dsl 3598: /* {
1.112 kleink 3599: syscallarg(const char *) path;
3600: syscallarg(uid_t) uid;
3601: syscallarg(gid_t) gid;
1.335 dsl 3602: } */
1.112 kleink 3603: int error;
1.395 dholland 3604: struct vnode *vp;
1.112 kleink 3605:
1.395 dholland 3606: error = namei_simple_user(SCARG(uap, path),
3607: NSM_NOFOLLOW_TRYEMULROOT, &vp);
3608: if (error != 0)
1.112 kleink 3609: return (error);
3610:
1.395 dholland 3611: error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
1.98 enami 3612:
1.395 dholland 3613: vrele(vp);
1.98 enami 3614: return (error);
3615: }
3616:
3617: /*
1.205 junyoung 3618: * Common routine to set ownership given a vnode.
1.86 kleink 3619: */
3620: static int
1.234 christos 3621: change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
1.221 thorpej 3622: int posix_semantics)
1.86 kleink 3623: {
3624: struct vattr vattr;
1.112 kleink 3625: mode_t newmode;
1.86 kleink 3626: int error;
3627:
1.113 fvdl 3628: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.332 pooka 3629: if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
1.86 kleink 3630: goto out;
3631:
1.175 thorpej 3632: #define CHANGED(x) ((int)(x) != -1)
1.112 kleink 3633: newmode = vattr.va_mode;
3634: if (posix_semantics) {
3635: /*
1.114 kleink 3636: * POSIX/XPG semantics: if the caller is not the super-user,
3637: * clear set-user-id and set-group-id bits. Both POSIX and
3638: * the XPG consider the behaviour for calls by the super-user
3639: * implementation-defined; we leave the set-user-id and set-
3640: * group-id settings intact in that case.
1.112 kleink 3641: */
1.450 elad 3642: if (vattr.va_mode & S_ISUID) {
1.451 christos 3643: if (kauth_authorize_vnode(l->l_cred,
3644: KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
1.450 elad 3645: newmode &= ~S_ISUID;
3646: }
3647: if (vattr.va_mode & S_ISGID) {
1.451 christos 3648: if (kauth_authorize_vnode(l->l_cred,
3649: KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
1.450 elad 3650: newmode &= ~S_ISGID;
3651: }
1.112 kleink 3652: } else {
3653: /*
3654: * NetBSD semantics: when changing owner and/or group,
3655: * clear the respective bit(s).
3656: */
3657: if (CHANGED(uid))
3658: newmode &= ~S_ISUID;
3659: if (CHANGED(gid))
3660: newmode &= ~S_ISGID;
3661: }
3662: /* Update va_mode iff altered. */
3663: if (vattr.va_mode == newmode)
3664: newmode = VNOVAL;
1.205 junyoung 3665:
1.402 pooka 3666: vattr_null(&vattr);
1.175 thorpej 3667: vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
3668: vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
1.86 kleink 3669: vattr.va_mode = newmode;
1.332 pooka 3670: error = VOP_SETATTR(vp, &vattr, l->l_cred);
1.112 kleink 3671: #undef CHANGED
1.205 junyoung 3672:
1.86 kleink 3673: out:
1.406 hannken 3674: VOP_UNLOCK(vp);
1.31 cgd 3675: return (error);
3676: }
3677:
3678: /*
1.98 enami 3679: * Set the access and modification times given a path name; this
3680: * version follows links.
1.31 cgd 3681: */
3682: /* ARGSUSED */
1.63 christos 3683: int
1.383 christos 3684: sys___utimes50(struct lwp *l, const struct sys___utimes50_args *uap,
3685: register_t *retval)
1.56 thorpej 3686: {
1.335 dsl 3687: /* {
1.74 cgd 3688: syscallarg(const char *) path;
3689: syscallarg(const struct timeval *) tptr;
1.335 dsl 3690: } */
1.31 cgd 3691:
1.312 dsl 3692: return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
1.346 ad 3693: SCARG(uap, tptr), UIO_USERSPACE);
1.71 mycroft 3694: }
3695:
3696: /*
3697: * Set the access and modification times given a file descriptor.
3698: */
3699: /* ARGSUSED */
3700: int
1.383 christos 3701: sys___futimes50(struct lwp *l, const struct sys___futimes50_args *uap,
3702: register_t *retval)
1.71 mycroft 3703: {
1.335 dsl 3704: /* {
1.71 mycroft 3705: syscallarg(int) fd;
1.74 cgd 3706: syscallarg(const struct timeval *) tptr;
1.335 dsl 3707: } */
1.71 mycroft 3708: int error;
1.346 ad 3709: file_t *fp;
1.71 mycroft 3710:
1.346 ad 3711: /* fd_getvnode() will use the descriptor for us */
3712: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.96 enami 3713: return (error);
1.491 matt 3714: error = do_sys_utimes(l, fp->f_vnode, NULL, 0, SCARG(uap, tptr),
1.346 ad 3715: UIO_USERSPACE);
3716: fd_putfile(SCARG(uap, fd));
1.135 thorpej 3717: return (error);
1.98 enami 3718: }
3719:
1.434 manu 3720: int
3721: sys_futimens(struct lwp *l, const struct sys_futimens_args *uap,
3722: register_t *retval)
3723: {
3724: /* {
3725: syscallarg(int) fd;
3726: syscallarg(const struct timespec *) tptr;
3727: } */
3728: int error;
3729: file_t *fp;
3730:
3731: /* fd_getvnode() will use the descriptor for us */
3732: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3733: return (error);
1.491 matt 3734: error = do_sys_utimensat(l, AT_FDCWD, fp->f_vnode, NULL, 0,
1.460 manu 3735: SCARG(uap, tptr), UIO_USERSPACE);
1.434 manu 3736: fd_putfile(SCARG(uap, fd));
3737: return (error);
3738: }
3739:
1.98 enami 3740: /*
3741: * Set the access and modification times given a path name; this
3742: * version does not follow links.
3743: */
3744: int
1.383 christos 3745: sys___lutimes50(struct lwp *l, const struct sys___lutimes50_args *uap,
3746: register_t *retval)
1.98 enami 3747: {
1.335 dsl 3748: /* {
1.98 enami 3749: syscallarg(const char *) path;
3750: syscallarg(const struct timeval *) tptr;
1.335 dsl 3751: } */
1.98 enami 3752:
1.312 dsl 3753: return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
1.346 ad 3754: SCARG(uap, tptr), UIO_USERSPACE);
1.97 enami 3755: }
3756:
1.433 manu 3757: int
3758: sys_utimensat(struct lwp *l, const struct sys_utimensat_args *uap,
3759: register_t *retval)
3760: {
3761: /* {
3762: syscallarg(int) fd;
3763: syscallarg(const char *) path;
3764: syscallarg(const struct timespec *) tptr;
3765: syscallarg(int) flag;
3766: } */
1.434 manu 3767: int follow;
3768: const struct timespec *tptr;
1.460 manu 3769: int error;
1.434 manu 3770:
3771: tptr = SCARG(uap, tptr);
3772: follow = (SCARG(uap, flag) & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
3773:
1.460 manu 3774: error = do_sys_utimensat(l, SCARG(uap, fd), NULL,
3775: SCARG(uap, path), follow, tptr, UIO_USERSPACE);
3776:
3777: return error;
1.433 manu 3778: }
3779:
1.97 enami 3780: /*
3781: * Common routine to set access and modification times given a vnode.
3782: */
1.312 dsl 3783: int
1.434 manu 3784: do_sys_utimens(struct lwp *l, struct vnode *vp, const char *path, int flag,
3785: const struct timespec *tptr, enum uio_seg seg)
1.97 enami 3786: {
1.460 manu 3787: return do_sys_utimensat(l, AT_FDCWD, vp, path, flag, tptr, seg);
3788: }
3789:
1.466 matt 3790: int
1.460 manu 3791: do_sys_utimensat(struct lwp *l, int fdat, struct vnode *vp,
3792: const char *path, int flag, const struct timespec *tptr, enum uio_seg seg)
3793: {
1.97 enami 3794: struct vattr vattr;
1.395 dholland 3795: int error, dorele = 0;
3796: namei_simple_flags_t sflags;
1.367 christos 3797: bool vanull, setbirthtime;
3798: struct timespec ts[2];
1.97 enami 3799:
1.460 manu 3800: KASSERT(l != NULL || fdat == AT_FDCWD);
3801:
1.395 dholland 3802: /*
3803: * I have checked all callers and they pass either FOLLOW,
3804: * NOFOLLOW, or 0 (when they don't pass a path), and NOFOLLOW
3805: * is 0. More to the point, they don't pass anything else.
3806: * Let's keep it that way at least until the namei interfaces
3807: * are fully sanitized.
3808: */
3809: KASSERT(flag == NOFOLLOW || flag == FOLLOW);
3810: sflags = (flag == FOLLOW) ?
3811: NSM_FOLLOW_TRYEMULROOT : NSM_NOFOLLOW_TRYEMULROOT;
3812:
1.97 enami 3813: if (tptr == NULL) {
1.367 christos 3814: vanull = true;
3815: nanotime(&ts[0]);
3816: ts[1] = ts[0];
1.71 mycroft 3817: } else {
1.367 christos 3818: vanull = false;
1.312 dsl 3819: if (seg != UIO_SYSSPACE) {
1.434 manu 3820: error = copyin(tptr, ts, sizeof (ts));
1.312 dsl 3821: if (error != 0)
3822: return error;
1.437 manu 3823: } else {
3824: ts[0] = tptr[0];
3825: ts[1] = tptr[1];
1.312 dsl 3826: }
1.71 mycroft 3827: }
1.312 dsl 3828:
1.438 enami 3829: if (ts[0].tv_nsec == UTIME_NOW) {
1.434 manu 3830: nanotime(&ts[0]);
1.438 enami 3831: if (ts[1].tv_nsec == UTIME_NOW) {
3832: vanull = true;
3833: ts[1] = ts[0];
3834: }
3835: } else if (ts[1].tv_nsec == UTIME_NOW)
1.434 manu 3836: nanotime(&ts[1]);
3837:
1.312 dsl 3838: if (vp == NULL) {
1.395 dholland 3839: /* note: SEG describes TPTR, not PATH; PATH is always user */
1.460 manu 3840: error = fd_nameiat_simple_user(l, fdat, path, sflags, &vp);
1.395 dholland 3841: if (error != 0)
1.367 christos 3842: return error;
1.395 dholland 3843: dorele = 1;
3844: }
1.312 dsl 3845:
1.113 fvdl 3846: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.367 christos 3847: setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 &&
3848: timespeccmp(&ts[1], &vattr.va_birthtime, <));
1.402 pooka 3849: vattr_null(&vattr);
1.434 manu 3850:
3851: if (ts[0].tv_nsec != UTIME_OMIT)
3852: vattr.va_atime = ts[0];
3853:
3854: if (ts[1].tv_nsec != UTIME_OMIT) {
3855: vattr.va_mtime = ts[1];
3856: if (setbirthtime)
3857: vattr.va_birthtime = ts[1];
3858: }
3859:
1.367 christos 3860: if (vanull)
1.392 yamt 3861: vattr.va_vaflags |= VA_UTIMES_NULL;
1.332 pooka 3862: error = VOP_SETATTR(vp, &vattr, l->l_cred);
1.406 hannken 3863: VOP_UNLOCK(vp);
1.312 dsl 3864:
1.395 dholland 3865: if (dorele != 0)
3866: vrele(vp);
1.312 dsl 3867:
1.367 christos 3868: return error;
1.31 cgd 3869: }
3870:
1.434 manu 3871: int
3872: do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
3873: const struct timeval *tptr, enum uio_seg seg)
3874: {
3875: struct timespec ts[2];
3876: struct timespec *tsptr = NULL;
3877: int error;
3878:
3879: if (tptr != NULL) {
3880: struct timeval tv[2];
3881:
3882: if (seg != UIO_SYSSPACE) {
3883: error = copyin(tptr, tv, sizeof (tv));
3884: if (error != 0)
3885: return error;
3886: tptr = tv;
3887: }
3888:
3889: if ((tv[0].tv_usec == UTIME_NOW) ||
3890: (tv[0].tv_usec == UTIME_OMIT))
3891: ts[0].tv_nsec = tv[0].tv_usec;
3892: else
3893: TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]);
3894:
3895: if ((tv[1].tv_usec == UTIME_NOW) ||
3896: (tv[1].tv_usec == UTIME_OMIT))
3897: ts[1].tv_nsec = tv[1].tv_usec;
3898: else
3899: TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]);
3900:
3901: tsptr = &ts[0];
3902: }
3903:
3904: return do_sys_utimens(l, vp, path, flag, tsptr, UIO_SYSSPACE);
3905: }
3906:
1.31 cgd 3907: /*
3908: * Truncate a file given its path name.
3909: */
3910: /* ARGSUSED */
1.63 christos 3911: int
1.335 dsl 3912: sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
1.56 thorpej 3913: {
1.335 dsl 3914: /* {
1.74 cgd 3915: syscallarg(const char *) path;
1.35 cgd 3916: syscallarg(int) pad;
3917: syscallarg(off_t) length;
1.335 dsl 3918: } */
1.155 augustss 3919: struct vnode *vp;
1.31 cgd 3920: struct vattr vattr;
3921: int error;
3922:
1.484 njoly 3923: if (SCARG(uap, length) < 0)
3924: return EINVAL;
3925:
1.395 dholland 3926: error = namei_simple_user(SCARG(uap, path),
3927: NSM_FOLLOW_TRYEMULROOT, &vp);
3928: if (error != 0)
1.31 cgd 3929: return (error);
1.113 fvdl 3930: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.31 cgd 3931: if (vp->v_type == VDIR)
3932: error = EISDIR;
3933: else if ((error = vn_writechk(vp)) == 0 &&
1.332 pooka 3934: (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
1.402 pooka 3935: vattr_null(&vattr);
1.35 cgd 3936: vattr.va_size = SCARG(uap, length);
1.332 pooka 3937: error = VOP_SETATTR(vp, &vattr, l->l_cred);
1.31 cgd 3938: }
3939: vput(vp);
3940: return (error);
3941: }
3942:
3943: /*
3944: * Truncate a file given a file descriptor.
3945: */
3946: /* ARGSUSED */
1.63 christos 3947: int
1.335 dsl 3948: sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
1.56 thorpej 3949: {
1.335 dsl 3950: /* {
1.35 cgd 3951: syscallarg(int) fd;
3952: syscallarg(int) pad;
3953: syscallarg(off_t) length;
1.335 dsl 3954: } */
1.31 cgd 3955: struct vattr vattr;
3956: struct vnode *vp;
1.346 ad 3957: file_t *fp;
1.31 cgd 3958: int error;
3959:
1.484 njoly 3960: if (SCARG(uap, length) < 0)
3961: return EINVAL;
3962:
1.346 ad 3963: /* fd_getvnode() will use the descriptor for us */
3964: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.31 cgd 3965: return (error);
1.135 thorpej 3966: if ((fp->f_flag & FWRITE) == 0) {
3967: error = EINVAL;
3968: goto out;
3969: }
1.491 matt 3970: vp = fp->f_vnode;
1.113 fvdl 3971: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.31 cgd 3972: if (vp->v_type == VDIR)
3973: error = EISDIR;
3974: else if ((error = vn_writechk(vp)) == 0) {
1.402 pooka 3975: vattr_null(&vattr);
1.35 cgd 3976: vattr.va_size = SCARG(uap, length);
1.332 pooka 3977: error = VOP_SETATTR(vp, &vattr, fp->f_cred);
1.31 cgd 3978: }
1.406 hannken 3979: VOP_UNLOCK(vp);
1.135 thorpej 3980: out:
1.346 ad 3981: fd_putfile(SCARG(uap, fd));
1.31 cgd 3982: return (error);
3983: }
3984:
3985: /*
3986: * Sync an open file.
3987: */
3988: /* ARGSUSED */
1.63 christos 3989: int
1.335 dsl 3990: sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
1.56 thorpej 3991: {
1.335 dsl 3992: /* {
1.35 cgd 3993: syscallarg(int) fd;
1.335 dsl 3994: } */
1.155 augustss 3995: struct vnode *vp;
1.346 ad 3996: file_t *fp;
1.31 cgd 3997: int error;
3998:
1.346 ad 3999: /* fd_getvnode() will use the descriptor for us */
4000: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.31 cgd 4001: return (error);
1.491 matt 4002: vp = fp->f_vnode;
1.113 fvdl 4003: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.332 pooka 4004: error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
1.406 hannken 4005: VOP_UNLOCK(vp);
1.346 ad 4006: fd_putfile(SCARG(uap, fd));
1.201 thorpej 4007: return (error);
4008: }
4009:
4010: /*
4011: * Sync a range of file data. API modeled after that found in AIX.
4012: *
4013: * FDATASYNC indicates that we need only save enough metadata to be able
4014: * to re-read the written data. Note we duplicate AIX's requirement that
4015: * the file be open for writing.
4016: */
4017: /* ARGSUSED */
4018: int
1.335 dsl 4019: sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
1.201 thorpej 4020: {
1.335 dsl 4021: /* {
1.201 thorpej 4022: syscallarg(int) fd;
4023: syscallarg(int) flags;
4024: syscallarg(off_t) start;
1.225 cube 4025: syscallarg(off_t) length;
1.335 dsl 4026: } */
1.201 thorpej 4027: struct vnode *vp;
1.346 ad 4028: file_t *fp;
1.201 thorpej 4029: int flags, nflags;
4030: off_t s, e, len;
4031: int error;
4032:
1.346 ad 4033: /* fd_getvnode() will use the descriptor for us */
4034: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.201 thorpej 4035: return (error);
4036:
4037: if ((fp->f_flag & FWRITE) == 0) {
1.293 wrstuden 4038: error = EBADF;
4039: goto out;
1.201 thorpej 4040: }
4041:
4042: flags = SCARG(uap, flags);
4043: if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
4044: ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
1.293 wrstuden 4045: error = EINVAL;
4046: goto out;
1.201 thorpej 4047: }
4048: /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
4049: if (flags & FDATASYNC)
4050: nflags = FSYNC_DATAONLY | FSYNC_WAIT;
4051: else
4052: nflags = FSYNC_WAIT;
1.216 wrstuden 4053: if (flags & FDISKSYNC)
4054: nflags |= FSYNC_CACHE;
1.201 thorpej 4055:
4056: len = SCARG(uap, length);
1.424 dsl 4057: /* If length == 0, we do the whole file, and s = e = 0 will do that */
1.201 thorpej 4058: if (len) {
4059: s = SCARG(uap, start);
4060: e = s + len;
4061: if (e < s) {
1.293 wrstuden 4062: error = EINVAL;
4063: goto out;
1.201 thorpej 4064: }
4065: } else {
4066: e = 0;
4067: s = 0;
4068: }
4069:
1.491 matt 4070: vp = fp->f_vnode;
1.201 thorpej 4071: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.332 pooka 4072: error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
1.406 hannken 4073: VOP_UNLOCK(vp);
1.293 wrstuden 4074: out:
1.346 ad 4075: fd_putfile(SCARG(uap, fd));
1.31 cgd 4076: return (error);
4077: }
4078:
4079: /*
1.117 kleink 4080: * Sync the data of an open file.
4081: */
4082: /* ARGSUSED */
4083: int
1.335 dsl 4084: sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
1.117 kleink 4085: {
1.335 dsl 4086: /* {
1.117 kleink 4087: syscallarg(int) fd;
1.335 dsl 4088: } */
1.117 kleink 4089: struct vnode *vp;
1.346 ad 4090: file_t *fp;
1.117 kleink 4091: int error;
4092:
1.346 ad 4093: /* fd_getvnode() will use the descriptor for us */
4094: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.117 kleink 4095: return (error);
1.199 kleink 4096: if ((fp->f_flag & FWRITE) == 0) {
1.346 ad 4097: fd_putfile(SCARG(uap, fd));
1.199 kleink 4098: return (EBADF);
4099: }
1.491 matt 4100: vp = fp->f_vnode;
1.117 kleink 4101: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.332 pooka 4102: error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
1.406 hannken 4103: VOP_UNLOCK(vp);
1.346 ad 4104: fd_putfile(SCARG(uap, fd));
1.117 kleink 4105: return (error);
4106: }
4107:
4108: /*
1.90 kleink 4109: * Rename files, (standard) BSD semantics frontend.
1.31 cgd 4110: */
4111: /* ARGSUSED */
1.63 christos 4112: int
1.335 dsl 4113: sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
1.56 thorpej 4114: {
1.335 dsl 4115: /* {
1.74 cgd 4116: syscallarg(const char *) from;
4117: syscallarg(const char *) to;
1.335 dsl 4118: } */
1.90 kleink 4119:
1.460 manu 4120: return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
4121: SCARG(uap, to), UIO_USERSPACE, 0));
1.90 kleink 4122: }
4123:
1.433 manu 4124: int
4125: sys_renameat(struct lwp *l, const struct sys_renameat_args *uap,
4126: register_t *retval)
4127: {
4128: /* {
4129: syscallarg(int) fromfd;
4130: syscallarg(const char *) from;
4131: syscallarg(int) tofd;
4132: syscallarg(const char *) to;
4133: } */
4134:
1.460 manu 4135: return (do_sys_renameat(l, SCARG(uap, fromfd), SCARG(uap, from),
4136: SCARG(uap, tofd), SCARG(uap, to), UIO_USERSPACE, 0));
1.433 manu 4137: }
4138:
1.90 kleink 4139: /*
4140: * Rename files, POSIX semantics frontend.
4141: */
4142: /* ARGSUSED */
4143: int
1.335 dsl 4144: sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
1.90 kleink 4145: {
1.335 dsl 4146: /* {
1.90 kleink 4147: syscallarg(const char *) from;
4148: syscallarg(const char *) to;
1.335 dsl 4149: } */
1.90 kleink 4150:
1.460 manu 4151: return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
4152: SCARG(uap, to), UIO_USERSPACE, 1));
1.90 kleink 4153: }
4154:
4155: /*
4156: * Rename files. Source and destination must either both be directories,
4157: * or both not be directories. If target is a directory, it must be empty.
4158: * If `from' and `to' refer to the same object, the value of the `retain'
4159: * argument is used to determine whether `from' will be
4160: *
4161: * (retain == 0) deleted unless `from' and `to' refer to the same
4162: * object in the file system's name space (BSD).
4163: * (retain == 1) always retained (POSIX).
1.458 riastrad 4164: *
4165: * XXX Synchronize with nfsrv_rename in nfs_serv.c.
1.90 kleink 4166: */
1.336 ad 4167: int
4168: do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
1.90 kleink 4169: {
1.460 manu 4170: return do_sys_renameat(NULL, AT_FDCWD, from, AT_FDCWD, to, seg, retain);
4171: }
4172:
4173: static int
4174: do_sys_renameat(struct lwp *l, int fromfd, const char *from, int tofd,
4175: const char *to, enum uio_seg seg, int retain)
4176: {
1.458 riastrad 4177: struct pathbuf *fpb, *tpb;
4178: struct nameidata fnd, tnd;
4179: struct vnode *fdvp, *fvp;
4180: struct vnode *tdvp, *tvp;
4181: struct mount *mp, *tmp;
1.31 cgd 4182: int error;
4183:
1.460 manu 4184: KASSERT(l != NULL || (fromfd == AT_FDCWD && tofd == AT_FDCWD));
1.458 riastrad 4185:
4186: error = pathbuf_maybe_copyin(from, seg, &fpb);
4187: if (error)
4188: goto out0;
4189: KASSERT(fpb != NULL);
4190:
4191: error = pathbuf_maybe_copyin(to, seg, &tpb);
4192: if (error)
4193: goto out1;
4194: KASSERT(tpb != NULL);
4195:
4196: /*
4197: * Lookup from.
4198: *
4199: * XXX LOCKPARENT is wrong because we don't actually want it
4200: * locked yet, but (a) namei is insane, and (b) VOP_RENAME is
4201: * insane, so for the time being we need to leave it like this.
4202: */
1.497 riastrad 4203: NDINIT(&fnd, DELETE, (LOCKPARENT | TRYEMULROOT), fpb);
1.460 manu 4204: if ((error = fd_nameiat(l, fromfd, &fnd)) != 0)
1.458 riastrad 4205: goto out2;
4206:
4207: /*
4208: * Pull out the important results of the lookup, fdvp and fvp.
4209: * Of course, fvp is bogus because we're about to unlock fdvp.
4210: */
4211: fdvp = fnd.ni_dvp;
4212: fvp = fnd.ni_vp;
4213: KASSERT(fdvp != NULL);
4214: KASSERT(fvp != NULL);
4215: KASSERT((fdvp == fvp) || (VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE));
4216:
4217: /*
4218: * Make sure neither fdvp nor fvp is locked.
4219: */
4220: if (fdvp != fvp)
4221: VOP_UNLOCK(fdvp);
4222: /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4223: /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4224:
4225: /*
4226: * Reject renaming `.' and `..'. Can't do this until after
4227: * namei because we need namei's parsing to find the final
4228: * component name. (namei should just leave us with the final
4229: * component name and not look it up itself, but anyway...)
4230: *
4231: * This was here before because we used to relookup from
4232: * instead of to and relookup requires the caller to check
4233: * this, but now file systems may depend on this check, so we
4234: * must retain it until the file systems are all rototilled.
4235: */
4236: if (((fnd.ni_cnd.cn_namelen == 1) &&
4237: (fnd.ni_cnd.cn_nameptr[0] == '.')) ||
4238: ((fnd.ni_cnd.cn_namelen == 2) &&
4239: (fnd.ni_cnd.cn_nameptr[0] == '.') &&
4240: (fnd.ni_cnd.cn_nameptr[1] == '.'))) {
4241: error = EINVAL; /* XXX EISDIR? */
4242: goto abort0;
1.409 dholland 4243: }
1.458 riastrad 4244:
4245: /*
4246: * Lookup to.
4247: *
4248: * XXX LOCKPARENT is wrong, but...insanity, &c. Also, using
4249: * fvp here to decide whether to add CREATEDIR is a load of
4250: * bollocks because fvp might be the wrong node by now, since
4251: * fdvp is unlocked.
4252: *
4253: * XXX Why not pass CREATEDIR always?
4254: */
4255: NDINIT(&tnd, RENAME,
1.497 riastrad 4256: (LOCKPARENT | NOCACHE | TRYEMULROOT |
1.458 riastrad 4257: ((fvp->v_type == VDIR)? CREATEDIR : 0)),
4258: tpb);
1.460 manu 4259: if ((error = fd_nameiat(l, tofd, &tnd)) != 0)
1.458 riastrad 4260: goto abort0;
4261:
4262: /*
4263: * Pull out the important results of the lookup, tdvp and tvp.
4264: * Of course, tvp is bogus because we're about to unlock tdvp.
4265: */
4266: tdvp = tnd.ni_dvp;
4267: tvp = tnd.ni_vp;
4268: KASSERT(tdvp != NULL);
4269: KASSERT((tdvp == tvp) || (VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE));
4270:
4271: /*
4272: * Make sure neither tdvp nor tvp is locked.
4273: */
4274: if (tdvp != tvp)
4275: VOP_UNLOCK(tdvp);
4276: /* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
4277: /* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
4278:
4279: /*
4280: * Reject renaming onto `.' or `..'. relookup is unhappy with
4281: * these, which is why we must do this here. Once upon a time
4282: * we relooked up from instead of to, and consequently didn't
4283: * need this check, but now that we relookup to instead of
4284: * from, we need this; and we shall need it forever forward
4285: * until the VOP_RENAME protocol changes, because file systems
4286: * will no doubt begin to depend on this check.
4287: */
1.495 riastrad 4288: if ((tnd.ni_cnd.cn_namelen == 1) && (tnd.ni_cnd.cn_nameptr[0] == '.')) {
1.494 riastrad 4289: error = EISDIR;
1.458 riastrad 4290: goto abort1;
1.409 dholland 4291: }
1.495 riastrad 4292: if ((tnd.ni_cnd.cn_namelen == 2) &&
4293: (tnd.ni_cnd.cn_nameptr[0] == '.') &&
4294: (tnd.ni_cnd.cn_nameptr[1] == '.')) {
4295: error = EINVAL;
4296: goto abort1;
4297: }
1.409 dholland 4298:
1.458 riastrad 4299: /*
4300: * Get the mount point. If the file system has been unmounted,
4301: * which it may be because we're not holding any vnode locks,
4302: * then v_mount will be NULL. We're not really supposed to
4303: * read v_mount without holding the vnode lock, but since we
4304: * have fdvp referenced, if fdvp->v_mount changes then at worst
4305: * it will be set to NULL, not changed to another mount point.
4306: * And, of course, since it is up to the file system to
4307: * determine the real lock order, we can't lock both fdvp and
4308: * tdvp at the same time.
4309: */
4310: mp = fdvp->v_mount;
4311: if (mp == NULL) {
4312: error = ENOENT;
4313: goto abort1;
1.344 dholland 4314: }
4315:
4316: /*
1.458 riastrad 4317: * Make sure the mount points match. Again, although we don't
4318: * hold any vnode locks, the v_mount fields may change -- but
4319: * at worst they will change to NULL, so this will never become
4320: * a cross-device rename, because we hold vnode references.
1.344 dholland 4321: *
1.458 riastrad 4322: * XXX Because nothing is locked and the compiler may reorder
4323: * things here, unmounting the file system at an inopportune
1.505 dholland 4324: * moment may cause rename to fail with EXDEV when it really
1.458 riastrad 4325: * should fail with ENOENT.
1.344 dholland 4326: */
1.458 riastrad 4327: tmp = tdvp->v_mount;
4328: if (tmp == NULL) {
4329: error = ENOENT;
4330: goto abort1;
1.348 dholland 4331: }
1.458 riastrad 4332:
4333: if (mp != tmp) {
4334: error = EXDEV;
4335: goto abort1;
1.344 dholland 4336: }
1.458 riastrad 4337:
4338: /*
4339: * Take the vfs rename lock to avoid cross-directory screw cases.
4340: * Nothing is locked currently, so taking this lock is safe.
4341: */
1.459 riastrad 4342: error = VFS_RENAMELOCK_ENTER(mp);
4343: if (error)
4344: goto abort1;
1.90 kleink 4345:
1.458 riastrad 4346: /*
4347: * Now fdvp, fvp, tdvp, and (if nonnull) tvp are referenced,
4348: * and nothing is locked except for the vfs rename lock.
4349: *
4350: * The next step is a little rain dance to conform to the
4351: * insane lock protocol, even though it does nothing to ward
4352: * off race conditions.
4353: *
4354: * We need tdvp and tvp to be locked. However, because we have
4355: * unlocked tdvp in order to hold no locks while we take the
4356: * vfs rename lock, tvp may be wrong here, and we can't safely
4357: * lock it even if the sensible file systems will just unlock
4358: * it straight away. Consequently, we must lock tdvp and then
4359: * relookup tvp to get it locked.
4360: *
4361: * Finally, because the VOP_RENAME protocol is brain-damaged
4362: * and various file systems insanely depend on the semantics of
4363: * this brain damage, the lookup of to must be the last lookup
4364: * before VOP_RENAME.
4365: */
4366: vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
4367: error = relookup(tdvp, &tnd.ni_vp, &tnd.ni_cnd, 0);
4368: if (error)
4369: goto abort2;
4370:
4371: /*
4372: * Drop the old tvp and pick up the new one -- which might be
4373: * the same, but that doesn't matter to us. After this, tdvp
4374: * and tvp should both be locked.
4375: */
4376: if (tvp != NULL)
4377: vrele(tvp);
4378: tvp = tnd.ni_vp;
4379: KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
4380: KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
4381:
4382: /*
4383: * The old do_sys_rename had various consistency checks here
4384: * involving fvp and tvp. fvp is bogus already here, and tvp
4385: * will become bogus soon in any sensible file system, so the
4386: * only purpose in putting these checks here is to give lip
4387: * service to these screw cases and to acknowledge that they
4388: * exist, not actually to handle them, but here you go
4389: * anyway...
4390: */
4391:
4392: /*
4393: * Acknowledge that directories and non-directories aren't
4394: * suposed to mix.
4395: */
1.31 cgd 4396: if (tvp != NULL) {
1.458 riastrad 4397: if ((fvp->v_type == VDIR) && (tvp->v_type != VDIR)) {
1.31 cgd 4398: error = ENOTDIR;
1.458 riastrad 4399: goto abort3;
4400: } else if ((fvp->v_type != VDIR) && (tvp->v_type == VDIR)) {
1.31 cgd 4401: error = EISDIR;
1.458 riastrad 4402: goto abort3;
1.31 cgd 4403: }
4404: }
1.90 kleink 4405:
1.458 riastrad 4406: /*
4407: * Acknowledge some random screw case, among the dozens that
4408: * might arise.
4409: */
4410: if (fvp == tdvp) {
1.31 cgd 4411: error = EINVAL;
1.458 riastrad 4412: goto abort3;
4413: }
1.90 kleink 4414:
1.82 kleink 4415: /*
1.458 riastrad 4416: * Acknowledge that POSIX has a wacky screw case.
4417: *
4418: * XXX Eventually the retain flag needs to be passed on to
4419: * VOP_RENAME.
1.82 kleink 4420: */
1.90 kleink 4421: if (fvp == tvp) {
1.458 riastrad 4422: if (retain) {
4423: error = 0;
4424: goto abort3;
4425: } else if ((fdvp == tdvp) &&
4426: (fnd.ni_cnd.cn_namelen == tnd.ni_cnd.cn_namelen) &&
4427: (0 == memcmp(fnd.ni_cnd.cn_nameptr, tnd.ni_cnd.cn_nameptr,
4428: fnd.ni_cnd.cn_namelen))) {
4429: error = 0;
4430: goto abort3;
4431: }
1.90 kleink 4432: }
1.458 riastrad 4433:
1.419 yamt 4434: /*
1.458 riastrad 4435: * Make sure veriexec can screw us up. (But a race can screw
4436: * up veriexec, of course -- remember, fvp and (soon) tvp are
4437: * bogus.)
1.419 yamt 4438: */
1.256 elad 4439: #if NVERIEXEC > 0
1.458 riastrad 4440: {
1.313 elad 4441: char *f1, *f2;
1.384 yamt 4442: size_t f1_len;
4443: size_t f2_len;
1.313 elad 4444:
1.458 riastrad 4445: f1_len = fnd.ni_cnd.cn_namelen + 1;
1.384 yamt 4446: f1 = kmem_alloc(f1_len, KM_SLEEP);
1.458 riastrad 4447: strlcpy(f1, fnd.ni_cnd.cn_nameptr, f1_len);
1.384 yamt 4448:
1.458 riastrad 4449: f2_len = tnd.ni_cnd.cn_namelen + 1;
1.384 yamt 4450: f2 = kmem_alloc(f2_len, KM_SLEEP);
1.458 riastrad 4451: strlcpy(f2, tnd.ni_cnd.cn_nameptr, f2_len);
1.282 elad 4452:
1.428 uebayasi 4453: error = veriexec_renamechk(curlwp, fvp, f1, tvp, f2);
1.282 elad 4454:
1.384 yamt 4455: kmem_free(f1, f1_len);
4456: kmem_free(f2, f2_len);
1.458 riastrad 4457:
4458: if (error)
4459: goto abort3;
1.282 elad 4460: }
1.256 elad 4461: #endif /* NVERIEXEC > 0 */
1.229 elad 4462:
1.458 riastrad 4463: /*
4464: * All ready. Incant the rename vop.
4465: */
4466: /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4467: /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4468: KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
4469: KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
4470: error = VOP_RENAME(fdvp, fvp, &fnd.ni_cnd, tdvp, tvp, &tnd.ni_cnd);
4471:
4472: /*
4473: * VOP_RENAME releases fdvp, fvp, tdvp, and tvp, and unlocks
4474: * tdvp and tvp. But we can't assert any of that.
4475: */
4476: /* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
4477: /* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
4478: /* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
4479: /* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
4480:
4481: /*
4482: * So all we have left to do is to drop the rename lock and
4483: * destroy the pathbufs.
4484: */
1.459 riastrad 4485: VFS_RENAMELOCK_EXIT(mp);
1.458 riastrad 4486: goto out2;
4487:
4488: abort3: if ((tvp != NULL) && (tvp != tdvp))
4489: VOP_UNLOCK(tvp);
4490: abort2: VOP_UNLOCK(tdvp);
1.459 riastrad 4491: VFS_RENAMELOCK_EXIT(mp);
1.458 riastrad 4492: abort1: VOP_ABORTOP(tdvp, &tnd.ni_cnd);
4493: vrele(tdvp);
4494: if (tvp != NULL)
4495: vrele(tvp);
4496: abort0: VOP_ABORTOP(fdvp, &fnd.ni_cnd);
4497: vrele(fdvp);
4498: vrele(fvp);
4499: out2: pathbuf_destroy(tpb);
4500: out1: pathbuf_destroy(fpb);
4501: out0: return error;
1.31 cgd 4502: }
4503:
4504: /*
4505: * Make a directory file.
4506: */
4507: /* ARGSUSED */
1.63 christos 4508: int
1.335 dsl 4509: sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
1.56 thorpej 4510: {
1.335 dsl 4511: /* {
1.74 cgd 4512: syscallarg(const char *) path;
1.35 cgd 4513: syscallarg(int) mode;
1.335 dsl 4514: } */
1.396 pooka 4515:
1.460 manu 4516: return do_sys_mkdirat(l, AT_FDCWD, SCARG(uap, path),
4517: SCARG(uap, mode), UIO_USERSPACE);
1.396 pooka 4518: }
4519:
4520: int
1.433 manu 4521: sys_mkdirat(struct lwp *l, const struct sys_mkdirat_args *uap,
4522: register_t *retval)
4523: {
4524: /* {
4525: syscallarg(int) fd;
4526: syscallarg(const char *) path;
4527: syscallarg(int) mode;
4528: } */
4529:
1.460 manu 4530: return do_sys_mkdirat(l, SCARG(uap, fd), SCARG(uap, path),
4531: SCARG(uap, mode), UIO_USERSPACE);
1.433 manu 4532: }
4533:
4534:
4535: int
1.399 haad 4536: do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg)
1.396 pooka 4537: {
1.460 manu 4538: return do_sys_mkdirat(NULL, AT_FDCWD, path, mode, UIO_USERSPACE);
4539: }
4540:
4541: static int
4542: do_sys_mkdirat(struct lwp *l, int fdat, const char *path, mode_t mode,
4543: enum uio_seg seg)
4544: {
1.396 pooka 4545: struct proc *p = curlwp->l_proc;
1.155 augustss 4546: struct vnode *vp;
1.31 cgd 4547: struct vattr vattr;
4548: int error;
1.409 dholland 4549: struct pathbuf *pb;
1.31 cgd 4550: struct nameidata nd;
4551:
1.460 manu 4552: KASSERT(l != NULL || fdat == AT_FDCWD);
4553:
1.409 dholland 4554: /* XXX bollocks, should pass in a pathbuf */
4555: error = pathbuf_maybe_copyin(path, seg, &pb);
4556: if (error) {
4557: return error;
4558: }
4559:
4560: NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, pb);
1.460 manu 4561:
4562: if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
1.409 dholland 4563: pathbuf_destroy(pb);
1.31 cgd 4564: return (error);
1.409 dholland 4565: }
1.31 cgd 4566: vp = nd.ni_vp;
4567: if (vp != NULL) {
4568: VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
4569: if (nd.ni_dvp == vp)
4570: vrele(nd.ni_dvp);
4571: else
4572: vput(nd.ni_dvp);
4573: vrele(vp);
1.409 dholland 4574: pathbuf_destroy(pb);
1.31 cgd 4575: return (EEXIST);
4576: }
1.402 pooka 4577: vattr_null(&vattr);
1.31 cgd 4578: vattr.va_type = VDIR;
1.328 ad 4579: /* We will read cwdi->cwdi_cmask unlocked. */
1.396 pooka 4580: vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
1.31 cgd 4581: error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
4582: if (!error)
1.473 hannken 4583: vrele(nd.ni_vp);
1.472 hannken 4584: vput(nd.ni_dvp);
1.409 dholland 4585: pathbuf_destroy(pb);
1.31 cgd 4586: return (error);
4587: }
4588:
4589: /*
4590: * Remove a directory file.
4591: */
4592: /* ARGSUSED */
1.63 christos 4593: int
1.335 dsl 4594: sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
1.56 thorpej 4595: {
1.460 manu 4596: return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path),
4597: AT_REMOVEDIR, UIO_USERSPACE);
1.31 cgd 4598: }
4599:
4600: /*
4601: * Read a block of directory entries in a file system independent format.
4602: */
1.63 christos 4603: int
1.335 dsl 4604: sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
1.56 thorpej 4605: {
1.335 dsl 4606: /* {
1.35 cgd 4607: syscallarg(int) fd;
4608: syscallarg(char *) buf;
1.101 fvdl 4609: syscallarg(size_t) count;
1.335 dsl 4610: } */
1.346 ad 4611: file_t *fp;
1.101 fvdl 4612: int error, done;
1.31 cgd 4613:
1.346 ad 4614: /* fd_getvnode() will use the descriptor for us */
4615: if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1.31 cgd 4616: return (error);
1.135 thorpej 4617: if ((fp->f_flag & FREAD) == 0) {
4618: error = EBADF;
4619: goto out;
4620: }
1.101 fvdl 4621: error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
1.234 christos 4622: SCARG(uap, count), &done, l, 0, 0);
1.325 ad 4623: ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
1.101 fvdl 4624: *retval = done;
1.135 thorpej 4625: out:
1.346 ad 4626: fd_putfile(SCARG(uap, fd));
1.31 cgd 4627: return (error);
4628: }
4629:
4630: /*
4631: * Set the mode mask for creation of filesystem nodes.
4632: */
1.56 thorpej 4633: int
1.335 dsl 4634: sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
1.56 thorpej 4635: {
1.335 dsl 4636: /* {
1.103 mycroft 4637: syscallarg(mode_t) newmask;
1.335 dsl 4638: } */
1.179 thorpej 4639: struct proc *p = l->l_proc;
1.134 thorpej 4640: struct cwdinfo *cwdi;
1.31 cgd 4641:
1.328 ad 4642: /*
4643: * cwdi->cwdi_cmask will be read unlocked elsewhere. What's
4644: * important is that we serialize changes to the mask. The
4645: * rw_exit() will issue a write memory barrier on our behalf,
4646: * and force the changes out to other CPUs (as it must use an
4647: * atomic operation, draining the local CPU's store buffers).
4648: */
1.134 thorpej 4649: cwdi = p->p_cwdi;
1.328 ad 4650: rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1.134 thorpej 4651: *retval = cwdi->cwdi_cmask;
4652: cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
1.328 ad 4653: rw_exit(&cwdi->cwdi_lock);
4654:
1.31 cgd 4655: return (0);
4656: }
4657:
1.340 elad 4658: int
4659: dorevoke(struct vnode *vp, kauth_cred_t cred)
4660: {
4661: struct vattr vattr;
1.450 elad 4662: int error, fs_decision;
1.340 elad 4663:
1.440 hannken 4664: vn_lock(vp, LK_SHARED | LK_RETRY);
4665: error = VOP_GETATTR(vp, &vattr, cred);
4666: VOP_UNLOCK(vp);
4667: if (error != 0)
1.342 ad 4668: return error;
1.450 elad 4669: fs_decision = (kauth_cred_geteuid(cred) == vattr.va_uid) ? 0 : EPERM;
4670: error = kauth_authorize_vnode(cred, KAUTH_VNODE_REVOKE, vp, NULL,
4671: fs_decision);
4672: if (!error)
1.340 elad 4673: VOP_REVOKE(vp, REVOKEALL);
4674: return (error);
4675: }
4676:
1.31 cgd 4677: /*
4678: * Void all references to file by ripping underlying filesystem
4679: * away from vnode.
4680: */
4681: /* ARGSUSED */
1.63 christos 4682: int
1.335 dsl 4683: sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
1.56 thorpej 4684: {
1.335 dsl 4685: /* {
1.74 cgd 4686: syscallarg(const char *) path;
1.335 dsl 4687: } */
1.155 augustss 4688: struct vnode *vp;
1.31 cgd 4689: int error;
4690:
1.395 dholland 4691: error = namei_simple_user(SCARG(uap, path),
4692: NSM_FOLLOW_TRYEMULROOT, &vp);
4693: if (error != 0)
1.31 cgd 4694: return (error);
1.340 elad 4695: error = dorevoke(vp, l->l_cred);
1.31 cgd 4696: vrele(vp);
4697: return (error);
4698: }
1.488 dholland 4699:
4700: /*
4701: * Allocate backing store for a file, filling a hole without having to
4702: * explicitly write anything out.
4703: */
4704: /* ARGSUSED */
4705: int
4706: sys_posix_fallocate(struct lwp *l, const struct sys_posix_fallocate_args *uap,
4707: register_t *retval)
4708: {
4709: /* {
4710: syscallarg(int) fd;
4711: syscallarg(off_t) pos;
4712: syscallarg(off_t) len;
4713: } */
4714: int fd;
4715: off_t pos, len;
4716: struct file *fp;
4717: struct vnode *vp;
1.490 maxv 4718: int error;
1.488 dholland 4719:
4720: fd = SCARG(uap, fd);
4721: pos = SCARG(uap, pos);
4722: len = SCARG(uap, len);
4723:
4724: if (pos < 0 || len < 0 || len > OFF_T_MAX - pos) {
1.493 martin 4725: *retval = EINVAL;
4726: return 0;
1.488 dholland 4727: }
4728:
1.490 maxv 4729: error = fd_getvnode(fd, &fp);
4730: if (error) {
1.493 martin 4731: *retval = error;
4732: return 0;
1.488 dholland 4733: }
4734: if ((fp->f_flag & FWRITE) == 0) {
1.490 maxv 4735: error = EBADF;
1.488 dholland 4736: goto fail;
4737: }
1.491 matt 4738: vp = fp->f_vnode;
1.488 dholland 4739:
4740: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4741: if (vp->v_type == VDIR) {
1.490 maxv 4742: error = EISDIR;
1.488 dholland 4743: } else {
1.490 maxv 4744: error = VOP_FALLOCATE(vp, pos, len);
1.488 dholland 4745: }
4746: VOP_UNLOCK(vp);
4747:
4748: fail:
4749: fd_putfile(fd);
1.493 martin 4750: *retval = error;
4751: return 0;
1.488 dholland 4752: }
4753:
4754: /*
1.489 dholland 4755: * Deallocate backing store for a file, creating a hole. Also used for
1.488 dholland 4756: * invoking TRIM on disks.
4757: */
4758: /* ARGSUSED */
4759: int
4760: sys_fdiscard(struct lwp *l, const struct sys_fdiscard_args *uap,
4761: register_t *retval)
4762: {
4763: /* {
4764: syscallarg(int) fd;
4765: syscallarg(off_t) pos;
4766: syscallarg(off_t) len;
4767: } */
4768: int fd;
4769: off_t pos, len;
4770: struct file *fp;
4771: struct vnode *vp;
1.490 maxv 4772: int error;
1.488 dholland 4773:
4774: fd = SCARG(uap, fd);
4775: pos = SCARG(uap, pos);
4776: len = SCARG(uap, len);
4777:
4778: if (pos < 0 || len < 0 || len > OFF_T_MAX - pos) {
4779: return EINVAL;
4780: }
4781:
1.490 maxv 4782: error = fd_getvnode(fd, &fp);
4783: if (error) {
4784: return error;
1.488 dholland 4785: }
4786: if ((fp->f_flag & FWRITE) == 0) {
1.490 maxv 4787: error = EBADF;
1.488 dholland 4788: goto fail;
4789: }
1.491 matt 4790: vp = fp->f_vnode;
1.488 dholland 4791:
4792: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4793: if (vp->v_type == VDIR) {
1.490 maxv 4794: error = EISDIR;
1.488 dholland 4795: } else {
1.490 maxv 4796: error = VOP_FDISCARD(vp, pos, len);
1.488 dholland 4797: }
4798: VOP_UNLOCK(vp);
4799:
4800: fail:
4801: fd_putfile(fd);
1.490 maxv 4802: return error;
1.488 dholland 4803: }
CVSweb <webmaster@jp.NetBSD.org>