Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mmap.c |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996 by Volker Lendecke |
| 5 | * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <linux/stat.h> |
| 10 | #include <linux/time.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/shm.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/mman.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/fcntl.h> |
| 19 | #include <linux/ncp_fs.h> |
| 20 | |
| 21 | #include "ncplib_kernel.h" |
| 22 | #include <asm/uaccess.h> |
| 23 | #include <asm/system.h> |
| 24 | |
| 25 | /* |
| 26 | * Fill in the supplied page for mmap |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 27 | * XXX: how are we excluding truncate/invalidate here? Maybe need to lock |
| 28 | * page? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 30 | static int ncp_file_mmap_fault(struct vm_area_struct *area, |
| 31 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | struct file *file = area->vm_file; |
Josef Sipek | 92e5bae | 2006-12-08 02:37:22 -0800 | [diff] [blame] | 34 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | char *pg_addr; |
| 37 | unsigned int already_read; |
| 38 | unsigned int count; |
| 39 | int bufsize; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 40 | int pos; /* XXX: loff_t ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 42 | /* |
| 43 | * ncpfs has nothing against high pages as long |
| 44 | * as recvmsg and memset works on it |
| 45 | */ |
| 46 | vmf->page = alloc_page(GFP_HIGHUSER); |
| 47 | if (!vmf->page) |
| 48 | return VM_FAULT_OOM; |
| 49 | pg_addr = kmap(vmf->page); |
| 50 | pos = vmf->pgoff << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | count = PAGE_SIZE; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 53 | if ((unsigned long)vmf->virtual_address + PAGE_SIZE > area->vm_end) { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 54 | WARN_ON(1); /* shouldn't happen? */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 55 | count = area->vm_end - (unsigned long)vmf->virtual_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | /* what we can read in one go */ |
| 58 | bufsize = NCP_SERVER(inode)->buffer_size; |
| 59 | |
| 60 | already_read = 0; |
| 61 | if (ncp_make_open(inode, O_RDONLY) >= 0) { |
| 62 | while (already_read < count) { |
| 63 | int read_this_time; |
| 64 | int to_read; |
| 65 | |
| 66 | to_read = bufsize - (pos % bufsize); |
| 67 | |
| 68 | to_read = min_t(unsigned int, to_read, count - already_read); |
| 69 | |
| 70 | if (ncp_read_kernel(NCP_SERVER(inode), |
| 71 | NCP_FINFO(inode)->file_handle, |
| 72 | pos, to_read, |
| 73 | pg_addr + already_read, |
| 74 | &read_this_time) != 0) { |
| 75 | read_this_time = 0; |
| 76 | } |
| 77 | pos += read_this_time; |
| 78 | already_read += read_this_time; |
| 79 | |
| 80 | if (read_this_time < to_read) { |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | ncp_inode_close(inode); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | if (already_read < PAGE_SIZE) |
| 89 | memset(pg_addr + already_read, 0, PAGE_SIZE - already_read); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 90 | flush_dcache_page(vmf->page); |
| 91 | kunmap(vmf->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * If I understand ncp_read_kernel() properly, the above always |
| 95 | * fetches from the network, here the analogue of disk. |
| 96 | * -- wli |
| 97 | */ |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 98 | count_vm_event(PGMAJFAULT); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 99 | return VM_FAULT_MAJOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static struct vm_operations_struct ncp_file_mmap = |
| 103 | { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 104 | .fault = ncp_file_mmap_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | |
| 108 | /* This is used for a general mmap of a ncp file */ |
| 109 | int ncp_mmap(struct file *file, struct vm_area_struct *vma) |
| 110 | { |
Josef Sipek | 92e5bae | 2006-12-08 02:37:22 -0800 | [diff] [blame] | 111 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | DPRINTK("ncp_mmap: called\n"); |
| 114 | |
| 115 | if (!ncp_conn_valid(NCP_SERVER(inode))) |
| 116 | return -EIO; |
| 117 | |
| 118 | /* only PAGE_COW or read-only supported now */ |
| 119 | if (vma->vm_flags & VM_SHARED) |
| 120 | return -EINVAL; |
| 121 | /* we do not support files bigger than 4GB... We eventually |
| 122 | supports just 4GB... */ |
| 123 | if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff |
| 124 | > (1U << (32 - PAGE_SHIFT))) |
| 125 | return -EFBIG; |
| 126 | |
| 127 | vma->vm_ops = &ncp_file_mmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | file_accessed(file); |
| 129 | return 0; |
| 130 | } |