filemap_xip.c 10 KB

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