file-nommu.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* file-nommu.c: no-MMU version of ramfs
  2. *
  3. * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/highmem.h>
  16. #include <linux/init.h>
  17. #include <linux/string.h>
  18. #include <linux/backing-dev.h>
  19. #include <linux/ramfs.h>
  20. #include <linux/pagevec.h>
  21. #include <linux/mman.h>
  22. #include <asm/uaccess.h>
  23. #include "internal.h"
  24. static int ramfs_nommu_setattr(struct dentry *, struct iattr *);
  25. const struct address_space_operations ramfs_aops = {
  26. .readpage = simple_readpage,
  27. .write_begin = simple_write_begin,
  28. .write_end = simple_write_end,
  29. .set_page_dirty = __set_page_dirty_no_writeback,
  30. };
  31. const struct file_operations ramfs_file_operations = {
  32. .mmap = ramfs_nommu_mmap,
  33. .get_unmapped_area = ramfs_nommu_get_unmapped_area,
  34. .read = do_sync_read,
  35. .aio_read = generic_file_aio_read,
  36. .write = do_sync_write,
  37. .aio_write = generic_file_aio_write,
  38. .fsync = simple_sync_file,
  39. .splice_read = generic_file_splice_read,
  40. .splice_write = generic_file_splice_write,
  41. .llseek = generic_file_llseek,
  42. };
  43. const struct inode_operations ramfs_file_inode_operations = {
  44. .setattr = ramfs_nommu_setattr,
  45. .getattr = simple_getattr,
  46. };
  47. /*****************************************************************************/
  48. /*
  49. * add a contiguous set of pages into a ramfs inode when it's truncated from
  50. * size 0 on the assumption that it's going to be used for an mmap of shared
  51. * memory
  52. */
  53. int ramfs_nommu_expand_for_mapping(struct inode *inode, size_t newsize)
  54. {
  55. struct pagevec lru_pvec;
  56. unsigned long npages, xpages, loop, limit;
  57. struct page *pages;
  58. unsigned order;
  59. void *data;
  60. int ret;
  61. /* make various checks */
  62. order = get_order(newsize);
  63. if (unlikely(order >= MAX_ORDER))
  64. goto too_big;
  65. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  66. if (limit != RLIM_INFINITY && newsize > limit)
  67. goto fsize_exceeded;
  68. if (newsize > inode->i_sb->s_maxbytes)
  69. goto too_big;
  70. i_size_write(inode, newsize);
  71. /* allocate enough contiguous pages to be able to satisfy the
  72. * request */
  73. pages = alloc_pages(mapping_gfp_mask(inode->i_mapping), order);
  74. if (!pages)
  75. return -ENOMEM;
  76. /* split the high-order page into an array of single pages */
  77. xpages = 1UL << order;
  78. npages = (newsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  79. split_page(pages, order);
  80. /* trim off any pages we don't actually require */
  81. for (loop = npages; loop < xpages; loop++)
  82. __free_page(pages + loop);
  83. /* clear the memory we allocated */
  84. newsize = PAGE_SIZE * npages;
  85. data = page_address(pages);
  86. memset(data, 0, newsize);
  87. /* attach all the pages to the inode's address space */
  88. pagevec_init(&lru_pvec, 0);
  89. for (loop = 0; loop < npages; loop++) {
  90. struct page *page = pages + loop;
  91. ret = add_to_page_cache(page, inode->i_mapping, loop, GFP_KERNEL);
  92. if (ret < 0)
  93. goto add_error;
  94. if (!pagevec_add(&lru_pvec, page))
  95. __pagevec_lru_add_file(&lru_pvec);
  96. /* prevent the page from being discarded on memory pressure */
  97. SetPageDirty(page);
  98. unlock_page(page);
  99. }
  100. pagevec_lru_add_file(&lru_pvec);
  101. return 0;
  102. fsize_exceeded:
  103. send_sig(SIGXFSZ, current, 0);
  104. too_big:
  105. return -EFBIG;
  106. add_error:
  107. pagevec_lru_add_file(&lru_pvec);
  108. page_cache_release(pages + loop);
  109. for (loop++; loop < npages; loop++)
  110. __free_page(pages + loop);
  111. return ret;
  112. }
  113. /*****************************************************************************/
  114. /*
  115. * check that file shrinkage doesn't leave any VMAs dangling in midair
  116. */
  117. static int ramfs_nommu_check_mappings(struct inode *inode,
  118. size_t newsize, size_t size)
  119. {
  120. struct vm_area_struct *vma;
  121. struct prio_tree_iter iter;
  122. /* search for VMAs that fall within the dead zone */
  123. vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
  124. newsize >> PAGE_SHIFT,
  125. (size + PAGE_SIZE - 1) >> PAGE_SHIFT
  126. ) {
  127. /* found one - only interested if it's shared out of the page
  128. * cache */
  129. if (vma->vm_flags & VM_SHARED)
  130. return -ETXTBSY; /* not quite true, but near enough */
  131. }
  132. return 0;
  133. }
  134. /*****************************************************************************/
  135. /*
  136. *
  137. */
  138. static int ramfs_nommu_resize(struct inode *inode, loff_t newsize, loff_t size)
  139. {
  140. int ret;
  141. /* assume a truncate from zero size is going to be for the purposes of
  142. * shared mmap */
  143. if (size == 0) {
  144. if (unlikely(newsize >> 32))
  145. return -EFBIG;
  146. return ramfs_nommu_expand_for_mapping(inode, newsize);
  147. }
  148. /* check that a decrease in size doesn't cut off any shared mappings */
  149. if (newsize < size) {
  150. ret = ramfs_nommu_check_mappings(inode, newsize, size);
  151. if (ret < 0)
  152. return ret;
  153. }
  154. ret = vmtruncate(inode, newsize);
  155. return ret;
  156. }
  157. /*****************************************************************************/
  158. /*
  159. * handle a change of attributes
  160. * - we're specifically interested in a change of size
  161. */
  162. static int ramfs_nommu_setattr(struct dentry *dentry, struct iattr *ia)
  163. {
  164. struct inode *inode = dentry->d_inode;
  165. unsigned int old_ia_valid = ia->ia_valid;
  166. int ret = 0;
  167. /* POSIX UID/GID verification for setting inode attributes */
  168. ret = inode_change_ok(inode, ia);
  169. if (ret)
  170. return ret;
  171. /* pick out size-changing events */
  172. if (ia->ia_valid & ATTR_SIZE) {
  173. loff_t size = i_size_read(inode);
  174. if (ia->ia_size != size) {
  175. ret = ramfs_nommu_resize(inode, ia->ia_size, size);
  176. if (ret < 0 || ia->ia_valid == ATTR_SIZE)
  177. goto out;
  178. } else {
  179. /* we skipped the truncate but must still update
  180. * timestamps
  181. */
  182. ia->ia_valid |= ATTR_MTIME|ATTR_CTIME;
  183. }
  184. }
  185. ret = inode_setattr(inode, ia);
  186. out:
  187. ia->ia_valid = old_ia_valid;
  188. return ret;
  189. }
  190. /*****************************************************************************/
  191. /*
  192. * try to determine where a shared mapping can be made
  193. * - we require that:
  194. * - the pages to be mapped must exist
  195. * - the pages be physically contiguous in sequence
  196. */
  197. unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
  198. unsigned long addr, unsigned long len,
  199. unsigned long pgoff, unsigned long flags)
  200. {
  201. unsigned long maxpages, lpages, nr, loop, ret;
  202. struct inode *inode = file->f_path.dentry->d_inode;
  203. struct page **pages = NULL, **ptr, *page;
  204. loff_t isize;
  205. if (!(flags & MAP_SHARED))
  206. return addr;
  207. /* the mapping mustn't extend beyond the EOF */
  208. lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  209. isize = i_size_read(inode);
  210. ret = -EINVAL;
  211. maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  212. if (pgoff >= maxpages)
  213. goto out;
  214. if (maxpages - pgoff < lpages)
  215. goto out;
  216. /* gang-find the pages */
  217. ret = -ENOMEM;
  218. pages = kzalloc(lpages * sizeof(struct page *), GFP_KERNEL);
  219. if (!pages)
  220. goto out_free;
  221. nr = find_get_pages(inode->i_mapping, pgoff, lpages, pages);
  222. if (nr != lpages)
  223. goto out_free_pages; /* leave if some pages were missing */
  224. /* check the pages for physical adjacency */
  225. ptr = pages;
  226. page = *ptr++;
  227. page++;
  228. for (loop = lpages; loop > 1; loop--)
  229. if (*ptr++ != page++)
  230. goto out_free_pages;
  231. /* okay - all conditions fulfilled */
  232. ret = (unsigned long) page_address(pages[0]);
  233. out_free_pages:
  234. ptr = pages;
  235. for (loop = nr; loop > 0; loop--)
  236. put_page(*ptr++);
  237. out_free:
  238. kfree(pages);
  239. out:
  240. return ret;
  241. }
  242. /*****************************************************************************/
  243. /*
  244. * set up a mapping for shared memory segments
  245. */
  246. int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma)
  247. {
  248. if (!(vma->vm_flags & VM_SHARED))
  249. return -ENOSYS;
  250. file_accessed(file);
  251. vma->vm_ops = &generic_file_vm_ops;
  252. return 0;
  253. }