feeca141e98360d1996ead165dc0a64cc38ce10f
[dragonfly.git] / sys / kern / kern_fp.c
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $DragonFly: src/sys/kern/kern_fp.c,v 1.3 2003/10/19 19:24:18 dillon Exp $
27  */
28
29 /*
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.
34  *
35  * FUTURE: file handle conversion routines to support checkpointing, 
36  * and additional file operations (ioctl, fcntl).
37  */
38
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>
44 #include <sys/conf.h>
45 #include <sys/filedesc.h>
46 #include <sys/sysctl.h>
47 #include <sys/vnode.h>
48 #include <sys/proc.h>
49 #include <sys/namei.h>
50 #include <sys/file.h>
51 #include <sys/stat.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>
57 #include <sys/mman.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <sys/lock.h>
62 #include <vm/pmap.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>
71
72 #include <sys/file2.h>
73 #include <machine/limits.h>
74
75 typedef struct file *file_t;
76
77 /*
78  * fp_open:
79  *
80  *      Open a file as specified.  Use O_* flags for flags.
81  *
82  *      NOTE! O_ROOTCRED not quite working yet, vn_open() asserts that the
83  *      cred must match the process's cred.
84  */
85 int
86 fp_open(const char *path, int flags, int mode, file_t *fpp)
87 {
88     struct nameidata nd;
89     struct thread *td;
90     struct file *fp;
91     int error;
92
93     if ((error = falloc(NULL, fpp, NULL)) != 0)
94         return (error);
95     fp = *fpp;
96     td = curthread;
97     if ((flags & O_ROOTCRED) == 0 && td->td_proc)
98         fsetcred(fp, td->td_proc->p_ucred);
99
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;
106         fp->f_flag = flags;
107         fp->f_ops = &vnops;
108         fp->f_type = DTYPE_VNODE;
109         VOP_UNLOCK(nd.ni_vp, 0, td);
110     } else {
111         fdrop(fp, td);
112         *fpp = NULL;
113     }
114     return(error);
115 }
116
117 /*
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.
121  */
122 int
123 fp_pread(file_t fp, void *buf, size_t nbytes, off_t offset, ssize_t *res)
124 {
125     struct uio auio;
126     struct iovec aiov;
127     size_t count;
128     int error;
129
130     if (res)
131         *res = 0;
132     if (nbytes > INT_MAX)
133         return (EINVAL);
134     bzero(&auio, sizeof(auio));
135     aiov.iov_base = (caddr_t)buf;
136     aiov.iov_len = nbytes;
137     auio.uio_iov = &aiov;
138     auio.uio_iovcnt = 1;
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;
144     else
145         auio.uio_segflg = UIO_SYSSPACE;
146     auio.uio_td = curthread;
147
148     count = nbytes;
149     error = fo_read(fp, &auio, fp->f_cred, FOF_OFFSET, auio.uio_td);
150     if (error) {
151         if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
152             error == EWOULDBLOCK)
153         ) {
154             error = 0;
155         }
156     }
157     count -= auio.uio_resid;
158     if (res)
159         *res = count;
160     return(error);
161 }
162
163 int
164 fp_read(file_t fp, void *buf, size_t nbytes, ssize_t *res)
165 {
166     struct uio auio;
167     struct iovec aiov;
168     size_t count;
169     int error;
170
171     if (res)
172         *res = 0;
173     if (nbytes > INT_MAX)
174         return (EINVAL);
175     bzero(&auio, sizeof(auio));
176     aiov.iov_base = (caddr_t)buf;
177     aiov.iov_len = nbytes;
178     auio.uio_iov = &aiov;
179     auio.uio_iovcnt = 1;
180     auio.uio_offset = 0;
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;
185     else
186         auio.uio_segflg = UIO_SYSSPACE;
187     auio.uio_td = curthread;
188
189     count = nbytes;
190     error = fo_read(fp, &auio, fp->f_cred, 0, auio.uio_td);
191     if (error) {
192         if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
193             error == EWOULDBLOCK)
194         ) {
195             error = 0;
196         }
197     }
198     count -= auio.uio_resid;
199     if (res)
200         *res = count;
201     return(error);
202 }
203
204 int
205 fp_pwrite(file_t fp, void *buf, size_t nbytes, off_t offset, ssize_t *res)
206 {
207     struct uio auio;
208     struct iovec aiov;
209     size_t count;
210     int error;
211
212     if (res)
213         *res = 0;
214     if (nbytes > INT_MAX)
215         return (EINVAL);
216     bzero(&auio, sizeof(auio));
217     aiov.iov_base = (caddr_t)buf;
218     aiov.iov_len = nbytes;
219     auio.uio_iov = &aiov;
220     auio.uio_iovcnt = 1;
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;
226     else
227         auio.uio_segflg = UIO_SYSSPACE;
228     auio.uio_td = curthread;
229
230     count = nbytes;
231     error = fo_write(fp, &auio, fp->f_cred, FOF_OFFSET, auio.uio_td);
232     if (error) {
233         if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
234             error == EWOULDBLOCK)
235         ) {
236             error = 0;
237         }
238     }
239     count -= auio.uio_resid;
240     if (res)
241         *res = count;
242     return(error);
243 }
244
245
246 int
247 fp_write(file_t fp, void *buf, size_t nbytes, ssize_t *res)
248 {
249     struct uio auio;
250     struct iovec aiov;
251     size_t count;
252     int error;
253
254     if (res)
255         *res = 0;
256     if (nbytes > INT_MAX)
257         return (EINVAL);
258     bzero(&auio, sizeof(auio));
259     aiov.iov_base = (caddr_t)buf;
260     aiov.iov_len = nbytes;
261     auio.uio_iov = &aiov;
262     auio.uio_iovcnt = 1;
263     auio.uio_offset = 0;
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;
268     else
269         auio.uio_segflg = UIO_SYSSPACE;
270     auio.uio_td = curthread;
271
272     count = nbytes;
273     error = fo_write(fp, &auio, fp->f_cred, 0, auio.uio_td);
274     if (error) {
275         if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
276             error == EWOULDBLOCK)
277         ) {
278             error = 0;
279         }
280     }
281     count -= auio.uio_resid;
282     if (res)
283         *res = count;
284     return(error);
285 }
286
287 int
288 fp_stat(file_t fp, struct stat *ub)
289 {
290     int error;
291
292     error = fo_stat(fp, ub, curthread);
293     return(error);
294 }
295
296 /*
297  * non-anonymous, non-stack descriptor mappings only!
298  *
299  * This routine mostly snarfed from vm/vm_mmap.c
300  */
301 int
302 fp_mmap(void *addr_arg, size_t size, int prot, int flags, struct file *fp,
303     off_t pos, void **resp)
304 {
305     struct thread *td = curthread;
306     struct proc *p = td->td_proc;
307     vm_size_t pageoff;
308     vm_prot_t maxprot;
309     vm_offset_t addr;
310     void *handle;
311     int error;
312     vm_object_t obj;
313     struct vmspace *vms = p->p_vmspace;
314     struct vnode *vp;
315     int disablexworkaround;
316
317     prot &= VM_PROT_ALL;
318
319     if ((ssize_t)size < 0 || (flags & MAP_ANON))
320         return(EINVAL);
321
322     pageoff = (pos & PAGE_MASK);
323     pos -= pageoff;
324
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;
329
330     /*
331      * Check for illegal addresses.  Watch out for address wrap... Note
332      * that VM_*_ADDRESS are not constants due to casts (argh).
333      */
334     if (flags & MAP_FIXED) {
335         /*
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.
339          */
340         addr -= pageoff;
341         if (addr & PAGE_MASK)
342             return (EINVAL);
343         /* Address range must be all in user VM space. */
344         if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
345             return (EINVAL);
346 #ifndef i386
347         if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
348             return (EINVAL);
349 #endif
350         if (addr + size < addr)
351             return (EINVAL);
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))
355     ) {
356         /*
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.
360          *
361          * There should really be a pmap call to determine a reasonable
362          * location.
363          */
364         addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
365     }
366
367     /*
368      * Mapping file, get fp for validation. Obtain vnode and make
369      * sure it is of appropriate type.
370      */
371     if (fp->f_type != DTYPE_VNODE)
372         return (EINVAL);
373
374     /*
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.
381      */
382     if (fp->f_flag & FPOSIXSHM)
383         flags |= MAP_NOSYNC;
384     vp = (struct vnode *) fp->f_data;
385     if (vp->v_type != VREG && vp->v_type != VCHR)
386         return (EINVAL);
387
388     /*
389      * Get the proper underlying object
390      */
391     if (vp->v_type == VREG) {
392         if (VOP_GETVOBJECT(vp, &obj) != 0)
393             return (EINVAL);
394         vp = (struct vnode*)obj->handle;
395     }
396
397     /*
398      * XXX hack to handle use of /dev/zero to map anon memory (ala
399      * SunOS).
400      */
401     if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
402         handle = NULL;
403         maxprot = VM_PROT_ALL;
404         flags |= MAP_ANON;
405         pos = 0;
406     } else {
407         /*
408          * cdevs does not provide private mappings of any kind.
409          */
410         /*
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
416          * other securelevel.
417          * XXX this will have to go
418          */
419         if (securelevel >= 1)
420             disablexworkaround = 1;
421         else
422             disablexworkaround = suser(td);
423         if (vp->v_type == VCHR && disablexworkaround &&
424             (flags & (MAP_PRIVATE|MAP_COPY))) {
425                 error = EINVAL;
426                 goto done;
427         }
428         /*
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?
436          */
437         maxprot = VM_PROT_EXECUTE;      /* ??? */
438         if (fp->f_flag & FREAD) {
439             maxprot |= VM_PROT_READ;
440         } else if (prot & PROT_READ) {
441             error = EACCES;
442             goto done;
443         }
444         /*
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.
452          */
453
454         if ((flags & MAP_SHARED) != 0 ||
455             (vp->v_type == VCHR && disablexworkaround)
456         ) {
457             if ((fp->f_flag & FWRITE) != 0) {
458                 struct vattr va;
459                 if ((error = VOP_GETATTR(vp, &va, td))) {
460                     goto done;
461                 }
462                 if ((va.va_flags & (IMMUTABLE|APPEND)) == 0) {
463                     maxprot |= VM_PROT_WRITE;
464                 } else if (prot & PROT_WRITE) {
465                     error = EPERM;
466                     goto done;
467                 }
468             } else if ((prot & PROT_WRITE) != 0) {
469                 error = EACCES;
470                 goto done;
471             }
472         } else {
473             maxprot |= VM_PROT_WRITE;
474         }
475         handle = (void *)vp;
476     }
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;
481 done:
482     return (error);
483 }
484
485 int
486 fp_close(file_t fp)
487 {
488     return(fdrop(fp, curthread));
489 }
490