file-nommu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <asm/uaccess.h>
  25. #include "internal.h"
  26. static int ramfs_nommu_setattr(struct dentry *, struct iattr *);
  27. const struct address_space_operations ramfs_aops = {
  28. .readpage = simple_readpage,
  29. .write_begin = simple_write_begin,
  30. .write_end = simple_write_end,
  31. .set_page_dirty = __set_page_dirty_no_writeback,
  32. };
  33. const struct file_operations ramfs_file_operations = {
  34. .mmap = ramfs_nommu_mmap,
  35. .get_unmapped_area = ramfs_nommu_get_unmapped_area,
  36. .read = do_sync_read,
  37. .aio_read = generic_file_aio_read,
  38. .write = do_sync_write,
  39. .aio_write = generic_file_aio_write,
  40. .fsync = noop_fsync,
  41. .splice_read = generic_file_splice_read,
  42. .splice_write = generic_file_splice_write,
  43. .llseek = generic_file_llseek,
  44. };
  45. const struct inode_operations ramfs_file_inode_operations = {
  46. .setattr = ramfs_nommu_setattr,
  47. .getattr = simple_getattr,
  48. };
  49. /*****************************************************************************/
  50. /*
  51. * add a contiguous set of pages into a ramfs inode when it's truncated from
  52. * size 0 on the assumption that it's going to be used for an mmap of shared
  53. * memory
  54. */
  55. int ramfs_nommu_expand_for_mapping(struct inode *inode, size_t newsize)
  56. {
  57. unsigned long npages, xpages, loop;
  58. struct page *pages;
  59. unsigned order;
  60. void *data;
  61. int ret;
  62. /* make various checks */
  63. order = get_order(newsize);
  64. if (unlikely(order >= MAX_ORDER))
  65. return -EFBIG;
  66. ret = inode_newsize_ok(inode, newsize);
  67. if (ret)
  68. return ret;
  69. i_size_write(inode, newsize);
  70. /* allocate enough contiguous pages to be able to satisfy the
  71. * request */
  72. pages = alloc_pages(mapping_gfp_mask(inode->i_mapping), order);
  73. if (!pages)
  74. return -ENOMEM;
  75. /* split the high-order page into an array of single pages */
  76. xpages = 1UL << order;
  77. npages = (newsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  78. split_page(pages, order);
  79. /* trim off any pages we don't actually require */
  80. for (loop = npages; loop < xpages; loop++)
  81. __free_page(pages + loop);
  82. /* clear the memory we allocated */
  83. newsize = PAGE_SIZE * npages;
  84. data = page_address(pages);
  85. memset(data, 0, newsize);
  86. /* attach all the pages to the inode's address space */
  87. for (loop = 0; loop < npages; loop++) {
  88. struct page *page = pages + loop;
  89. ret = add_to_page_cache_lru(page, inode->i_mapping, loop,
  90. GFP_KERNEL);
  91. if (ret < 0)
  92. goto add_error;
  93. /* prevent the page from being discarded on memory pressure */
  94. SetPageDirty(page);
  95. unlock_page(page);
  96. put_page(page);
  97. }
  98. return 0;
  99. add_error:
  100. while (loop < npages)
  101. __free_page(pages + loop++);
  102. return ret;
  103. }
  104. /*****************************************************************************/
  105. /*
  106. *
  107. */
  108. static int ramfs_nommu_resize(struct inode *inode, loff_t newsize, loff_t size)
  109. {
  110. int ret;
  111. /* assume a truncate from zero size is going to be for the purposes of
  112. * shared mmap */
  113. if (size == 0) {
  114. if (unlikely(newsize >> 32))
  115. return -EFBIG;
  116. return ramfs_nommu_expand_for_mapping(inode, newsize);
  117. }
  118. /* check that a decrease in size doesn't cut off any shared mappings */
  119. if (newsize < size) {
  120. ret = nommu_shrink_inode_mappings(inode, size, newsize);
  121. if (ret < 0)
  122. return ret;
  123. }
  124. truncate_setsize(inode, newsize);
  125. return 0;
  126. }
  127. /*****************************************************************************/
  128. /*
  129. * handle a change of attributes
  130. * - we're specifically interested in a change of size
  131. */
  132. static int ramfs_nommu_setattr(struct dentry *dentry, struct iattr *ia)
  133. {
  134. struct inode *inode = dentry->d_inode;
  135. unsigned int old_ia_valid = ia->ia_valid;
  136. int ret = 0;
  137. /* POSIX UID/GID verification for setting inode attributes */
  138. ret = inode_change_ok(inode, ia);
  139. if (ret)
  140. return ret;
  141. /* pick out size-changing events */
  142. if (ia->ia_valid & ATTR_SIZE) {
  143. loff_t size = inode->i_size;
  144. if (ia->ia_size != size) {
  145. ret = ramfs_nommu_resize(inode, ia->ia_size, size);
  146. if (ret < 0 || ia->ia_valid == ATTR_SIZE)
  147. goto out;
  148. } else {
  149. /* we skipped the truncate but must still update
  150. * timestamps
  151. */
  152. ia->ia_valid |= ATTR_MTIME|ATTR_CTIME;
  153. }
  154. }
  155. setattr_copy(inode, ia);
  156. out:
  157. ia->ia_valid = old_ia_valid;
  158. return ret;
  159. }
  160. /*****************************************************************************/
  161. /*
  162. * try to determine where a shared mapping can be made
  163. * - we require that:
  164. * - the pages to be mapped must exist
  165. * - the pages be physically contiguous in sequence
  166. */
  167. unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
  168. unsigned long addr, unsigned long len,
  169. unsigned long pgoff, unsigned long flags)
  170. {
  171. unsigned long maxpages, lpages, nr, loop, ret;
  172. struct inode *inode = file->f_path.dentry->d_inode;
  173. struct page **pages = NULL, **ptr, *page;
  174. loff_t isize;
  175. if (!(flags & MAP_SHARED))
  176. return addr;
  177. /* the mapping mustn't extend beyond the EOF */
  178. lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  179. isize = i_size_read(inode);
  180. ret = -EINVAL;
  181. maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  182. if (pgoff >= maxpages)
  183. goto out;
  184. if (maxpages - pgoff < lpages)
  185. goto out;
  186. /* gang-find the pages */
  187. ret = -ENOMEM;
  188. pages = kzalloc(lpages * sizeof(struct page *), GFP_KERNEL);
  189. if (!pages)
  190. goto out_free;
  191. nr = find_get_pages(inode->i_mapping, pgoff, lpages, pages);
  192. if (nr != lpages)
  193. goto out_free_pages; /* leave if some pages were missing */
  194. /* check the pages for physical adjacency */
  195. ptr = pages;
  196. page = *ptr++;
  197. page++;
  198. for (loop = lpages; loop > 1; loop--)
  199. if (*ptr++ != page++)
  200. goto out_free_pages;
  201. /* okay - all conditions fulfilled */
  202. ret = (unsigned long) page_address(pages[0]);
  203. out_free_pages:
  204. ptr = pages;
  205. for (loop = nr; loop > 0; loop--)
  206. put_page(*ptr++);
  207. out_free:
  208. kfree(pages);
  209. out:
  210. return ret;
  211. }
  212. /*****************************************************************************/
  213. /*
  214. * set up a mapping for shared memory segments
  215. */
  216. int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma)
  217. {
  218. if (!(vma->vm_flags & VM_SHARED))
  219. return -ENOSYS;
  220. file_accessed(file);
  221. vma->vm_ops = &generic_file_vm_ops;
  222. return 0;
  223. }