filemap_xip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. struct page *page;
  155. spin_lock(&mapping->i_mmap_lock);
  156. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  157. mm = vma->vm_mm;
  158. address = vma->vm_start +
  159. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  160. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  161. page = ZERO_PAGE(address);
  162. /*
  163. * We need the page_table_lock to protect us from page faults,
  164. * munmap, fork, etc...
  165. */
  166. pte = page_check_address(page, mm, address);
  167. if (!IS_ERR(pte)) {
  168. /* Nuke the page table entry. */
  169. flush_cache_page(vma, address, pte_pfn(*pte));
  170. pteval = ptep_clear_flush(vma, address, pte);
  171. page_remove_rmap(page);
  172. dec_mm_counter(mm, file_rss);
  173. BUG_ON(pte_dirty(pteval));
  174. pte_unmap(pte);
  175. spin_unlock(&mm->page_table_lock);
  176. page_cache_release(page);
  177. }
  178. }
  179. spin_unlock(&mapping->i_mmap_lock);
  180. }
  181. /*
  182. * xip_nopage() is invoked via the vma operations vector for a
  183. * mapped memory region to read in file data during a page fault.
  184. *
  185. * This function is derived from filemap_nopage, but used for execute in place
  186. */
  187. static struct page *
  188. xip_file_nopage(struct vm_area_struct * area,
  189. unsigned long address,
  190. int *type)
  191. {
  192. struct file *file = area->vm_file;
  193. struct address_space *mapping = file->f_mapping;
  194. struct inode *inode = mapping->host;
  195. struct page *page;
  196. unsigned long size, pgoff, endoff;
  197. pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT)
  198. + area->vm_pgoff;
  199. endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT)
  200. + area->vm_pgoff;
  201. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  202. if (pgoff >= size) {
  203. return NULL;
  204. }
  205. page = mapping->a_ops->get_xip_page(mapping, pgoff*(PAGE_SIZE/512), 0);
  206. if (!IS_ERR(page)) {
  207. goto out;
  208. }
  209. if (PTR_ERR(page) != -ENODATA)
  210. return NULL;
  211. /* sparse block */
  212. if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
  213. (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
  214. (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
  215. /* maybe shared writable, allocate new block */
  216. page = mapping->a_ops->get_xip_page (mapping,
  217. pgoff*(PAGE_SIZE/512), 1);
  218. if (IS_ERR(page))
  219. return NULL;
  220. /* unmap page at pgoff from all other vmas */
  221. __xip_unmap(mapping, pgoff);
  222. } else {
  223. /* not shared and writable, use ZERO_PAGE() */
  224. page = ZERO_PAGE(address);
  225. }
  226. out:
  227. page_cache_get(page);
  228. return page;
  229. }
  230. static struct vm_operations_struct xip_file_vm_ops = {
  231. .nopage = xip_file_nopage,
  232. };
  233. int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
  234. {
  235. BUG_ON(!file->f_mapping->a_ops->get_xip_page);
  236. file_accessed(file);
  237. vma->vm_ops = &xip_file_vm_ops;
  238. return 0;
  239. }
  240. EXPORT_SYMBOL_GPL(xip_file_mmap);
  241. static ssize_t
  242. __xip_file_write(struct file *filp, const char __user *buf,
  243. size_t count, loff_t pos, loff_t *ppos)
  244. {
  245. struct address_space * mapping = filp->f_mapping;
  246. struct address_space_operations *a_ops = mapping->a_ops;
  247. struct inode *inode = mapping->host;
  248. long status = 0;
  249. struct page *page;
  250. size_t bytes;
  251. ssize_t written = 0;
  252. BUG_ON(!mapping->a_ops->get_xip_page);
  253. do {
  254. unsigned long index;
  255. unsigned long offset;
  256. size_t copied;
  257. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  258. index = pos >> PAGE_CACHE_SHIFT;
  259. bytes = PAGE_CACHE_SIZE - offset;
  260. if (bytes > count)
  261. bytes = count;
  262. /*
  263. * Bring in the user page that we will copy from _first_.
  264. * Otherwise there's a nasty deadlock on copying from the
  265. * same page as we're writing to, without it being marked
  266. * up-to-date.
  267. */
  268. fault_in_pages_readable(buf, bytes);
  269. page = a_ops->get_xip_page(mapping,
  270. index*(PAGE_SIZE/512), 0);
  271. if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
  272. /* we allocate a new page unmap it */
  273. page = a_ops->get_xip_page(mapping,
  274. index*(PAGE_SIZE/512), 1);
  275. if (!IS_ERR(page))
  276. /* unmap page at pgoff from all other vmas */
  277. __xip_unmap(mapping, index);
  278. }
  279. if (IS_ERR(page)) {
  280. status = PTR_ERR(page);
  281. break;
  282. }
  283. copied = filemap_copy_from_user(page, offset, buf, bytes);
  284. flush_dcache_page(page);
  285. if (likely(copied > 0)) {
  286. status = copied;
  287. if (status >= 0) {
  288. written += status;
  289. count -= status;
  290. pos += status;
  291. buf += status;
  292. }
  293. }
  294. if (unlikely(copied != bytes))
  295. if (status >= 0)
  296. status = -EFAULT;
  297. if (status < 0)
  298. break;
  299. } while (count);
  300. *ppos = pos;
  301. /*
  302. * No need to use i_size_read() here, the i_size
  303. * cannot change under us because we hold i_sem.
  304. */
  305. if (pos > inode->i_size) {
  306. i_size_write(inode, pos);
  307. mark_inode_dirty(inode);
  308. }
  309. return written ? written : status;
  310. }
  311. ssize_t
  312. xip_file_write(struct file *filp, const char __user *buf, size_t len,
  313. loff_t *ppos)
  314. {
  315. struct address_space *mapping = filp->f_mapping;
  316. struct inode *inode = mapping->host;
  317. size_t count;
  318. loff_t pos;
  319. ssize_t ret;
  320. down(&inode->i_sem);
  321. if (!access_ok(VERIFY_READ, buf, len)) {
  322. ret=-EFAULT;
  323. goto out_up;
  324. }
  325. pos = *ppos;
  326. count = len;
  327. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  328. /* We can write back this queue in page reclaim */
  329. current->backing_dev_info = mapping->backing_dev_info;
  330. ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
  331. if (ret)
  332. goto out_backing;
  333. if (count == 0)
  334. goto out_backing;
  335. ret = remove_suid(filp->f_dentry);
  336. if (ret)
  337. goto out_backing;
  338. inode_update_time(inode, 1);
  339. ret = __xip_file_write (filp, buf, count, pos, ppos);
  340. out_backing:
  341. current->backing_dev_info = NULL;
  342. out_up:
  343. up(&inode->i_sem);
  344. return ret;
  345. }
  346. EXPORT_SYMBOL_GPL(xip_file_write);
  347. /*
  348. * truncate a page used for execute in place
  349. * functionality is analog to block_truncate_page but does use get_xip_page
  350. * to get the page instead of page cache
  351. */
  352. int
  353. xip_truncate_page(struct address_space *mapping, loff_t from)
  354. {
  355. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  356. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  357. unsigned blocksize;
  358. unsigned length;
  359. struct page *page;
  360. void *kaddr;
  361. BUG_ON(!mapping->a_ops->get_xip_page);
  362. blocksize = 1 << mapping->host->i_blkbits;
  363. length = offset & (blocksize - 1);
  364. /* Block boundary? Nothing to do */
  365. if (!length)
  366. return 0;
  367. length = blocksize - length;
  368. page = mapping->a_ops->get_xip_page(mapping,
  369. index*(PAGE_SIZE/512), 0);
  370. if (!page)
  371. return -ENOMEM;
  372. if (unlikely(IS_ERR(page))) {
  373. if (PTR_ERR(page) == -ENODATA)
  374. /* Hole? No need to truncate */
  375. return 0;
  376. else
  377. return PTR_ERR(page);
  378. }
  379. kaddr = kmap_atomic(page, KM_USER0);
  380. memset(kaddr + offset, 0, length);
  381. kunmap_atomic(kaddr, KM_USER0);
  382. flush_dcache_page(page);
  383. return 0;
  384. }
  385. EXPORT_SYMBOL_GPL(xip_truncate_page);