RDMA/ucma: Check that device exists prior to accessing it
[pandora-kernel.git] / drivers / char / mem.c
1 /*
2  *  linux/drivers/char/mem.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Added devfs support.
7  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8  *  Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9  */
10
11 #include <linux/mm.h>
12 #include <linux/miscdevice.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mman.h>
16 #include <linux/random.h>
17 #include <linux/init.h>
18 #include <linux/raw.h>
19 #include <linux/tty.h>
20 #include <linux/capability.h>
21 #include <linux/ptrace.h>
22 #include <linux/device.h>
23 #include <linux/highmem.h>
24 #include <linux/crash_dump.h>
25 #include <linux/backing-dev.h>
26 #include <linux/bootmem.h>
27 #include <linux/splice.h>
28 #include <linux/pfn.h>
29 #include <linux/export.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
33
34 #ifdef CONFIG_IA64
35 # include <linux/efi.h>
36 #endif
37
38 static inline unsigned long size_inside_page(unsigned long start,
39                                              unsigned long size)
40 {
41         unsigned long sz;
42
43         sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
44
45         return min(sz, size);
46 }
47
48 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
49 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
50 {
51         return addr + count <= __pa(high_memory);
52 }
53
54 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
55 {
56         return 1;
57 }
58 #endif
59
60 #ifdef CONFIG_STRICT_DEVMEM
61 static inline int page_is_allowed(unsigned long pfn)
62 {
63         return devmem_is_allowed(pfn);
64 }
65 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
66 {
67         u64 from = ((u64)pfn) << PAGE_SHIFT;
68         u64 to = from + size;
69         u64 cursor = from;
70
71         while (cursor < to) {
72                 if (!devmem_is_allowed(pfn)) {
73                         printk(KERN_INFO
74                 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
75                                 current->comm, from, to);
76                         return 0;
77                 }
78                 cursor += PAGE_SIZE;
79                 pfn++;
80         }
81         return 1;
82 }
83 #else
84 static inline int page_is_allowed(unsigned long pfn)
85 {
86         return 1;
87 }
88 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
89 {
90         return 1;
91 }
92 #endif
93
94 void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
95 {
96 }
97
98 /*
99  * This funcion reads the *physical* memory. The f_pos points directly to the
100  * memory location.
101  */
102 static ssize_t read_mem(struct file *file, char __user *buf,
103                         size_t count, loff_t *ppos)
104 {
105         unsigned long p = *ppos;
106         ssize_t read, sz;
107         char *ptr;
108
109         if (!valid_phys_addr_range(p, count))
110                 return -EFAULT;
111         read = 0;
112 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
113         /* we don't have page 0 mapped on sparc and m68k.. */
114         if (p < PAGE_SIZE) {
115                 sz = size_inside_page(p, count);
116                 if (sz > 0) {
117                         if (clear_user(buf, sz))
118                                 return -EFAULT;
119                         buf += sz;
120                         p += sz;
121                         count -= sz;
122                         read += sz;
123                 }
124         }
125 #endif
126
127         while (count > 0) {
128                 unsigned long remaining;
129                 int allowed;
130
131                 sz = size_inside_page(p, count);
132
133                 allowed = page_is_allowed(p >> PAGE_SHIFT);
134                 if (!allowed)
135                         return -EPERM;
136                 if (allowed == 2) {
137                         /* Show zeros for restricted memory. */
138                         remaining = clear_user(buf, sz);
139                 } else {
140                         /*
141                          * On ia64 if a page has been mapped somewhere as
142                          * uncached, then it must also be accessed uncached
143                          * by the kernel or data corruption may occur.
144                          */
145                         ptr = xlate_dev_mem_ptr(p);
146                         if (!ptr)
147                                 return -EFAULT;
148
149                         remaining = copy_to_user(buf, ptr, sz);
150
151                         unxlate_dev_mem_ptr(p, ptr);
152                 }
153
154                 if (remaining)
155                         return -EFAULT;
156
157                 buf += sz;
158                 p += sz;
159                 count -= sz;
160                 read += sz;
161         }
162
163         *ppos += read;
164         return read;
165 }
166
167 static ssize_t write_mem(struct file *file, const char __user *buf,
168                          size_t count, loff_t *ppos)
169 {
170         unsigned long p = *ppos;
171         ssize_t written, sz;
172         unsigned long copied;
173         void *ptr;
174
175         if (!valid_phys_addr_range(p, count))
176                 return -EFAULT;
177
178         written = 0;
179
180 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
181         /* we don't have page 0 mapped on sparc and m68k.. */
182         if (p < PAGE_SIZE) {
183                 sz = size_inside_page(p, count);
184                 /* Hmm. Do something? */
185                 buf += sz;
186                 p += sz;
187                 count -= sz;
188                 written += sz;
189         }
190 #endif
191
192         while (count > 0) {
193                 int allowed;
194
195                 sz = size_inside_page(p, count);
196
197                 allowed = page_is_allowed(p >> PAGE_SHIFT);
198                 if (!allowed)
199                         return -EPERM;
200
201                 /* Skip actual writing when a page is marked as restricted. */
202                 if (allowed == 1) {
203                         /*
204                          * On ia64 if a page has been mapped somewhere as
205                          * uncached, then it must also be accessed uncached
206                          * by the kernel or data corruption may occur.
207                          */
208                         ptr = xlate_dev_mem_ptr(p);
209                         if (!ptr) {
210                                 if (written)
211                                         break;
212                                 return -EFAULT;
213                         }
214
215                         copied = copy_from_user(ptr, buf, sz);
216                         unxlate_dev_mem_ptr(p, ptr);
217                         if (copied) {
218                                 written += sz - copied;
219                                 if (written)
220                                         break;
221                                 return -EFAULT;
222                         }
223                 }
224
225                 buf += sz;
226                 p += sz;
227                 count -= sz;
228                 written += sz;
229         }
230
231         *ppos += written;
232         return written;
233 }
234
235 int __weak phys_mem_access_prot_allowed(struct file *file,
236         unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
237 {
238         return 1;
239 }
240
241 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
242
243 /*
244  * Architectures vary in how they handle caching for addresses
245  * outside of main memory.
246  *
247  */
248 #ifdef pgprot_noncached
249 static int uncached_access(struct file *file, unsigned long addr)
250 {
251 #if defined(CONFIG_IA64)
252         /*
253          * On ia64, we ignore O_DSYNC because we cannot tolerate memory
254          * attribute aliases.
255          */
256         return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
257 #elif defined(CONFIG_MIPS)
258         {
259                 extern int __uncached_access(struct file *file,
260                                              unsigned long addr);
261
262                 return __uncached_access(file, addr);
263         }
264 #else
265         /*
266          * Accessing memory above the top the kernel knows about or through a
267          * file pointer
268          * that was marked O_DSYNC will be done non-cached.
269          */
270         if (file->f_flags & O_DSYNC)
271                 return 1;
272         return addr >= __pa(high_memory);
273 #endif
274 }
275 #endif
276
277 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
278                                      unsigned long size, pgprot_t vma_prot)
279 {
280 #ifdef pgprot_noncached
281         unsigned long offset = pfn << PAGE_SHIFT;
282
283         if (uncached_access(file, offset))
284                 return pgprot_noncached(vma_prot);
285 #endif
286         return vma_prot;
287 }
288 #endif
289
290 #ifndef CONFIG_MMU
291 static unsigned long get_unmapped_area_mem(struct file *file,
292                                            unsigned long addr,
293                                            unsigned long len,
294                                            unsigned long pgoff,
295                                            unsigned long flags)
296 {
297         if (!valid_mmap_phys_addr_range(pgoff, len))
298                 return (unsigned long) -EINVAL;
299         return pgoff << PAGE_SHIFT;
300 }
301
302 /* can't do an in-place private mapping if there's no MMU */
303 static inline int private_mapping_ok(struct vm_area_struct *vma)
304 {
305         return vma->vm_flags & VM_MAYSHARE;
306 }
307 #else
308 #define get_unmapped_area_mem   NULL
309
310 static inline int private_mapping_ok(struct vm_area_struct *vma)
311 {
312         return 1;
313 }
314 #endif
315
316 static const struct vm_operations_struct mmap_mem_ops = {
317 #ifdef CONFIG_HAVE_IOREMAP_PROT
318         .access = generic_access_phys
319 #endif
320 };
321
322 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
323 {
324         size_t size = vma->vm_end - vma->vm_start;
325         phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
326
327         /* It's illegal to wrap around the end of the physical address space. */
328         if (offset + (phys_addr_t)size - 1 < offset)
329                 return -EINVAL;
330
331         if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
332                 return -EINVAL;
333
334         if (!private_mapping_ok(vma))
335                 return -ENOSYS;
336
337         if (!range_is_allowed(vma->vm_pgoff, size))
338                 return -EPERM;
339
340         if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
341                                                 &vma->vm_page_prot))
342                 return -EINVAL;
343
344         vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
345                                                  size,
346                                                  vma->vm_page_prot);
347
348         vma->vm_ops = &mmap_mem_ops;
349
350         /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
351         if (remap_pfn_range(vma,
352                             vma->vm_start,
353                             vma->vm_pgoff,
354                             size,
355                             vma->vm_page_prot)) {
356                 return -EAGAIN;
357         }
358         return 0;
359 }
360
361 #ifdef CONFIG_DEVKMEM
362 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
363 {
364         unsigned long pfn;
365
366         /* Turn a kernel-virtual address into a physical page frame */
367         pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
368
369         /*
370          * RED-PEN: on some architectures there is more mapped memory than
371          * available in mem_map which pfn_valid checks for. Perhaps should add a
372          * new macro here.
373          *
374          * RED-PEN: vmalloc is not supported right now.
375          */
376         if (!pfn_valid(pfn))
377                 return -EIO;
378
379         vma->vm_pgoff = pfn;
380         return mmap_mem(file, vma);
381 }
382 #endif
383
384 #ifdef CONFIG_CRASH_DUMP
385 /*
386  * Read memory corresponding to the old kernel.
387  */
388 static ssize_t read_oldmem(struct file *file, char __user *buf,
389                                 size_t count, loff_t *ppos)
390 {
391         unsigned long pfn, offset;
392         size_t read = 0, csize;
393         int rc = 0;
394
395         while (count) {
396                 pfn = *ppos / PAGE_SIZE;
397                 if (pfn > saved_max_pfn)
398                         return read;
399
400                 offset = (unsigned long)(*ppos % PAGE_SIZE);
401                 if (count > PAGE_SIZE - offset)
402                         csize = PAGE_SIZE - offset;
403                 else
404                         csize = count;
405
406                 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
407                 if (rc < 0)
408                         return rc;
409                 buf += csize;
410                 *ppos += csize;
411                 read += csize;
412                 count -= csize;
413         }
414         return read;
415 }
416 #endif
417
418 #ifdef CONFIG_DEVKMEM
419 /*
420  * This function reads the *virtual* memory as seen by the kernel.
421  */
422 static ssize_t read_kmem(struct file *file, char __user *buf,
423                          size_t count, loff_t *ppos)
424 {
425         unsigned long p = *ppos;
426         ssize_t low_count, read, sz;
427         char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
428         int err = 0;
429
430         read = 0;
431         if (p < (unsigned long) high_memory) {
432                 low_count = count;
433                 if (count > (unsigned long)high_memory - p)
434                         low_count = (unsigned long)high_memory - p;
435
436 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
437                 /* we don't have page 0 mapped on sparc and m68k.. */
438                 if (p < PAGE_SIZE && low_count > 0) {
439                         sz = size_inside_page(p, low_count);
440                         if (clear_user(buf, sz))
441                                 return -EFAULT;
442                         buf += sz;
443                         p += sz;
444                         read += sz;
445                         low_count -= sz;
446                         count -= sz;
447                 }
448 #endif
449                 while (low_count > 0) {
450                         sz = size_inside_page(p, low_count);
451
452                         /*
453                          * On ia64 if a page has been mapped somewhere as
454                          * uncached, then it must also be accessed uncached
455                          * by the kernel or data corruption may occur
456                          */
457                         kbuf = xlate_dev_kmem_ptr((char *)p);
458
459                         if (copy_to_user(buf, kbuf, sz))
460                                 return -EFAULT;
461                         buf += sz;
462                         p += sz;
463                         read += sz;
464                         low_count -= sz;
465                         count -= sz;
466                 }
467         }
468
469         if (count > 0) {
470                 kbuf = (char *)__get_free_page(GFP_KERNEL);
471                 if (!kbuf)
472                         return -ENOMEM;
473                 while (count > 0) {
474                         sz = size_inside_page(p, count);
475                         if (!is_vmalloc_or_module_addr((void *)p)) {
476                                 err = -ENXIO;
477                                 break;
478                         }
479                         sz = vread(kbuf, (char *)p, sz);
480                         if (!sz)
481                                 break;
482                         if (copy_to_user(buf, kbuf, sz)) {
483                                 err = -EFAULT;
484                                 break;
485                         }
486                         count -= sz;
487                         buf += sz;
488                         read += sz;
489                         p += sz;
490                 }
491                 free_page((unsigned long)kbuf);
492         }
493         *ppos = p;
494         return read ? read : err;
495 }
496
497
498 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
499                                 size_t count, loff_t *ppos)
500 {
501         ssize_t written, sz;
502         unsigned long copied;
503
504         written = 0;
505 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
506         /* we don't have page 0 mapped on sparc and m68k.. */
507         if (p < PAGE_SIZE) {
508                 sz = size_inside_page(p, count);
509                 /* Hmm. Do something? */
510                 buf += sz;
511                 p += sz;
512                 count -= sz;
513                 written += sz;
514         }
515 #endif
516
517         while (count > 0) {
518                 char *ptr;
519
520                 sz = size_inside_page(p, count);
521
522                 /*
523                  * On ia64 if a page has been mapped somewhere as uncached, then
524                  * it must also be accessed uncached by the kernel or data
525                  * corruption may occur.
526                  */
527                 ptr = xlate_dev_kmem_ptr((char *)p);
528
529                 copied = copy_from_user(ptr, buf, sz);
530                 if (copied) {
531                         written += sz - copied;
532                         if (written)
533                                 break;
534                         return -EFAULT;
535                 }
536                 buf += sz;
537                 p += sz;
538                 count -= sz;
539                 written += sz;
540         }
541
542         *ppos += written;
543         return written;
544 }
545
546 /*
547  * This function writes to the *virtual* memory as seen by the kernel.
548  */
549 static ssize_t write_kmem(struct file *file, const char __user *buf,
550                           size_t count, loff_t *ppos)
551 {
552         unsigned long p = *ppos;
553         ssize_t wrote = 0;
554         ssize_t virtr = 0;
555         char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
556         int err = 0;
557
558         if (p < (unsigned long) high_memory) {
559                 unsigned long to_write = min_t(unsigned long, count,
560                                                (unsigned long)high_memory - p);
561                 wrote = do_write_kmem(p, buf, to_write, ppos);
562                 if (wrote != to_write)
563                         return wrote;
564                 p += wrote;
565                 buf += wrote;
566                 count -= wrote;
567         }
568
569         if (count > 0) {
570                 kbuf = (char *)__get_free_page(GFP_KERNEL);
571                 if (!kbuf)
572                         return wrote ? wrote : -ENOMEM;
573                 while (count > 0) {
574                         unsigned long sz = size_inside_page(p, count);
575                         unsigned long n;
576
577                         if (!is_vmalloc_or_module_addr((void *)p)) {
578                                 err = -ENXIO;
579                                 break;
580                         }
581                         n = copy_from_user(kbuf, buf, sz);
582                         if (n) {
583                                 err = -EFAULT;
584                                 break;
585                         }
586                         vwrite(kbuf, (char *)p, sz);
587                         count -= sz;
588                         buf += sz;
589                         virtr += sz;
590                         p += sz;
591                 }
592                 free_page((unsigned long)kbuf);
593         }
594
595         *ppos = p;
596         return virtr + wrote ? : err;
597 }
598 #endif
599
600 #ifdef CONFIG_DEVPORT
601 static ssize_t read_port(struct file *file, char __user *buf,
602                          size_t count, loff_t *ppos)
603 {
604         unsigned long i = *ppos;
605         char __user *tmp = buf;
606
607         if (!access_ok(VERIFY_WRITE, buf, count))
608                 return -EFAULT;
609         while (count-- > 0 && i < 65536) {
610                 if (__put_user(inb(i), tmp) < 0)
611                         return -EFAULT;
612                 i++;
613                 tmp++;
614         }
615         *ppos = i;
616         return tmp-buf;
617 }
618
619 static ssize_t write_port(struct file *file, const char __user *buf,
620                           size_t count, loff_t *ppos)
621 {
622         unsigned long i = *ppos;
623         const char __user * tmp = buf;
624
625         if (!access_ok(VERIFY_READ, buf, count))
626                 return -EFAULT;
627         while (count-- > 0 && i < 65536) {
628                 char c;
629                 if (__get_user(c, tmp)) {
630                         if (tmp > buf)
631                                 break;
632                         return -EFAULT;
633                 }
634                 outb(c, i);
635                 i++;
636                 tmp++;
637         }
638         *ppos = i;
639         return tmp-buf;
640 }
641 #endif
642
643 static ssize_t read_null(struct file *file, char __user *buf,
644                          size_t count, loff_t *ppos)
645 {
646         return 0;
647 }
648
649 static ssize_t write_null(struct file *file, const char __user *buf,
650                           size_t count, loff_t *ppos)
651 {
652         return count;
653 }
654
655 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
656                         struct splice_desc *sd)
657 {
658         return sd->len;
659 }
660
661 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
662                                  loff_t *ppos, size_t len, unsigned int flags)
663 {
664         return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
665 }
666
667 static ssize_t read_zero(struct file *file, char __user *buf,
668                          size_t count, loff_t *ppos)
669 {
670         size_t written;
671
672         if (!count)
673                 return 0;
674
675         if (!access_ok(VERIFY_WRITE, buf, count))
676                 return -EFAULT;
677
678         written = 0;
679         while (count) {
680                 unsigned long unwritten;
681                 size_t chunk = count;
682
683                 if (chunk > PAGE_SIZE)
684                         chunk = PAGE_SIZE;      /* Just for latency reasons */
685                 unwritten = __clear_user(buf, chunk);
686                 written += chunk - unwritten;
687                 if (unwritten)
688                         break;
689                 if (signal_pending(current))
690                         return written ? written : -ERESTARTSYS;
691                 buf += chunk;
692                 count -= chunk;
693                 cond_resched();
694         }
695         return written ? written : -EFAULT;
696 }
697
698 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
699 {
700 #ifndef CONFIG_MMU
701         return -ENOSYS;
702 #endif
703         if (vma->vm_flags & VM_SHARED)
704                 return shmem_zero_setup(vma);
705         return 0;
706 }
707
708 static ssize_t write_full(struct file *file, const char __user *buf,
709                           size_t count, loff_t *ppos)
710 {
711         return -ENOSPC;
712 }
713
714 /*
715  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
716  * can fopen() both devices with "a" now.  This was previously impossible.
717  * -- SRB.
718  */
719 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
720 {
721         return file->f_pos = 0;
722 }
723
724 /*
725  * The memory devices use the full 32/64 bits of the offset, and so we cannot
726  * check against negative addresses: they are ok. The return value is weird,
727  * though, in that case (0).
728  *
729  * also note that seeking relative to the "end of file" isn't supported:
730  * it has no meaning, so it returns -EINVAL.
731  */
732 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
733 {
734         loff_t ret;
735
736         mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
737         switch (orig) {
738         case SEEK_CUR:
739                 offset += file->f_pos;
740         case SEEK_SET:
741                 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
742                 if ((unsigned long long)offset >= ~0xFFFULL) {
743                         ret = -EOVERFLOW;
744                         break;
745                 }
746                 file->f_pos = offset;
747                 ret = file->f_pos;
748                 force_successful_syscall_return();
749                 break;
750         default:
751                 ret = -EINVAL;
752         }
753         mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
754         return ret;
755 }
756
757 static int open_port(struct inode * inode, struct file * filp)
758 {
759         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
760 }
761
762 #define zero_lseek      null_lseek
763 #define full_lseek      null_lseek
764 #define write_zero      write_null
765 #define read_full       read_zero
766 #define open_mem        open_port
767 #define open_kmem       open_mem
768 #define open_oldmem     open_mem
769
770 static const struct file_operations mem_fops = {
771         .llseek         = memory_lseek,
772         .read           = read_mem,
773         .write          = write_mem,
774         .mmap           = mmap_mem,
775         .open           = open_mem,
776         .get_unmapped_area = get_unmapped_area_mem,
777 };
778
779 #ifdef CONFIG_DEVKMEM
780 static const struct file_operations kmem_fops = {
781         .llseek         = memory_lseek,
782         .read           = read_kmem,
783         .write          = write_kmem,
784         .mmap           = mmap_kmem,
785         .open           = open_kmem,
786         .get_unmapped_area = get_unmapped_area_mem,
787 };
788 #endif
789
790 static const struct file_operations null_fops = {
791         .llseek         = null_lseek,
792         .read           = read_null,
793         .write          = write_null,
794         .splice_write   = splice_write_null,
795 };
796
797 #ifdef CONFIG_DEVPORT
798 static const struct file_operations port_fops = {
799         .llseek         = memory_lseek,
800         .read           = read_port,
801         .write          = write_port,
802         .open           = open_port,
803 };
804 #endif
805
806 static const struct file_operations zero_fops = {
807         .llseek         = zero_lseek,
808         .read           = read_zero,
809         .write          = write_zero,
810         .mmap           = mmap_zero,
811 };
812
813 /*
814  * capabilities for /dev/zero
815  * - permits private mappings, "copies" are taken of the source of zeros
816  * - no writeback happens
817  */
818 static struct backing_dev_info zero_bdi = {
819         .name           = "char/mem",
820         .capabilities   = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
821 };
822
823 static const struct file_operations full_fops = {
824         .llseek         = full_lseek,
825         .read           = read_full,
826         .write          = write_full,
827 };
828
829 #ifdef CONFIG_CRASH_DUMP
830 static const struct file_operations oldmem_fops = {
831         .read   = read_oldmem,
832         .open   = open_oldmem,
833         .llseek = default_llseek,
834 };
835 #endif
836
837 static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
838                            unsigned long count, loff_t pos)
839 {
840         char *line, *p;
841         int i;
842         ssize_t ret = -EFAULT;
843         size_t len = iov_length(iv, count);
844
845         line = kmalloc(len + 1, GFP_KERNEL);
846         if (line == NULL)
847                 return -ENOMEM;
848
849         /*
850          * copy all vectors into a single string, to ensure we do
851          * not interleave our log line with other printk calls
852          */
853         p = line;
854         for (i = 0; i < count; i++) {
855                 if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
856                         goto out;
857                 p += iv[i].iov_len;
858         }
859         p[0] = '\0';
860
861         ret = printk("%s", line);
862         /* printk can add a prefix */
863         if (ret > len)
864                 ret = len;
865 out:
866         kfree(line);
867         return ret;
868 }
869
870 static const struct file_operations kmsg_fops = {
871         .aio_write = kmsg_writev,
872         .llseek = noop_llseek,
873 };
874
875 static const struct memdev {
876         const char *name;
877         mode_t mode;
878         const struct file_operations *fops;
879         struct backing_dev_info *dev_info;
880 } devlist[] = {
881          [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
882 #ifdef CONFIG_DEVKMEM
883          [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
884 #endif
885          [3] = { "null", 0666, &null_fops, NULL },
886 #ifdef CONFIG_DEVPORT
887          [4] = { "port", 0, &port_fops, NULL },
888 #endif
889          [5] = { "zero", 0666, &zero_fops, &zero_bdi },
890          [7] = { "full", 0666, &full_fops, NULL },
891          [8] = { "random", 0666, &random_fops, NULL },
892          [9] = { "urandom", 0666, &urandom_fops, NULL },
893         [11] = { "kmsg", 0, &kmsg_fops, NULL },
894 #ifdef CONFIG_CRASH_DUMP
895         [12] = { "oldmem", 0, &oldmem_fops, NULL },
896 #endif
897 };
898
899 static int memory_open(struct inode *inode, struct file *filp)
900 {
901         int minor;
902         const struct memdev *dev;
903
904         minor = iminor(inode);
905         if (minor >= ARRAY_SIZE(devlist))
906                 return -ENXIO;
907
908         dev = &devlist[minor];
909         if (!dev->fops)
910                 return -ENXIO;
911
912         filp->f_op = dev->fops;
913         if (dev->dev_info)
914                 filp->f_mapping->backing_dev_info = dev->dev_info;
915
916         /* Is /dev/mem or /dev/kmem ? */
917         if (dev->dev_info == &directly_mappable_cdev_bdi)
918                 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
919
920         if (dev->fops->open)
921                 return dev->fops->open(inode, filp);
922
923         return 0;
924 }
925
926 static const struct file_operations memory_fops = {
927         .open = memory_open,
928         .llseek = noop_llseek,
929 };
930
931 static char *mem_devnode(struct device *dev, mode_t *mode)
932 {
933         if (mode && devlist[MINOR(dev->devt)].mode)
934                 *mode = devlist[MINOR(dev->devt)].mode;
935         return NULL;
936 }
937
938 static struct class *mem_class;
939
940 static int __init chr_dev_init(void)
941 {
942         int minor;
943         int err;
944
945         err = bdi_init(&zero_bdi);
946         if (err)
947                 return err;
948
949         if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
950                 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
951
952         mem_class = class_create(THIS_MODULE, "mem");
953         if (IS_ERR(mem_class))
954                 return PTR_ERR(mem_class);
955
956         mem_class->devnode = mem_devnode;
957         for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
958                 if (!devlist[minor].name)
959                         continue;
960                 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
961                               NULL, devlist[minor].name);
962         }
963
964         return tty_init();
965 }
966
967 fs_initcall(chr_dev_init);