mmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/gfp.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/fcntl.h>
  18. #include <linux/memcontrol.h>
  19. #include <asm/uaccess.h>
  20. #include "ncp_fs.h"
  21. /*
  22. * Fill in the supplied page for mmap
  23. * XXX: how are we excluding truncate/invalidate here? Maybe need to lock
  24. * page?
  25. */
  26. static int ncp_file_mmap_fault(struct vm_area_struct *area,
  27. struct vm_fault *vmf)
  28. {
  29. struct inode *inode = file_inode(area->vm_file);
  30. char *pg_addr;
  31. unsigned int already_read;
  32. unsigned int count;
  33. int bufsize;
  34. int pos; /* XXX: loff_t ? */
  35. /*
  36. * ncpfs has nothing against high pages as long
  37. * as recvmsg and memset works on it
  38. */
  39. vmf->page = alloc_page(GFP_HIGHUSER);
  40. if (!vmf->page)
  41. return VM_FAULT_OOM;
  42. pg_addr = kmap(vmf->page);
  43. pos = vmf->pgoff << PAGE_SHIFT;
  44. count = PAGE_SIZE;
  45. /* what we can read in one go */
  46. bufsize = NCP_SERVER(inode)->buffer_size;
  47. already_read = 0;
  48. if (ncp_make_open(inode, O_RDONLY) >= 0) {
  49. while (already_read < count) {
  50. int read_this_time;
  51. int to_read;
  52. to_read = bufsize - (pos % bufsize);
  53. to_read = min_t(unsigned int, to_read, count - already_read);
  54. if (ncp_read_kernel(NCP_SERVER(inode),
  55. NCP_FINFO(inode)->file_handle,
  56. pos, to_read,
  57. pg_addr + already_read,
  58. &read_this_time) != 0) {
  59. read_this_time = 0;
  60. }
  61. pos += read_this_time;
  62. already_read += read_this_time;
  63. if (read_this_time < to_read) {
  64. break;
  65. }
  66. }
  67. ncp_inode_close(inode);
  68. }
  69. if (already_read < PAGE_SIZE)
  70. memset(pg_addr + already_read, 0, PAGE_SIZE - already_read);
  71. flush_dcache_page(vmf->page);
  72. kunmap(vmf->page);
  73. /*
  74. * If I understand ncp_read_kernel() properly, the above always
  75. * fetches from the network, here the analogue of disk.
  76. * -- nyc
  77. */
  78. count_vm_event(PGMAJFAULT);
  79. mem_cgroup_count_vm_event(area->vm_mm, PGMAJFAULT);
  80. return VM_FAULT_MAJOR;
  81. }
  82. static const struct vm_operations_struct ncp_file_mmap =
  83. {
  84. .fault = ncp_file_mmap_fault,
  85. };
  86. /* This is used for a general mmap of a ncp file */
  87. int ncp_mmap(struct file *file, struct vm_area_struct *vma)
  88. {
  89. struct inode *inode = file_inode(file);
  90. ncp_dbg(1, "called\n");
  91. if (!ncp_conn_valid(NCP_SERVER(inode)))
  92. return -EIO;
  93. /* only PAGE_COW or read-only supported now */
  94. if (vma->vm_flags & VM_SHARED)
  95. return -EINVAL;
  96. /* we do not support files bigger than 4GB... We eventually
  97. supports just 4GB... */
  98. if (vma_pages(vma) + vma->vm_pgoff
  99. > (1U << (32 - PAGE_SHIFT)))
  100. return -EFBIG;
  101. vma->vm_ops = &ncp_file_mmap;
  102. file_accessed(file);
  103. return 0;
  104. }