file-nommu.c 7.4 KB

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