file-nommu.c 8.0 KB

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