filemap_xip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * linux/mm/filemap_xip.c
  3. *
  4. * Copyright (C) 2005 IBM Corporation
  5. * Author: Carsten Otte <cotte@de.ibm.com>
  6. *
  7. * derived from linux/mm/filemap.c - Copyright (C) Linus Torvalds
  8. *
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/module.h>
  13. #include <linux/uio.h>
  14. #include <linux/rmap.h>
  15. #include <asm/tlbflush.h>
  16. #include "filemap.h"
  17. /*
  18. * This is a file read routine for execute in place files, and uses
  19. * the mapping->a_ops->get_xip_page() function for the actual low-level
  20. * stuff.
  21. *
  22. * Note the struct file* is not used at all. It may be NULL.
  23. */
  24. static void
  25. do_xip_mapping_read(struct address_space *mapping,
  26. struct file_ra_state *_ra,
  27. struct file *filp,
  28. loff_t *ppos,
  29. read_descriptor_t *desc,
  30. read_actor_t actor)
  31. {
  32. struct inode *inode = mapping->host;
  33. unsigned long index, end_index, offset;
  34. loff_t isize;
  35. BUG_ON(!mapping->a_ops->get_xip_page);
  36. index = *ppos >> PAGE_CACHE_SHIFT;
  37. offset = *ppos & ~PAGE_CACHE_MASK;
  38. isize = i_size_read(inode);
  39. if (!isize)
  40. goto out;
  41. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  42. for (;;) {
  43. struct page *page;
  44. unsigned long nr, ret;
  45. /* nr is the maximum number of bytes to copy from this page */
  46. nr = PAGE_CACHE_SIZE;
  47. if (index >= end_index) {
  48. if (index > end_index)
  49. goto out;
  50. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  51. if (nr <= offset) {
  52. goto out;
  53. }
  54. }
  55. nr = nr - offset;
  56. page = mapping->a_ops->get_xip_page(mapping,
  57. index*(PAGE_SIZE/512), 0);
  58. if (!page)
  59. goto no_xip_page;
  60. if (unlikely(IS_ERR(page))) {
  61. if (PTR_ERR(page) == -ENODATA) {
  62. /* sparse */
  63. page = ZERO_PAGE(0);
  64. } else {
  65. desc->error = PTR_ERR(page);
  66. goto out;
  67. }
  68. }
  69. /* If users can be writing to this page using arbitrary
  70. * virtual addresses, take care about potential aliasing
  71. * before reading the page on the kernel side.
  72. */
  73. if (mapping_writably_mapped(mapping))
  74. flush_dcache_page(page);
  75. /*
  76. * Ok, we have the page, so now we can copy it to user space...
  77. *
  78. * The actor routine returns how many bytes were actually used..
  79. * NOTE! This may not be the same as how much of a user buffer
  80. * we filled up (we may be padding etc), so we can only update
  81. * "pos" here (the actor routine has to update the user buffer
  82. * pointers and the remaining count).
  83. */
  84. ret = actor(desc, page, offset, nr);
  85. offset += ret;
  86. index += offset >> PAGE_CACHE_SHIFT;
  87. offset &= ~PAGE_CACHE_MASK;
  88. if (ret == nr && desc->count)
  89. continue;
  90. goto out;
  91. no_xip_page:
  92. /* Did not get the page. Report it */
  93. desc->error = -EIO;
  94. goto out;
  95. }
  96. out:
  97. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  98. if (filp)
  99. file_accessed(filp);
  100. }
  101. ssize_t
  102. xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  103. {
  104. read_descriptor_t desc;
  105. if (!access_ok(VERIFY_WRITE, buf, len))
  106. return -EFAULT;
  107. desc.written = 0;
  108. desc.arg.buf = buf;
  109. desc.count = len;
  110. desc.error = 0;
  111. do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
  112. ppos, &desc, file_read_actor);
  113. if (desc.written)
  114. return desc.written;
  115. else
  116. return desc.error;
  117. }
  118. EXPORT_SYMBOL_GPL(xip_file_read);
  119. ssize_t
  120. xip_file_sendfile(struct file *in_file, loff_t *ppos,
  121. size_t count, read_actor_t actor, void *target)
  122. {
  123. read_descriptor_t desc;
  124. if (!count)
  125. return 0;
  126. desc.written = 0;
  127. desc.count = count;
  128. desc.arg.data = target;
  129. desc.error = 0;
  130. do_xip_mapping_read(in_file->f_mapping, &in_file->f_ra, in_file,
  131. ppos, &desc, actor);
  132. if (desc.written)
  133. return desc.written;
  134. return desc.error;
  135. }
  136. EXPORT_SYMBOL_GPL(xip_file_sendfile);
  137. /*
  138. * __xip_unmap is invoked from xip_unmap and
  139. * xip_write
  140. *
  141. * This function walks all vmas of the address_space and unmaps the
  142. * ZERO_PAGE when found at pgoff. Should it go in rmap.c?
  143. */
  144. static void
  145. __xip_unmap (struct address_space * mapping,
  146. unsigned long pgoff)
  147. {
  148. struct vm_area_struct *vma;
  149. struct mm_struct *mm;
  150. struct prio_tree_iter iter;
  151. unsigned long address;
  152. pte_t *pte;
  153. pte_t pteval;
  154. spin_lock(&mapping->i_mmap_lock);
  155. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  156. mm = vma->vm_mm;
  157. address = vma->vm_start +
  158. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  159. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  160. /*
  161. * We need the page_table_lock to protect us from page faults,
  162. * munmap, fork, etc...
  163. */
  164. pte = page_check_address(ZERO_PAGE(address), mm,
  165. address);
  166. if (!IS_ERR(pte)) {
  167. /* Nuke the page table entry. */
  168. flush_cache_page(vma, address, pte_pfn(*pte));
  169. pteval = ptep_clear_flush(vma, address, pte);
  170. BUG_ON(pte_dirty(pteval));
  171. pte_unmap(pte);
  172. spin_unlock(&mm->page_table_lock);
  173. }
  174. }
  175. spin_unlock(&mapping->i_mmap_lock);
  176. }
  177. /*
  178. * xip_nopage() is invoked via the vma operations vector for a
  179. * mapped memory region to read in file data during a page fault.
  180. *
  181. * This function is derived from filemap_nopage, but used for execute in place
  182. */
  183. static struct page *
  184. xip_file_nopage(struct vm_area_struct * area,
  185. unsigned long address,
  186. int *type)
  187. {
  188. struct file *file = area->vm_file;
  189. struct address_space *mapping = file->f_mapping;
  190. struct inode *inode = mapping->host;
  191. struct page *page;
  192. unsigned long size, pgoff, endoff;
  193. pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT)
  194. + area->vm_pgoff;
  195. endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT)
  196. + area->vm_pgoff;
  197. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  198. if (pgoff >= size) {
  199. return NULL;
  200. }
  201. page = mapping->a_ops->get_xip_page(mapping, pgoff*(PAGE_SIZE/512), 0);
  202. if (!IS_ERR(page)) {
  203. return page;
  204. }
  205. if (PTR_ERR(page) != -ENODATA)
  206. return NULL;
  207. /* sparse block */
  208. if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
  209. (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
  210. (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
  211. /* maybe shared writable, allocate new block */
  212. page = mapping->a_ops->get_xip_page (mapping,
  213. pgoff*(PAGE_SIZE/512), 1);
  214. if (IS_ERR(page))
  215. return NULL;
  216. /* unmap page at pgoff from all other vmas */
  217. __xip_unmap(mapping, pgoff);
  218. } else {
  219. /* not shared and writable, use ZERO_PAGE() */
  220. page = ZERO_PAGE(address);
  221. }
  222. return page;
  223. }
  224. static struct vm_operations_struct xip_file_vm_ops = {
  225. .nopage = xip_file_nopage,
  226. };
  227. int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
  228. {
  229. BUG_ON(!file->f_mapping->a_ops->get_xip_page);
  230. file_accessed(file);
  231. vma->vm_ops = &xip_file_vm_ops;
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(xip_file_mmap);
  235. static ssize_t
  236. __xip_file_write(struct file *filp, const char __user *buf,
  237. size_t count, loff_t pos, loff_t *ppos)
  238. {
  239. struct address_space * mapping = filp->f_mapping;
  240. struct address_space_operations *a_ops = mapping->a_ops;
  241. struct inode *inode = mapping->host;
  242. long status = 0;
  243. struct page *page;
  244. size_t bytes;
  245. ssize_t written = 0;
  246. BUG_ON(!mapping->a_ops->get_xip_page);
  247. do {
  248. unsigned long index;
  249. unsigned long offset;
  250. size_t copied;
  251. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  252. index = pos >> PAGE_CACHE_SHIFT;
  253. bytes = PAGE_CACHE_SIZE - offset;
  254. if (bytes > count)
  255. bytes = count;
  256. /*
  257. * Bring in the user page that we will copy from _first_.
  258. * Otherwise there's a nasty deadlock on copying from the
  259. * same page as we're writing to, without it being marked
  260. * up-to-date.
  261. */
  262. fault_in_pages_readable(buf, bytes);
  263. page = a_ops->get_xip_page(mapping,
  264. index*(PAGE_SIZE/512), 0);
  265. if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
  266. /* we allocate a new page unmap it */
  267. page = a_ops->get_xip_page(mapping,
  268. index*(PAGE_SIZE/512), 1);
  269. if (!IS_ERR(page))
  270. /* unmap page at pgoff from all other vmas */
  271. __xip_unmap(mapping, index);
  272. }
  273. if (IS_ERR(page)) {
  274. status = PTR_ERR(page);
  275. break;
  276. }
  277. copied = filemap_copy_from_user(page, offset, buf, bytes);
  278. flush_dcache_page(page);
  279. if (likely(copied > 0)) {
  280. status = copied;
  281. if (status >= 0) {
  282. written += status;
  283. count -= status;
  284. pos += status;
  285. buf += status;
  286. }
  287. }
  288. if (unlikely(copied != bytes))
  289. if (status >= 0)
  290. status = -EFAULT;
  291. if (status < 0)
  292. break;
  293. } while (count);
  294. *ppos = pos;
  295. /*
  296. * No need to use i_size_read() here, the i_size
  297. * cannot change under us because we hold i_sem.
  298. */
  299. if (pos > inode->i_size) {
  300. i_size_write(inode, pos);
  301. mark_inode_dirty(inode);
  302. }
  303. return written ? written : status;
  304. }
  305. ssize_t
  306. xip_file_write(struct file *filp, const char __user *buf, size_t len,
  307. loff_t *ppos)
  308. {
  309. struct address_space *mapping = filp->f_mapping;
  310. struct inode *inode = mapping->host;
  311. size_t count;
  312. loff_t pos;
  313. ssize_t ret;
  314. down(&inode->i_sem);
  315. if (!access_ok(VERIFY_READ, buf, len)) {
  316. ret=-EFAULT;
  317. goto out_up;
  318. }
  319. pos = *ppos;
  320. count = len;
  321. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  322. /* We can write back this queue in page reclaim */
  323. current->backing_dev_info = mapping->backing_dev_info;
  324. ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
  325. if (ret)
  326. goto out_backing;
  327. if (count == 0)
  328. goto out_backing;
  329. ret = remove_suid(filp->f_dentry);
  330. if (ret)
  331. goto out_backing;
  332. inode_update_time(inode, 1);
  333. ret = __xip_file_write (filp, buf, count, pos, ppos);
  334. out_backing:
  335. current->backing_dev_info = NULL;
  336. out_up:
  337. up(&inode->i_sem);
  338. return ret;
  339. }
  340. EXPORT_SYMBOL_GPL(xip_file_write);
  341. /*
  342. * truncate a page used for execute in place
  343. * functionality is analog to block_truncate_page but does use get_xip_page
  344. * to get the page instead of page cache
  345. */
  346. int
  347. xip_truncate_page(struct address_space *mapping, loff_t from)
  348. {
  349. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  350. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  351. unsigned blocksize;
  352. unsigned length;
  353. struct page *page;
  354. void *kaddr;
  355. BUG_ON(!mapping->a_ops->get_xip_page);
  356. blocksize = 1 << mapping->host->i_blkbits;
  357. length = offset & (blocksize - 1);
  358. /* Block boundary? Nothing to do */
  359. if (!length)
  360. return 0;
  361. length = blocksize - length;
  362. page = mapping->a_ops->get_xip_page(mapping,
  363. index*(PAGE_SIZE/512), 0);
  364. if (!page)
  365. return -ENOMEM;
  366. if (unlikely(IS_ERR(page))) {
  367. if (PTR_ERR(page) == -ENODATA)
  368. /* Hole? No need to truncate */
  369. return 0;
  370. else
  371. return PTR_ERR(page);
  372. }
  373. kaddr = kmap_atomic(page, KM_USER0);
  374. memset(kaddr + offset, 0, length);
  375. kunmap_atomic(kaddr, KM_USER0);
  376. flush_dcache_page(page);
  377. return 0;
  378. }
  379. EXPORT_SYMBOL_GPL(xip_truncate_page);