filemap_xip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. spinlock_t *ptl;
  155. struct page *page;
  156. spin_lock(&mapping->i_mmap_lock);
  157. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  158. mm = vma->vm_mm;
  159. address = vma->vm_start +
  160. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  161. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  162. page = ZERO_PAGE(address);
  163. pte = page_check_address(page, mm, address, &ptl);
  164. if (pte) {
  165. /* Nuke the page table entry. */
  166. flush_cache_page(vma, address, pte_pfn(*pte));
  167. pteval = ptep_clear_flush(vma, address, pte);
  168. page_remove_rmap(page);
  169. dec_mm_counter(mm, file_rss);
  170. BUG_ON(pte_dirty(pteval));
  171. pte_unmap_unlock(pte, ptl);
  172. page_cache_release(page);
  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. goto out;
  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. out:
  223. page_cache_get(page);
  224. return page;
  225. }
  226. static struct vm_operations_struct xip_file_vm_ops = {
  227. .nopage = xip_file_nopage,
  228. };
  229. int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
  230. {
  231. BUG_ON(!file->f_mapping->a_ops->get_xip_page);
  232. file_accessed(file);
  233. vma->vm_ops = &xip_file_vm_ops;
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(xip_file_mmap);
  237. static ssize_t
  238. __xip_file_write(struct file *filp, const char __user *buf,
  239. size_t count, loff_t pos, loff_t *ppos)
  240. {
  241. struct address_space * mapping = filp->f_mapping;
  242. const struct address_space_operations *a_ops = mapping->a_ops;
  243. struct inode *inode = mapping->host;
  244. long status = 0;
  245. struct page *page;
  246. size_t bytes;
  247. ssize_t written = 0;
  248. BUG_ON(!mapping->a_ops->get_xip_page);
  249. do {
  250. unsigned long index;
  251. unsigned long offset;
  252. size_t copied;
  253. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  254. index = pos >> PAGE_CACHE_SHIFT;
  255. bytes = PAGE_CACHE_SIZE - offset;
  256. if (bytes > count)
  257. bytes = count;
  258. /*
  259. * Bring in the user page that we will copy from _first_.
  260. * Otherwise there's a nasty deadlock on copying from the
  261. * same page as we're writing to, without it being marked
  262. * up-to-date.
  263. */
  264. fault_in_pages_readable(buf, bytes);
  265. page = a_ops->get_xip_page(mapping,
  266. index*(PAGE_SIZE/512), 0);
  267. if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
  268. /* we allocate a new page unmap it */
  269. page = a_ops->get_xip_page(mapping,
  270. index*(PAGE_SIZE/512), 1);
  271. if (!IS_ERR(page))
  272. /* unmap page at pgoff from all other vmas */
  273. __xip_unmap(mapping, index);
  274. }
  275. if (IS_ERR(page)) {
  276. status = PTR_ERR(page);
  277. break;
  278. }
  279. copied = filemap_copy_from_user(page, offset, buf, bytes);
  280. flush_dcache_page(page);
  281. if (likely(copied > 0)) {
  282. status = copied;
  283. if (status >= 0) {
  284. written += status;
  285. count -= status;
  286. pos += status;
  287. buf += status;
  288. }
  289. }
  290. if (unlikely(copied != bytes))
  291. if (status >= 0)
  292. status = -EFAULT;
  293. if (status < 0)
  294. break;
  295. } while (count);
  296. *ppos = pos;
  297. /*
  298. * No need to use i_size_read() here, the i_size
  299. * cannot change under us because we hold i_mutex.
  300. */
  301. if (pos > inode->i_size) {
  302. i_size_write(inode, pos);
  303. mark_inode_dirty(inode);
  304. }
  305. return written ? written : status;
  306. }
  307. ssize_t
  308. xip_file_write(struct file *filp, const char __user *buf, size_t len,
  309. loff_t *ppos)
  310. {
  311. struct address_space *mapping = filp->f_mapping;
  312. struct inode *inode = mapping->host;
  313. size_t count;
  314. loff_t pos;
  315. ssize_t ret;
  316. mutex_lock(&inode->i_mutex);
  317. if (!access_ok(VERIFY_READ, buf, len)) {
  318. ret=-EFAULT;
  319. goto out_up;
  320. }
  321. pos = *ppos;
  322. count = len;
  323. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  324. /* We can write back this queue in page reclaim */
  325. current->backing_dev_info = mapping->backing_dev_info;
  326. ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
  327. if (ret)
  328. goto out_backing;
  329. if (count == 0)
  330. goto out_backing;
  331. ret = remove_suid(filp->f_dentry);
  332. if (ret)
  333. goto out_backing;
  334. file_update_time(filp);
  335. ret = __xip_file_write (filp, buf, count, pos, ppos);
  336. out_backing:
  337. current->backing_dev_info = NULL;
  338. out_up:
  339. mutex_unlock(&inode->i_mutex);
  340. return ret;
  341. }
  342. EXPORT_SYMBOL_GPL(xip_file_write);
  343. /*
  344. * truncate a page used for execute in place
  345. * functionality is analog to block_truncate_page but does use get_xip_page
  346. * to get the page instead of page cache
  347. */
  348. int
  349. xip_truncate_page(struct address_space *mapping, loff_t from)
  350. {
  351. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  352. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  353. unsigned blocksize;
  354. unsigned length;
  355. struct page *page;
  356. void *kaddr;
  357. BUG_ON(!mapping->a_ops->get_xip_page);
  358. blocksize = 1 << mapping->host->i_blkbits;
  359. length = offset & (blocksize - 1);
  360. /* Block boundary? Nothing to do */
  361. if (!length)
  362. return 0;
  363. length = blocksize - length;
  364. page = mapping->a_ops->get_xip_page(mapping,
  365. index*(PAGE_SIZE/512), 0);
  366. if (!page)
  367. return -ENOMEM;
  368. if (unlikely(IS_ERR(page))) {
  369. if (PTR_ERR(page) == -ENODATA)
  370. /* Hole? No need to truncate */
  371. return 0;
  372. else
  373. return PTR_ERR(page);
  374. }
  375. kaddr = kmap_atomic(page, KM_USER0);
  376. memset(kaddr + offset, 0, length);
  377. kunmap_atomic(kaddr, KM_USER0);
  378. flush_dcache_page(page);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL_GPL(xip_truncate_page);