2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $DragonFly: src/sys/kern/kern_fp.c,v 1.3 2003/10/19 19:24:18 dillon Exp $
30 * Direct file pointer API functions for in-kernel operations on files. These
31 * functions provide a open/read/write/close like interface within the kernel
32 * for operating on files that are not necessarily associated with processes
33 * and which do not (typically) have descriptors.
35 * FUTURE: file handle conversion routines to support checkpointing,
36 * and additional file operations (ioctl, fcntl).
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/sysproto.h>
45 #include <sys/filedesc.h>
46 #include <sys/sysctl.h>
47 #include <sys/vnode.h>
49 #include <sys/namei.h>
52 #include <sys/filio.h>
53 #include <sys/fcntl.h>
54 #include <sys/unistd.h>
55 #include <sys/resourcevar.h>
56 #include <sys/event.h>
60 #include <vm/vm_param.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_pager.h>
67 #include <vm/vm_pageout.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_kern.h>
72 #include <sys/file2.h>
73 #include <machine/limits.h>
75 typedef struct file *file_t;
80 * Open a file as specified. Use O_* flags for flags.
82 * NOTE! O_ROOTCRED not quite working yet, vn_open() asserts that the
83 * cred must match the process's cred.
86 fp_open(const char *path, int flags, int mode, file_t *fpp)
93 if ((error = falloc(NULL, fpp, NULL)) != 0)
97 if ((flags & O_ROOTCRED) == 0 && td->td_proc)
98 fsetcred(fp, td->td_proc->p_ucred);
100 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_SYSSPACE, path, td);
101 flags = FFLAGS(flags);
102 printf("%08x\n", flags);
103 if ((error = vn_open(&nd, flags, mode)) == 0) {
104 NDFREE(&nd, NDF_ONLY_PNBUF);
105 fp->f_data = (caddr_t)nd.ni_vp;
108 fp->f_type = DTYPE_VNODE;
109 VOP_UNLOCK(nd.ni_vp, 0, td);
118 * fp_*read() is meant to operate like the normal descriptor based syscalls
119 * would. Note that if 'buf' points to user memory a UIO_USERSPACE
120 * transfer will be used.
123 fp_pread(file_t fp, void *buf, size_t nbytes, off_t offset, ssize_t *res)
132 if (nbytes > INT_MAX)
134 bzero(&auio, sizeof(auio));
135 aiov.iov_base = (caddr_t)buf;
136 aiov.iov_len = nbytes;
137 auio.uio_iov = &aiov;
139 auio.uio_offset = offset;
140 auio.uio_resid = nbytes;
141 auio.uio_rw = UIO_READ;
142 if ((vm_offset_t)buf < VM_MAXUSER_ADDRESS)
143 auio.uio_segflg = UIO_USERSPACE;
145 auio.uio_segflg = UIO_SYSSPACE;
146 auio.uio_td = curthread;
149 error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, auio.uio_td);
151 if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
152 error == EWOULDBLOCK)
157 count -= auio.uio_resid;
164 fp_read(file_t fp, void *buf, size_t nbytes, ssize_t *res)
173 if (nbytes > INT_MAX)
175 bzero(&auio, sizeof(auio));
176 aiov.iov_base = (caddr_t)buf;
177 aiov.iov_len = nbytes;
178 auio.uio_iov = &aiov;
181 auio.uio_resid = nbytes;
182 auio.uio_rw = UIO_READ;
183 if ((vm_offset_t)buf < VM_MAXUSER_ADDRESS)
184 auio.uio_segflg = UIO_USERSPACE;
186 auio.uio_segflg = UIO_SYSSPACE;
187 auio.uio_td = curthread;
190 error = fo_read(fp, &auio, fp->f_cred, 0, auio.uio_td);
192 if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
193 error == EWOULDBLOCK)
198 count -= auio.uio_resid;
205 fp_pwrite(file_t fp, void *buf, size_t nbytes, off_t offset, ssize_t *res)
214 if (nbytes > INT_MAX)
216 bzero(&auio, sizeof(auio));
217 aiov.iov_base = (caddr_t)buf;
218 aiov.iov_len = nbytes;
219 auio.uio_iov = &aiov;
221 auio.uio_offset = offset;
222 auio.uio_resid = nbytes;
223 auio.uio_rw = UIO_WRITE;
224 if ((vm_offset_t)buf < VM_MAXUSER_ADDRESS)
225 auio.uio_segflg = UIO_USERSPACE;
227 auio.uio_segflg = UIO_SYSSPACE;
228 auio.uio_td = curthread;
231 error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, auio.uio_td);
233 if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
234 error == EWOULDBLOCK)
239 count -= auio.uio_resid;
247 fp_write(file_t fp, void *buf, size_t nbytes, ssize_t *res)
256 if (nbytes > INT_MAX)
258 bzero(&auio, sizeof(auio));
259 aiov.iov_base = (caddr_t)buf;
260 aiov.iov_len = nbytes;
261 auio.uio_iov = &aiov;
264 auio.uio_resid = nbytes;
265 auio.uio_rw = UIO_WRITE;
266 if ((vm_offset_t)buf < VM_MAXUSER_ADDRESS)
267 auio.uio_segflg = UIO_USERSPACE;
269 auio.uio_segflg = UIO_SYSSPACE;
270 auio.uio_td = curthread;
273 error = fo_write(fp, &auio, fp->f_cred, 0, auio.uio_td);
275 if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
276 error == EWOULDBLOCK)
281 count -= auio.uio_resid;
288 fp_stat(file_t fp, struct stat *ub)
292 error = fo_stat(fp, ub, curthread);
297 * non-anonymous, non-stack descriptor mappings only!
299 * This routine mostly snarfed from vm/vm_mmap.c
302 fp_mmap(void *addr_arg, size_t size, int prot, int flags, struct file *fp,
303 off_t pos, void **resp)
305 struct thread *td = curthread;
306 struct proc *p = td->td_proc;
313 struct vmspace *vms = p->p_vmspace;
315 int disablexworkaround;
319 if ((ssize_t)size < 0 || (flags & MAP_ANON))
322 pageoff = (pos & PAGE_MASK);
325 /* Adjust size for rounding (on both ends). */
326 size += pageoff; /* low end... */
327 size = (vm_size_t)round_page(size); /* hi end */
328 addr = (vm_offset_t)addr_arg;
331 * Check for illegal addresses. Watch out for address wrap... Note
332 * that VM_*_ADDRESS are not constants due to casts (argh).
334 if (flags & MAP_FIXED) {
336 * The specified address must have the same remainder
337 * as the file offset taken modulo PAGE_SIZE, so it
338 * should be aligned after adjustment by pageoff.
341 if (addr & PAGE_MASK)
343 /* Address range must be all in user VM space. */
344 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
347 if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
350 if (addr + size < addr)
352 } else if (addr == 0 ||
353 (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
354 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))
357 * XXX for non-fixed mappings where no hint is provided or
358 * the hint would fall in the potential heap space,
359 * place it after the end of the largest possible heap.
361 * There should really be a pmap call to determine a reasonable
364 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
368 * Mapping file, get fp for validation. Obtain vnode and make
369 * sure it is of appropriate type.
371 if (fp->f_type != DTYPE_VNODE)
375 * POSIX shared-memory objects are defined to have
376 * kernel persistence, and are not defined to support
377 * read(2)/write(2) -- or even open(2). Thus, we can
378 * use MAP_ASYNC to trade on-disk coherence for speed.
379 * The shm_open(3) library routine turns on the FPOSIXSHM
380 * flag to request this behavior.
382 if (fp->f_flag & FPOSIXSHM)
384 vp = (struct vnode *) fp->f_data;
385 if (vp->v_type != VREG && vp->v_type != VCHR)
389 * Get the proper underlying object
391 if (vp->v_type == VREG) {
392 if (VOP_GETVOBJECT(vp, &obj) != 0)
394 vp = (struct vnode*)obj->handle;
398 * XXX hack to handle use of /dev/zero to map anon memory (ala
401 if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
403 maxprot = VM_PROT_ALL;
408 * cdevs does not provide private mappings of any kind.
411 * However, for XIG X server to continue to work,
412 * we should allow the superuser to do it anyway.
413 * We only allow it at securelevel < 1.
414 * (Because the XIG X server writes directly to video
415 * memory via /dev/mem, it should never work at any
417 * XXX this will have to go
419 if (securelevel >= 1)
420 disablexworkaround = 1;
422 disablexworkaround = suser(td);
423 if (vp->v_type == VCHR && disablexworkaround &&
424 (flags & (MAP_PRIVATE|MAP_COPY))) {
429 * Ensure that file and memory protections are
430 * compatible. Note that we only worry about
431 * writability if mapping is shared; in this case,
432 * current and max prot are dictated by the open file.
433 * XXX use the vnode instead? Problem is: what
434 * credentials do we use for determination? What if
435 * proc does a setuid?
437 maxprot = VM_PROT_EXECUTE; /* ??? */
438 if (fp->f_flag & FREAD) {
439 maxprot |= VM_PROT_READ;
440 } else if (prot & PROT_READ) {
445 * If we are sharing potential changes (either via
446 * MAP_SHARED or via the implicit sharing of character
447 * device mappings), and we are trying to get write
448 * permission although we opened it without asking
449 * for it, bail out. Check for superuser, only if
450 * we're at securelevel < 1, to allow the XIG X server
451 * to continue to work.
454 if ((flags & MAP_SHARED) != 0 ||
455 (vp->v_type == VCHR && disablexworkaround)
457 if ((fp->f_flag & FWRITE) != 0) {
459 if ((error = VOP_GETATTR(vp, &va, td))) {
462 if ((va.va_flags & (IMMUTABLE|APPEND)) == 0) {
463 maxprot |= VM_PROT_WRITE;
464 } else if (prot & PROT_WRITE) {
468 } else if ((prot & PROT_WRITE) != 0) {
473 maxprot |= VM_PROT_WRITE;
477 error = vm_mmap(&vms->vm_map, &addr, size, prot,
478 maxprot, flags, handle, pos);
479 if (error == 0 && addr_arg)
480 *resp = (void *)addr;
488 return(fdrop(fp, curthread));