mmap.c 2.9 KB

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