mmap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <linux/stat.h>
  9. #include <linux/time.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/shm.h>
  13. #include <linux/errno.h>
  14. #include <linux/mman.h>
  15. #include <linux/string.h>
  16. #include <linux/slab.h>
  17. #include <linux/fcntl.h>
  18. #include <linux/ncp_fs.h>
  19. #include "ncplib_kernel.h"
  20. #include <asm/uaccess.h>
  21. #include <asm/system.h>
  22. /*
  23. * Fill in the supplied page for mmap
  24. */
  25. static struct page* ncp_file_mmap_fault(struct vm_area_struct *area,
  26. struct fault_data *fdata)
  27. {
  28. struct file *file = area->vm_file;
  29. struct dentry *dentry = file->f_path.dentry;
  30. struct inode *inode = dentry->d_inode;
  31. struct page* page;
  32. char *pg_addr;
  33. unsigned int already_read;
  34. unsigned int count;
  35. int bufsize;
  36. int pos;
  37. page = alloc_page(GFP_HIGHUSER); /* ncpfs has nothing against high pages
  38. as long as recvmsg and memset works on it */
  39. if (!page) {
  40. fdata->type = VM_FAULT_OOM;
  41. return NULL;
  42. }
  43. pg_addr = kmap(page);
  44. pos = fdata->pgoff << PAGE_SHIFT;
  45. count = PAGE_SIZE;
  46. if (fdata->address + PAGE_SIZE > area->vm_end) {
  47. WARN_ON(1); /* shouldn't happen? */
  48. count = area->vm_end - fdata->address;
  49. }
  50. /* what we can read in one go */
  51. bufsize = NCP_SERVER(inode)->buffer_size;
  52. already_read = 0;
  53. if (ncp_make_open(inode, O_RDONLY) >= 0) {
  54. while (already_read < count) {
  55. int read_this_time;
  56. int to_read;
  57. to_read = bufsize - (pos % bufsize);
  58. to_read = min_t(unsigned int, to_read, count - already_read);
  59. if (ncp_read_kernel(NCP_SERVER(inode),
  60. NCP_FINFO(inode)->file_handle,
  61. pos, to_read,
  62. pg_addr + already_read,
  63. &read_this_time) != 0) {
  64. read_this_time = 0;
  65. }
  66. pos += read_this_time;
  67. already_read += read_this_time;
  68. if (read_this_time < to_read) {
  69. break;
  70. }
  71. }
  72. ncp_inode_close(inode);
  73. }
  74. if (already_read < PAGE_SIZE)
  75. memset(pg_addr + already_read, 0, PAGE_SIZE - already_read);
  76. flush_dcache_page(page);
  77. kunmap(page);
  78. /*
  79. * If I understand ncp_read_kernel() properly, the above always
  80. * fetches from the network, here the analogue of disk.
  81. * -- wli
  82. */
  83. fdata->type = VM_FAULT_MAJOR;
  84. count_vm_event(PGMAJFAULT);
  85. return page;
  86. }
  87. static struct vm_operations_struct ncp_file_mmap =
  88. {
  89. .fault = ncp_file_mmap_fault,
  90. };
  91. /* This is used for a general mmap of a ncp file */
  92. int ncp_mmap(struct file *file, struct vm_area_struct *vma)
  93. {
  94. struct inode *inode = file->f_path.dentry->d_inode;
  95. DPRINTK("ncp_mmap: called\n");
  96. if (!ncp_conn_valid(NCP_SERVER(inode)))
  97. return -EIO;
  98. /* only PAGE_COW or read-only supported now */
  99. if (vma->vm_flags & VM_SHARED)
  100. return -EINVAL;
  101. /* we do not support files bigger than 4GB... We eventually
  102. supports just 4GB... */
  103. if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff
  104. > (1U << (32 - PAGE_SHIFT)))
  105. return -EFBIG;
  106. vma->vm_ops = &ncp_file_mmap;
  107. vma->vm_flags |= VM_CAN_INVALIDATE;
  108. file_accessed(file);
  109. return 0;
  110. }