openssl: Adjust manual pages for 1.0.1h.
[dragonfly.git] / sys / kern / kern_fp.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * Direct file pointer API functions for in-kernel operations on files.  These
37  * functions provide a open/read/write/close like interface within the kernel
38  * for operating on files that are not necessarily associated with processes
39  * and which do not (typically) have descriptors.
40  *
41  * FUTURE: file handle conversion routines to support checkpointing, 
42  * and additional file operations (ioctl, fcntl).
43  */
44
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/sysproto.h>
50 #include <sys/conf.h>
51 #include <sys/filedesc.h>
52 #include <sys/sysctl.h>
53 #include <sys/vnode.h>
54 #include <sys/proc.h>
55 #include <sys/priv.h>
56 #include <sys/nlookup.h>
57 #include <sys/file.h>
58 #include <sys/stat.h>
59 #include <sys/filio.h>
60 #include <sys/fcntl.h>
61 #include <sys/unistd.h>
62 #include <sys/resourcevar.h>
63 #include <sys/event.h>
64 #include <sys/mman.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_param.h>
68 #include <sys/lock.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_kern.h>
77
78 #include <sys/file2.h>
79 #include <machine/limits.h>
80
81 typedef struct file *file_t;
82
83 /*
84  * fp_open:
85  *
86  *      Open a file as specified.  Use O_* flags for flags.
87  *
88  *      vn_open() asserts that the cred must match the process's cred.
89  *
90  *      NOTE! when fp_open() is called from a pure thread, root creds are
91  *      used.
92  */
93 int
94 fp_open(const char *path, int flags, int mode, file_t *fpp)
95 {
96     struct nlookupdata nd;
97     struct thread *td;
98     struct file *fp;
99     int error;
100
101     if ((error = falloc(NULL, fpp, NULL)) != 0)
102         return (error);
103     fp = *fpp;
104     td = curthread;
105     if (td->td_proc)
106         fsetcred(fp, td->td_proc->p_ucred);
107     error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_LOCKVP);
108     flags = FFLAGS(flags);
109     if (error == 0)
110         error = vn_open(&nd, fp, flags, mode);
111     nlookup_done(&nd);
112     if (error) {
113         fdrop(fp);
114         *fpp = NULL;
115     }
116     return(error);
117 }
118
119
120 /*
121  * fp_vpopen(): convert a vnode to a file pointer, call VOP_OPEN() on the
122  * the vnode.  The vnode must be refd and locked.
123  *
124  * On success the vnode's ref is inherited by the file pointer and the caller
125  * should not vrele() it, and the vnode is unlocked.
126  *
127  * On failure the vnode remains locked and refd and the caller is responsible
128  * for vput()ing it.
129  */
130 int
131 fp_vpopen(struct vnode *vp, int flags, file_t *fpp)
132 {
133     struct thread *td;
134     struct file *fp;
135     int vmode;
136     int error;
137
138     td = curthread;
139
140     /*
141      * Vnode checks (from vn_open())
142      */
143     if (vp->v_type == VLNK) {
144         error = EMLINK;
145         goto bad2;
146     }
147     if (vp->v_type == VSOCK) {
148         error = EOPNOTSUPP;
149         goto bad2;
150     }
151     flags = FFLAGS(flags);
152     vmode = 0;
153     if (flags & (FWRITE | O_TRUNC)) {
154         if (vp->v_type == VDIR) {
155             error = EISDIR;
156             goto bad2;
157         }
158         error = vn_writechk(vp, NULL);
159         if (error)
160             goto bad2;
161         vmode |= VWRITE;
162     }
163     if (flags & FREAD)
164         vmode |= VREAD;
165     if (vmode) {
166         error = VOP_ACCESS(vp, vmode, td->td_proc->p_ucred);
167         if (error)
168             goto bad2;
169     }
170
171     /*
172      * File pointer setup
173      */
174     if ((error = falloc(NULL, fpp, NULL)) != 0)
175         goto bad2;
176     fp = *fpp;
177     if (td->td_proc)
178         fsetcred(fp, td->td_proc->p_ucred);
179
180     error = VOP_OPEN(vp, flags, td->td_proc->p_ucred, fp);
181     if (error)
182         goto bad1;
183
184     vput(vp);
185     return (0);
186 bad1:
187     fp->f_ops = &badfileops;    /* open failed, don't close */
188     fp->f_data = NULL;
189     fdrop(fp);
190     /* leave the vnode intact, but fall through and unlock it anyway */
191 bad2:
192     *fpp = NULL;
193     return (error);
194 }
195
196 /*
197  * fp_*read() is meant to operate like the normal descriptor based syscalls
198  * would.  Note that if 'buf' points to user memory a UIO_USERSPACE
199  * transfer will be used.
200  */
201 int
202 fp_pread(file_t fp, void *buf, size_t nbytes, off_t offset, ssize_t *res,
203          enum uio_seg seg)
204 {
205     struct uio auio;
206     struct iovec aiov;
207     size_t count;
208     int error;
209
210     if (res)
211         *res = 0;
212     if (nbytes > LONG_MAX)
213         return (EINVAL);
214     bzero(&auio, sizeof(auio));
215     aiov.iov_base = (caddr_t)buf;
216     aiov.iov_len = nbytes;
217     auio.uio_iov = &aiov;
218     auio.uio_iovcnt = 1;
219     auio.uio_offset = offset;
220     auio.uio_resid = nbytes;
221     auio.uio_rw = UIO_READ;
222     auio.uio_segflg = seg;
223     auio.uio_td = curthread;
224
225     count = nbytes;
226     error = fo_read(fp, &auio, fp->f_cred, O_FOFFSET);
227     if (error) {
228         if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
229             error == EWOULDBLOCK)
230         ) {
231             error = 0;
232         }
233     }
234     count -= auio.uio_resid;
235     if (res)
236         *res = count;
237     return(error);
238 }
239
240 int
241 fp_read(file_t fp, void *buf, size_t nbytes, ssize_t *res, int all,
242         enum uio_seg seg)
243 {
244     struct uio auio;
245     struct iovec aiov;
246     int error;
247     int lastresid;
248
249     if (res)
250         *res = 0;
251     if (nbytes > LONG_MAX)
252         return (EINVAL);
253     bzero(&auio, sizeof(auio));
254     aiov.iov_base = (caddr_t)buf;
255     aiov.iov_len = nbytes;
256     auio.uio_iov = &aiov;
257     auio.uio_iovcnt = 1;
258     auio.uio_offset = 0;
259     auio.uio_resid = nbytes;
260     auio.uio_rw = UIO_READ;
261     auio.uio_segflg = seg;
262     auio.uio_td = curthread;
263
264     /*
265      * If all is false call fo_read() once.
266      * If all is true we attempt to read the entire request.  We have to
267      * break out of the loop if an unrecoverable error or EOF occurs.
268      */
269     do {
270         lastresid = auio.uio_resid;
271         error = fo_read(fp, &auio, fp->f_cred, 0);
272     } while (all && auio.uio_resid &&
273              ((error == 0 && auio.uio_resid != lastresid) || 
274              error == ERESTART || error == EINTR));
275     if (all && error == 0 && auio.uio_resid)
276         error = ESPIPE;
277
278     /*
279      * If an error occured but some data was read, silently forget the
280      * error.  However, if this is a non-blocking descriptor and 'all'
281      * was specified, return an error even if some data was read (this
282      * is considered a bug in the caller for using an illegal combination
283      * of 'all' and a non-blocking descriptor).
284      */
285     if (error) {
286         if (auio.uio_resid != nbytes) {
287             if (error == ERESTART || error == EINTR)
288                 error = 0;
289             if (error == EWOULDBLOCK && all == 0)
290                 error = 0;
291         }
292     }
293     if (res)
294         *res = nbytes - auio.uio_resid;
295     return(error);
296 }
297
298 int
299 fp_pwrite(file_t fp, void *buf, size_t nbytes, off_t offset, ssize_t *res,
300           enum uio_seg seg)
301 {
302     struct uio auio;
303     struct iovec aiov;
304     size_t count;
305     int error;
306
307     if (res)
308         *res = 0;
309     if (nbytes > LONG_MAX)
310         return (EINVAL);
311     bzero(&auio, sizeof(auio));
312     aiov.iov_base = (caddr_t)buf;
313     aiov.iov_len = nbytes;
314     auio.uio_iov = &aiov;
315     auio.uio_iovcnt = 1;
316     auio.uio_offset = offset;
317     auio.uio_resid = nbytes;
318     auio.uio_rw = UIO_WRITE;
319     auio.uio_segflg = seg;
320     auio.uio_td = curthread;
321
322     count = nbytes;
323     error = fo_write(fp, &auio, fp->f_cred, O_FOFFSET);
324     if (error) {
325         if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
326             error == EWOULDBLOCK)
327         ) {
328             error = 0;
329         }
330     }
331     count -= auio.uio_resid;
332     if (res)
333         *res = count;
334     return(error);
335 }
336
337
338 int
339 fp_write(file_t fp, void *buf, size_t nbytes, ssize_t *res, enum uio_seg seg)
340 {
341     struct uio auio;
342     struct iovec aiov;
343     size_t count;
344     int error;
345
346     if (res)
347         *res = 0;
348     if (nbytes > LONG_MAX)
349         return (EINVAL);
350     bzero(&auio, sizeof(auio));
351     aiov.iov_base = (caddr_t)buf;
352     aiov.iov_len = nbytes;
353     auio.uio_iov = &aiov;
354     auio.uio_iovcnt = 1;
355     auio.uio_offset = 0;
356     auio.uio_resid = nbytes;
357     auio.uio_rw = UIO_WRITE;
358     auio.uio_segflg = seg;
359     auio.uio_td = curthread;
360
361     count = nbytes;
362     error = fo_write(fp, &auio, fp->f_cred, 0);
363     if (error) {
364         if (auio.uio_resid != nbytes && (error == ERESTART || error == EINTR ||
365             error == EWOULDBLOCK)
366         ) {
367             error = 0;
368         }
369     }
370     count -= auio.uio_resid;
371     if (res)
372         *res = count;
373     return(error);
374 }
375
376 int
377 fp_stat(file_t fp, struct stat *ub)
378 {
379     int error;
380
381     error = fo_stat(fp, ub, fp->f_cred);
382     return(error);
383 }
384
385 /*
386  * non-anonymous, non-stack descriptor mappings only!
387  *
388  * This routine mostly snarfed from vm/vm_mmap.c
389  */
390 int
391 fp_mmap(void *addr_arg, size_t size, int prot, int flags, struct file *fp,
392     off_t pos, void **resp)
393 {
394     struct thread *td = curthread;
395     struct proc *p = td->td_proc;
396     vm_size_t pageoff;
397     vm_prot_t maxprot;
398     vm_offset_t addr;
399     void *handle;
400     int error;
401     vm_object_t obj;
402     struct vmspace *vms = p->p_vmspace;
403     struct vnode *vp;
404
405     prot &= VM_PROT_ALL;
406
407     if ((ssize_t)size < 0 || (flags & MAP_ANON))
408         return(EINVAL);
409
410     pageoff = (pos & PAGE_MASK);
411     pos -= pageoff;
412
413     /* Adjust size for rounding (on both ends). */
414     size += pageoff;                            /* low end... */
415     size = (vm_size_t)round_page(size);         /* hi end */
416     addr = (vm_offset_t)addr_arg;
417
418     /*
419      * Check for illegal addresses.  Watch out for address wrap... Note
420      * that VM_*_ADDRESS are not constants due to casts (argh).
421      */
422     if (flags & MAP_FIXED) {
423         /*
424          * The specified address must have the same remainder
425          * as the file offset taken modulo PAGE_SIZE, so it
426          * should be aligned after adjustment by pageoff.
427          */
428         addr -= pageoff;
429         if (addr & PAGE_MASK)
430             return (EINVAL);
431         /* Address range must be all in user VM space. */
432         if (VM_MAX_USER_ADDRESS > 0 && addr + size > VM_MAX_USER_ADDRESS)
433             return (EINVAL);
434         if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
435             return (EINVAL);
436         if (addr + size < addr)
437             return (EINVAL);
438     } else if (addr == 0 ||
439         (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
440          addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))
441     ) {
442         /*
443          * XXX for non-fixed mappings where no hint is provided or
444          * the hint would fall in the potential heap space,
445          * place it after the end of the largest possible heap.
446          *
447          * There should really be a pmap call to determine a reasonable
448          * location.
449          */
450         addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz);
451     }
452
453     /*
454      * Mapping file, get fp for validation. Obtain vnode and make
455      * sure it is of appropriate type.
456      */
457     if (fp->f_type != DTYPE_VNODE)
458         return (EINVAL);
459
460     /*
461      * POSIX shared-memory objects are defined to have
462      * kernel persistence, and are not defined to support
463      * read(2)/write(2) -- or even open(2).  Thus, we can
464      * use MAP_ASYNC to trade on-disk coherence for speed.
465      * The shm_open(3) library routine turns on the FPOSIXSHM
466      * flag to request this behavior.
467      */
468     if (fp->f_flag & FPOSIXSHM)
469         flags |= MAP_NOSYNC;
470     vp = (struct vnode *) fp->f_data;
471     if (vp->v_type != VREG && vp->v_type != VCHR)
472         return (EINVAL);
473
474     /*
475      * Get the proper underlying object
476      */
477     if (vp->v_type == VREG) {
478         if ((obj = vp->v_object) == NULL)
479             return (EINVAL);
480         KKASSERT(vp == (struct vnode *)obj->handle);
481     }
482
483     /*
484      * XXX hack to handle use of /dev/zero to map anon memory (ala
485      * SunOS).
486      */
487     if (vp->v_type == VCHR && iszerodev(vp->v_rdev)) {
488         handle = NULL;
489         maxprot = VM_PROT_ALL;
490         flags |= MAP_ANON;
491         pos = 0;
492     } else {
493         /*
494          * cdevs does not provide private mappings of any kind.
495          */
496         if (vp->v_type == VCHR && 
497             (flags & (MAP_PRIVATE|MAP_COPY))) {
498                 error = EINVAL;
499                 goto done;
500         }
501         /*
502          * Ensure that file and memory protections are
503          * compatible.  Note that we only worry about
504          * writability if mapping is shared; in this case,
505          * current and max prot are dictated by the open file.
506          * XXX use the vnode instead?  Problem is: what
507          * credentials do we use for determination? What if
508          * proc does a setuid?
509          */
510         maxprot = VM_PROT_EXECUTE;      /* ??? */
511         if (fp->f_flag & FREAD) {
512             maxprot |= VM_PROT_READ;
513         } else if (prot & PROT_READ) {
514             error = EACCES;
515             goto done;
516         }
517         /*
518          * If we are sharing potential changes (either via
519          * MAP_SHARED or via the implicit sharing of character
520          * device mappings), and we are trying to get write
521          * permission although we opened it without asking
522          * for it, bail out.  
523          */
524
525         if ((flags & MAP_SHARED) != 0 ||
526             (vp->v_type == VCHR)
527         ) {
528             if ((fp->f_flag & FWRITE) != 0) {
529                 struct vattr va;
530                 if ((error = VOP_GETATTR(vp, &va))) {
531                     goto done;
532                 }
533                 if ((va.va_flags & (IMMUTABLE|APPEND)) == 0) {
534                     maxprot |= VM_PROT_WRITE;
535                 } else if (prot & PROT_WRITE) {
536                     error = EPERM;
537                     goto done;
538                 }
539             } else if ((prot & PROT_WRITE) != 0) {
540                 error = EACCES;
541                 goto done;
542             }
543         } else {
544             maxprot |= VM_PROT_WRITE;
545         }
546         handle = (void *)vp;
547     }
548     error = vm_mmap(&vms->vm_map, &addr, size, prot, 
549                     maxprot, flags, handle, pos);
550     if (error == 0 && addr_arg)
551         *resp = (void *)addr;
552 done:
553     return (error);
554 }
555
556 int
557 fp_close(file_t fp)
558 {
559     return(fdrop(fp));
560 }
561
562 int
563 fp_shutdown(file_t fp, int how)
564 {
565     return(fo_shutdown(fp, how));
566 }
567