mmap.c 2.8 KB

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