shmem.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/file.h>
  29. #include <linux/mm.h>
  30. #include <linux/module.h>
  31. #include <linux/swap.h>
  32. static struct vfsmount *shm_mnt;
  33. #ifdef CONFIG_SHMEM
  34. /*
  35. * This virtual memory filesystem is heavily based on the ramfs. It
  36. * extends ramfs by the ability to use swap and honor resource limits
  37. * which makes it a completely usable filesystem.
  38. */
  39. #include <linux/xattr.h>
  40. #include <linux/exportfs.h>
  41. #include <linux/posix_acl.h>
  42. #include <linux/generic_acl.h>
  43. #include <linux/mman.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/backing-dev.h>
  47. #include <linux/shmem_fs.h>
  48. #include <linux/writeback.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/pagevec.h>
  51. #include <linux/percpu_counter.h>
  52. #include <linux/splice.h>
  53. #include <linux/security.h>
  54. #include <linux/swapops.h>
  55. #include <linux/mempolicy.h>
  56. #include <linux/namei.h>
  57. #include <linux/ctype.h>
  58. #include <linux/migrate.h>
  59. #include <linux/highmem.h>
  60. #include <linux/seq_file.h>
  61. #include <linux/magic.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/pgtable.h>
  64. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  65. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  66. /* Pretend that each entry is of this size in directory's i_size */
  67. #define BOGO_DIRENT_SIZE 20
  68. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  69. #define SHORT_SYMLINK_LEN 128
  70. struct shmem_xattr {
  71. struct list_head list; /* anchored by shmem_inode_info->xattr_list */
  72. char *name; /* xattr name */
  73. size_t size;
  74. char value[0];
  75. };
  76. /* Flag allocation requirements to shmem_getpage */
  77. enum sgp_type {
  78. SGP_READ, /* don't exceed i_size, don't allocate page */
  79. SGP_CACHE, /* don't exceed i_size, may allocate page */
  80. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  81. SGP_WRITE, /* may exceed i_size, may allocate page */
  82. };
  83. #ifdef CONFIG_TMPFS
  84. static unsigned long shmem_default_max_blocks(void)
  85. {
  86. return totalram_pages / 2;
  87. }
  88. static unsigned long shmem_default_max_inodes(void)
  89. {
  90. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  91. }
  92. #endif
  93. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  94. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  95. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  96. struct page **pagep, enum sgp_type sgp, int *fault_type)
  97. {
  98. return shmem_getpage_gfp(inode, index, pagep, sgp,
  99. mapping_gfp_mask(inode->i_mapping), fault_type);
  100. }
  101. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  102. {
  103. return sb->s_fs_info;
  104. }
  105. /*
  106. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  107. * for shared memory and for shared anonymous (/dev/zero) mappings
  108. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  109. * consistent with the pre-accounting of private mappings ...
  110. */
  111. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  112. {
  113. return (flags & VM_NORESERVE) ?
  114. 0 : security_vm_enough_memory_kern(VM_ACCT(size));
  115. }
  116. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  117. {
  118. if (!(flags & VM_NORESERVE))
  119. vm_unacct_memory(VM_ACCT(size));
  120. }
  121. /*
  122. * ... whereas tmpfs objects are accounted incrementally as
  123. * pages are allocated, in order to allow huge sparse files.
  124. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  125. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  126. */
  127. static inline int shmem_acct_block(unsigned long flags)
  128. {
  129. return (flags & VM_NORESERVE) ?
  130. security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  131. }
  132. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  133. {
  134. if (flags & VM_NORESERVE)
  135. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  136. }
  137. static const struct super_operations shmem_ops;
  138. static const struct address_space_operations shmem_aops;
  139. static const struct file_operations shmem_file_operations;
  140. static const struct inode_operations shmem_inode_operations;
  141. static const struct inode_operations shmem_dir_inode_operations;
  142. static const struct inode_operations shmem_special_inode_operations;
  143. static const struct vm_operations_struct shmem_vm_ops;
  144. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  145. .ra_pages = 0, /* No readahead */
  146. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  147. };
  148. static LIST_HEAD(shmem_swaplist);
  149. static DEFINE_MUTEX(shmem_swaplist_mutex);
  150. static int shmem_reserve_inode(struct super_block *sb)
  151. {
  152. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  153. if (sbinfo->max_inodes) {
  154. spin_lock(&sbinfo->stat_lock);
  155. if (!sbinfo->free_inodes) {
  156. spin_unlock(&sbinfo->stat_lock);
  157. return -ENOSPC;
  158. }
  159. sbinfo->free_inodes--;
  160. spin_unlock(&sbinfo->stat_lock);
  161. }
  162. return 0;
  163. }
  164. static void shmem_free_inode(struct super_block *sb)
  165. {
  166. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  167. if (sbinfo->max_inodes) {
  168. spin_lock(&sbinfo->stat_lock);
  169. sbinfo->free_inodes++;
  170. spin_unlock(&sbinfo->stat_lock);
  171. }
  172. }
  173. /**
  174. * shmem_recalc_inode - recalculate the block usage of an inode
  175. * @inode: inode to recalc
  176. *
  177. * We have to calculate the free blocks since the mm can drop
  178. * undirtied hole pages behind our back.
  179. *
  180. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  181. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  182. *
  183. * It has to be called with the spinlock held.
  184. */
  185. static void shmem_recalc_inode(struct inode *inode)
  186. {
  187. struct shmem_inode_info *info = SHMEM_I(inode);
  188. long freed;
  189. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  190. if (freed > 0) {
  191. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  192. if (sbinfo->max_blocks)
  193. percpu_counter_add(&sbinfo->used_blocks, -freed);
  194. info->alloced -= freed;
  195. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  196. shmem_unacct_blocks(info->flags, freed);
  197. }
  198. }
  199. /*
  200. * Replace item expected in radix tree by a new item, while holding tree lock.
  201. */
  202. static int shmem_radix_tree_replace(struct address_space *mapping,
  203. pgoff_t index, void *expected, void *replacement)
  204. {
  205. void **pslot;
  206. void *item = NULL;
  207. VM_BUG_ON(!expected);
  208. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  209. if (pslot)
  210. item = radix_tree_deref_slot_protected(pslot,
  211. &mapping->tree_lock);
  212. if (item != expected)
  213. return -ENOENT;
  214. if (replacement)
  215. radix_tree_replace_slot(pslot, replacement);
  216. else
  217. radix_tree_delete(&mapping->page_tree, index);
  218. return 0;
  219. }
  220. /*
  221. * Like add_to_page_cache_locked, but error if expected item has gone.
  222. */
  223. static int shmem_add_to_page_cache(struct page *page,
  224. struct address_space *mapping,
  225. pgoff_t index, gfp_t gfp, void *expected)
  226. {
  227. int error = 0;
  228. VM_BUG_ON(!PageLocked(page));
  229. VM_BUG_ON(!PageSwapBacked(page));
  230. if (!expected)
  231. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  232. if (!error) {
  233. page_cache_get(page);
  234. page->mapping = mapping;
  235. page->index = index;
  236. spin_lock_irq(&mapping->tree_lock);
  237. if (!expected)
  238. error = radix_tree_insert(&mapping->page_tree,
  239. index, page);
  240. else
  241. error = shmem_radix_tree_replace(mapping, index,
  242. expected, page);
  243. if (!error) {
  244. mapping->nrpages++;
  245. __inc_zone_page_state(page, NR_FILE_PAGES);
  246. __inc_zone_page_state(page, NR_SHMEM);
  247. spin_unlock_irq(&mapping->tree_lock);
  248. } else {
  249. page->mapping = NULL;
  250. spin_unlock_irq(&mapping->tree_lock);
  251. page_cache_release(page);
  252. }
  253. if (!expected)
  254. radix_tree_preload_end();
  255. }
  256. if (error)
  257. mem_cgroup_uncharge_cache_page(page);
  258. return error;
  259. }
  260. /*
  261. * Like delete_from_page_cache, but substitutes swap for page.
  262. */
  263. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  264. {
  265. struct address_space *mapping = page->mapping;
  266. int error;
  267. spin_lock_irq(&mapping->tree_lock);
  268. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  269. page->mapping = NULL;
  270. mapping->nrpages--;
  271. __dec_zone_page_state(page, NR_FILE_PAGES);
  272. __dec_zone_page_state(page, NR_SHMEM);
  273. spin_unlock_irq(&mapping->tree_lock);
  274. page_cache_release(page);
  275. BUG_ON(error);
  276. }
  277. /*
  278. * Like find_get_pages, but collecting swap entries as well as pages.
  279. */
  280. static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
  281. pgoff_t start, unsigned int nr_pages,
  282. struct page **pages, pgoff_t *indices)
  283. {
  284. unsigned int i;
  285. unsigned int ret;
  286. unsigned int nr_found;
  287. rcu_read_lock();
  288. restart:
  289. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  290. (void ***)pages, indices, start, nr_pages);
  291. ret = 0;
  292. for (i = 0; i < nr_found; i++) {
  293. struct page *page;
  294. repeat:
  295. page = radix_tree_deref_slot((void **)pages[i]);
  296. if (unlikely(!page))
  297. continue;
  298. if (radix_tree_exception(page)) {
  299. if (radix_tree_exceptional_entry(page))
  300. goto export;
  301. /* radix_tree_deref_retry(page) */
  302. goto restart;
  303. }
  304. if (!page_cache_get_speculative(page))
  305. goto repeat;
  306. /* Has the page moved? */
  307. if (unlikely(page != *((void **)pages[i]))) {
  308. page_cache_release(page);
  309. goto repeat;
  310. }
  311. export:
  312. indices[ret] = indices[i];
  313. pages[ret] = page;
  314. ret++;
  315. }
  316. if (unlikely(!ret && nr_found))
  317. goto restart;
  318. rcu_read_unlock();
  319. return ret;
  320. }
  321. /*
  322. * Remove swap entry from radix tree, free the swap and its page cache.
  323. */
  324. static int shmem_free_swap(struct address_space *mapping,
  325. pgoff_t index, void *radswap)
  326. {
  327. int error;
  328. spin_lock_irq(&mapping->tree_lock);
  329. error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
  330. spin_unlock_irq(&mapping->tree_lock);
  331. if (!error)
  332. free_swap_and_cache(radix_to_swp_entry(radswap));
  333. return error;
  334. }
  335. /*
  336. * Pagevec may contain swap entries, so shuffle up pages before releasing.
  337. */
  338. static void shmem_pagevec_release(struct pagevec *pvec)
  339. {
  340. int i, j;
  341. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  342. struct page *page = pvec->pages[i];
  343. if (!radix_tree_exceptional_entry(page))
  344. pvec->pages[j++] = page;
  345. }
  346. pvec->nr = j;
  347. pagevec_release(pvec);
  348. }
  349. /*
  350. * Remove range of pages and swap entries from radix tree, and free them.
  351. */
  352. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  353. {
  354. struct address_space *mapping = inode->i_mapping;
  355. struct shmem_inode_info *info = SHMEM_I(inode);
  356. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  357. unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  358. pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
  359. struct pagevec pvec;
  360. pgoff_t indices[PAGEVEC_SIZE];
  361. long nr_swaps_freed = 0;
  362. pgoff_t index;
  363. int i;
  364. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  365. pagevec_init(&pvec, 0);
  366. index = start;
  367. while (index <= end) {
  368. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  369. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  370. pvec.pages, indices);
  371. if (!pvec.nr)
  372. break;
  373. mem_cgroup_uncharge_start();
  374. for (i = 0; i < pagevec_count(&pvec); i++) {
  375. struct page *page = pvec.pages[i];
  376. index = indices[i];
  377. if (index > end)
  378. break;
  379. if (radix_tree_exceptional_entry(page)) {
  380. nr_swaps_freed += !shmem_free_swap(mapping,
  381. index, page);
  382. continue;
  383. }
  384. if (!trylock_page(page))
  385. continue;
  386. if (page->mapping == mapping) {
  387. VM_BUG_ON(PageWriteback(page));
  388. truncate_inode_page(mapping, page);
  389. }
  390. unlock_page(page);
  391. }
  392. shmem_pagevec_release(&pvec);
  393. mem_cgroup_uncharge_end();
  394. cond_resched();
  395. index++;
  396. }
  397. if (partial) {
  398. struct page *page = NULL;
  399. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  400. if (page) {
  401. zero_user_segment(page, partial, PAGE_CACHE_SIZE);
  402. set_page_dirty(page);
  403. unlock_page(page);
  404. page_cache_release(page);
  405. }
  406. }
  407. index = start;
  408. for ( ; ; ) {
  409. cond_resched();
  410. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  411. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  412. pvec.pages, indices);
  413. if (!pvec.nr) {
  414. if (index == start)
  415. break;
  416. index = start;
  417. continue;
  418. }
  419. if (index == start && indices[0] > end) {
  420. shmem_pagevec_release(&pvec);
  421. break;
  422. }
  423. mem_cgroup_uncharge_start();
  424. for (i = 0; i < pagevec_count(&pvec); i++) {
  425. struct page *page = pvec.pages[i];
  426. index = indices[i];
  427. if (index > end)
  428. break;
  429. if (radix_tree_exceptional_entry(page)) {
  430. nr_swaps_freed += !shmem_free_swap(mapping,
  431. index, page);
  432. continue;
  433. }
  434. lock_page(page);
  435. if (page->mapping == mapping) {
  436. VM_BUG_ON(PageWriteback(page));
  437. truncate_inode_page(mapping, page);
  438. }
  439. unlock_page(page);
  440. }
  441. shmem_pagevec_release(&pvec);
  442. mem_cgroup_uncharge_end();
  443. index++;
  444. }
  445. spin_lock(&info->lock);
  446. info->swapped -= nr_swaps_freed;
  447. shmem_recalc_inode(inode);
  448. spin_unlock(&info->lock);
  449. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  450. }
  451. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  452. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  453. {
  454. struct inode *inode = dentry->d_inode;
  455. int error;
  456. error = inode_change_ok(inode, attr);
  457. if (error)
  458. return error;
  459. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  460. loff_t oldsize = inode->i_size;
  461. loff_t newsize = attr->ia_size;
  462. if (newsize != oldsize) {
  463. i_size_write(inode, newsize);
  464. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  465. }
  466. if (newsize < oldsize) {
  467. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  468. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  469. shmem_truncate_range(inode, newsize, (loff_t)-1);
  470. /* unmap again to remove racily COWed private pages */
  471. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  472. }
  473. }
  474. setattr_copy(inode, attr);
  475. #ifdef CONFIG_TMPFS_POSIX_ACL
  476. if (attr->ia_valid & ATTR_MODE)
  477. error = generic_acl_chmod(inode);
  478. #endif
  479. return error;
  480. }
  481. static void shmem_evict_inode(struct inode *inode)
  482. {
  483. struct shmem_inode_info *info = SHMEM_I(inode);
  484. struct shmem_xattr *xattr, *nxattr;
  485. if (inode->i_mapping->a_ops == &shmem_aops) {
  486. shmem_unacct_size(info->flags, inode->i_size);
  487. inode->i_size = 0;
  488. shmem_truncate_range(inode, 0, (loff_t)-1);
  489. if (!list_empty(&info->swaplist)) {
  490. mutex_lock(&shmem_swaplist_mutex);
  491. list_del_init(&info->swaplist);
  492. mutex_unlock(&shmem_swaplist_mutex);
  493. }
  494. } else
  495. kfree(info->symlink);
  496. list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
  497. kfree(xattr->name);
  498. kfree(xattr);
  499. }
  500. BUG_ON(inode->i_blocks);
  501. shmem_free_inode(inode->i_sb);
  502. end_writeback(inode);
  503. }
  504. /*
  505. * If swap found in inode, free it and move page from swapcache to filecache.
  506. */
  507. static int shmem_unuse_inode(struct shmem_inode_info *info,
  508. swp_entry_t swap, struct page *page)
  509. {
  510. struct address_space *mapping = info->vfs_inode.i_mapping;
  511. void *radswap;
  512. pgoff_t index;
  513. int error;
  514. radswap = swp_to_radix_entry(swap);
  515. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  516. if (index == -1)
  517. return 0;
  518. /*
  519. * Move _head_ to start search for next from here.
  520. * But be careful: shmem_evict_inode checks list_empty without taking
  521. * mutex, and there's an instant in list_move_tail when info->swaplist
  522. * would appear empty, if it were the only one on shmem_swaplist.
  523. */
  524. if (shmem_swaplist.next != &info->swaplist)
  525. list_move_tail(&shmem_swaplist, &info->swaplist);
  526. /*
  527. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  528. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  529. * beneath us (pagelock doesn't help until the page is in pagecache).
  530. */
  531. error = shmem_add_to_page_cache(page, mapping, index,
  532. GFP_NOWAIT, radswap);
  533. /* which does mem_cgroup_uncharge_cache_page on error */
  534. if (error != -ENOMEM) {
  535. /*
  536. * Truncation and eviction use free_swap_and_cache(), which
  537. * only does trylock page: if we raced, best clean up here.
  538. */
  539. delete_from_swap_cache(page);
  540. set_page_dirty(page);
  541. if (!error) {
  542. spin_lock(&info->lock);
  543. info->swapped--;
  544. spin_unlock(&info->lock);
  545. swap_free(swap);
  546. }
  547. error = 1; /* not an error, but entry was found */
  548. }
  549. return error;
  550. }
  551. /*
  552. * Search through swapped inodes to find and replace swap by page.
  553. */
  554. int shmem_unuse(swp_entry_t swap, struct page *page)
  555. {
  556. struct list_head *this, *next;
  557. struct shmem_inode_info *info;
  558. int found = 0;
  559. int error;
  560. /*
  561. * Charge page using GFP_KERNEL while we can wait, before taking
  562. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  563. * Charged back to the user (not to caller) when swap account is used.
  564. */
  565. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  566. if (error)
  567. goto out;
  568. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  569. mutex_lock(&shmem_swaplist_mutex);
  570. list_for_each_safe(this, next, &shmem_swaplist) {
  571. info = list_entry(this, struct shmem_inode_info, swaplist);
  572. if (info->swapped)
  573. found = shmem_unuse_inode(info, swap, page);
  574. else
  575. list_del_init(&info->swaplist);
  576. cond_resched();
  577. if (found)
  578. break;
  579. }
  580. mutex_unlock(&shmem_swaplist_mutex);
  581. if (!found)
  582. mem_cgroup_uncharge_cache_page(page);
  583. if (found < 0)
  584. error = found;
  585. out:
  586. unlock_page(page);
  587. page_cache_release(page);
  588. return error;
  589. }
  590. /*
  591. * Move the page from the page cache to the swap cache.
  592. */
  593. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  594. {
  595. struct shmem_inode_info *info;
  596. struct address_space *mapping;
  597. struct inode *inode;
  598. swp_entry_t swap;
  599. pgoff_t index;
  600. BUG_ON(!PageLocked(page));
  601. mapping = page->mapping;
  602. index = page->index;
  603. inode = mapping->host;
  604. info = SHMEM_I(inode);
  605. if (info->flags & VM_LOCKED)
  606. goto redirty;
  607. if (!total_swap_pages)
  608. goto redirty;
  609. /*
  610. * shmem_backing_dev_info's capabilities prevent regular writeback or
  611. * sync from ever calling shmem_writepage; but a stacking filesystem
  612. * might use ->writepage of its underlying filesystem, in which case
  613. * tmpfs should write out to swap only in response to memory pressure,
  614. * and not for the writeback threads or sync.
  615. */
  616. if (!wbc->for_reclaim) {
  617. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  618. goto redirty;
  619. }
  620. swap = get_swap_page();
  621. if (!swap.val)
  622. goto redirty;
  623. /*
  624. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  625. * if it's not already there. Do it now before the page is
  626. * moved to swap cache, when its pagelock no longer protects
  627. * the inode from eviction. But don't unlock the mutex until
  628. * we've incremented swapped, because shmem_unuse_inode() will
  629. * prune a !swapped inode from the swaplist under this mutex.
  630. */
  631. mutex_lock(&shmem_swaplist_mutex);
  632. if (list_empty(&info->swaplist))
  633. list_add_tail(&info->swaplist, &shmem_swaplist);
  634. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  635. swap_shmem_alloc(swap);
  636. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  637. spin_lock(&info->lock);
  638. info->swapped++;
  639. shmem_recalc_inode(inode);
  640. spin_unlock(&info->lock);
  641. mutex_unlock(&shmem_swaplist_mutex);
  642. BUG_ON(page_mapped(page));
  643. swap_writepage(page, wbc);
  644. return 0;
  645. }
  646. mutex_unlock(&shmem_swaplist_mutex);
  647. swapcache_free(swap, NULL);
  648. redirty:
  649. set_page_dirty(page);
  650. if (wbc->for_reclaim)
  651. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  652. unlock_page(page);
  653. return 0;
  654. }
  655. #ifdef CONFIG_NUMA
  656. #ifdef CONFIG_TMPFS
  657. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  658. {
  659. char buffer[64];
  660. if (!mpol || mpol->mode == MPOL_DEFAULT)
  661. return; /* show nothing */
  662. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  663. seq_printf(seq, ",mpol=%s", buffer);
  664. }
  665. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  666. {
  667. struct mempolicy *mpol = NULL;
  668. if (sbinfo->mpol) {
  669. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  670. mpol = sbinfo->mpol;
  671. mpol_get(mpol);
  672. spin_unlock(&sbinfo->stat_lock);
  673. }
  674. return mpol;
  675. }
  676. #endif /* CONFIG_TMPFS */
  677. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  678. struct shmem_inode_info *info, pgoff_t index)
  679. {
  680. struct mempolicy mpol, *spol;
  681. struct vm_area_struct pvma;
  682. spol = mpol_cond_copy(&mpol,
  683. mpol_shared_policy_lookup(&info->policy, index));
  684. /* Create a pseudo vma that just contains the policy */
  685. pvma.vm_start = 0;
  686. pvma.vm_pgoff = index;
  687. pvma.vm_ops = NULL;
  688. pvma.vm_policy = spol;
  689. return swapin_readahead(swap, gfp, &pvma, 0);
  690. }
  691. static struct page *shmem_alloc_page(gfp_t gfp,
  692. struct shmem_inode_info *info, pgoff_t index)
  693. {
  694. struct vm_area_struct pvma;
  695. /* Create a pseudo vma that just contains the policy */
  696. pvma.vm_start = 0;
  697. pvma.vm_pgoff = index;
  698. pvma.vm_ops = NULL;
  699. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  700. /*
  701. * alloc_page_vma() will drop the shared policy reference
  702. */
  703. return alloc_page_vma(gfp, &pvma, 0);
  704. }
  705. #else /* !CONFIG_NUMA */
  706. #ifdef CONFIG_TMPFS
  707. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  708. {
  709. }
  710. #endif /* CONFIG_TMPFS */
  711. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  712. struct shmem_inode_info *info, pgoff_t index)
  713. {
  714. return swapin_readahead(swap, gfp, NULL, 0);
  715. }
  716. static inline struct page *shmem_alloc_page(gfp_t gfp,
  717. struct shmem_inode_info *info, pgoff_t index)
  718. {
  719. return alloc_page(gfp);
  720. }
  721. #endif /* CONFIG_NUMA */
  722. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  723. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  724. {
  725. return NULL;
  726. }
  727. #endif
  728. /*
  729. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  730. *
  731. * If we allocate a new one we do not mark it dirty. That's up to the
  732. * vm. If we swap it in we mark it dirty since we also free the swap
  733. * entry since a page cannot live in both the swap and page cache
  734. */
  735. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  736. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  737. {
  738. struct address_space *mapping = inode->i_mapping;
  739. struct shmem_inode_info *info;
  740. struct shmem_sb_info *sbinfo;
  741. struct page *page;
  742. swp_entry_t swap;
  743. int error;
  744. int once = 0;
  745. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  746. return -EFBIG;
  747. repeat:
  748. swap.val = 0;
  749. page = find_lock_page(mapping, index);
  750. if (radix_tree_exceptional_entry(page)) {
  751. swap = radix_to_swp_entry(page);
  752. page = NULL;
  753. }
  754. if (sgp != SGP_WRITE &&
  755. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  756. error = -EINVAL;
  757. goto failed;
  758. }
  759. if (page || (sgp == SGP_READ && !swap.val)) {
  760. /*
  761. * Once we can get the page lock, it must be uptodate:
  762. * if there were an error in reading back from swap,
  763. * the page would not be inserted into the filecache.
  764. */
  765. BUG_ON(page && !PageUptodate(page));
  766. *pagep = page;
  767. return 0;
  768. }
  769. /*
  770. * Fast cache lookup did not find it:
  771. * bring it back from swap or allocate.
  772. */
  773. info = SHMEM_I(inode);
  774. sbinfo = SHMEM_SB(inode->i_sb);
  775. if (swap.val) {
  776. /* Look it up and read it in.. */
  777. page = lookup_swap_cache(swap);
  778. if (!page) {
  779. /* here we actually do the io */
  780. if (fault_type)
  781. *fault_type |= VM_FAULT_MAJOR;
  782. page = shmem_swapin(swap, gfp, info, index);
  783. if (!page) {
  784. error = -ENOMEM;
  785. goto failed;
  786. }
  787. }
  788. /* We have to do this with page locked to prevent races */
  789. lock_page(page);
  790. if (!PageUptodate(page)) {
  791. error = -EIO;
  792. goto failed;
  793. }
  794. wait_on_page_writeback(page);
  795. /* Someone may have already done it for us */
  796. if (page->mapping) {
  797. if (page->mapping == mapping &&
  798. page->index == index)
  799. goto done;
  800. error = -EEXIST;
  801. goto failed;
  802. }
  803. error = mem_cgroup_cache_charge(page, current->mm,
  804. gfp & GFP_RECLAIM_MASK);
  805. if (!error)
  806. error = shmem_add_to_page_cache(page, mapping, index,
  807. gfp, swp_to_radix_entry(swap));
  808. if (error)
  809. goto failed;
  810. spin_lock(&info->lock);
  811. info->swapped--;
  812. shmem_recalc_inode(inode);
  813. spin_unlock(&info->lock);
  814. delete_from_swap_cache(page);
  815. set_page_dirty(page);
  816. swap_free(swap);
  817. } else {
  818. if (shmem_acct_block(info->flags)) {
  819. error = -ENOSPC;
  820. goto failed;
  821. }
  822. if (sbinfo->max_blocks) {
  823. if (percpu_counter_compare(&sbinfo->used_blocks,
  824. sbinfo->max_blocks) >= 0) {
  825. error = -ENOSPC;
  826. goto unacct;
  827. }
  828. percpu_counter_inc(&sbinfo->used_blocks);
  829. }
  830. page = shmem_alloc_page(gfp, info, index);
  831. if (!page) {
  832. error = -ENOMEM;
  833. goto decused;
  834. }
  835. SetPageSwapBacked(page);
  836. __set_page_locked(page);
  837. error = mem_cgroup_cache_charge(page, current->mm,
  838. gfp & GFP_RECLAIM_MASK);
  839. if (!error)
  840. error = shmem_add_to_page_cache(page, mapping, index,
  841. gfp, NULL);
  842. if (error)
  843. goto decused;
  844. lru_cache_add_anon(page);
  845. spin_lock(&info->lock);
  846. info->alloced++;
  847. inode->i_blocks += BLOCKS_PER_PAGE;
  848. shmem_recalc_inode(inode);
  849. spin_unlock(&info->lock);
  850. clear_highpage(page);
  851. flush_dcache_page(page);
  852. SetPageUptodate(page);
  853. if (sgp == SGP_DIRTY)
  854. set_page_dirty(page);
  855. }
  856. done:
  857. /* Perhaps the file has been truncated since we checked */
  858. if (sgp != SGP_WRITE &&
  859. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  860. error = -EINVAL;
  861. goto trunc;
  862. }
  863. *pagep = page;
  864. return 0;
  865. /*
  866. * Error recovery.
  867. */
  868. trunc:
  869. ClearPageDirty(page);
  870. delete_from_page_cache(page);
  871. spin_lock(&info->lock);
  872. info->alloced--;
  873. inode->i_blocks -= BLOCKS_PER_PAGE;
  874. spin_unlock(&info->lock);
  875. decused:
  876. if (sbinfo->max_blocks)
  877. percpu_counter_add(&sbinfo->used_blocks, -1);
  878. unacct:
  879. shmem_unacct_blocks(info->flags, 1);
  880. failed:
  881. if (swap.val && error != -EINVAL) {
  882. struct page *test = find_get_page(mapping, index);
  883. if (test && !radix_tree_exceptional_entry(test))
  884. page_cache_release(test);
  885. /* Have another try if the entry has changed */
  886. if (test != swp_to_radix_entry(swap))
  887. error = -EEXIST;
  888. }
  889. if (page) {
  890. unlock_page(page);
  891. page_cache_release(page);
  892. }
  893. if (error == -ENOSPC && !once++) {
  894. info = SHMEM_I(inode);
  895. spin_lock(&info->lock);
  896. shmem_recalc_inode(inode);
  897. spin_unlock(&info->lock);
  898. goto repeat;
  899. }
  900. if (error == -EEXIST)
  901. goto repeat;
  902. return error;
  903. }
  904. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  905. {
  906. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  907. int error;
  908. int ret = VM_FAULT_LOCKED;
  909. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  910. if (error)
  911. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  912. if (ret & VM_FAULT_MAJOR) {
  913. count_vm_event(PGMAJFAULT);
  914. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  915. }
  916. return ret;
  917. }
  918. #ifdef CONFIG_NUMA
  919. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  920. {
  921. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  922. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  923. }
  924. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  925. unsigned long addr)
  926. {
  927. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  928. pgoff_t index;
  929. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  930. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  931. }
  932. #endif
  933. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  934. {
  935. struct inode *inode = file->f_path.dentry->d_inode;
  936. struct shmem_inode_info *info = SHMEM_I(inode);
  937. int retval = -ENOMEM;
  938. spin_lock(&info->lock);
  939. if (lock && !(info->flags & VM_LOCKED)) {
  940. if (!user_shm_lock(inode->i_size, user))
  941. goto out_nomem;
  942. info->flags |= VM_LOCKED;
  943. mapping_set_unevictable(file->f_mapping);
  944. }
  945. if (!lock && (info->flags & VM_LOCKED) && user) {
  946. user_shm_unlock(inode->i_size, user);
  947. info->flags &= ~VM_LOCKED;
  948. mapping_clear_unevictable(file->f_mapping);
  949. scan_mapping_unevictable_pages(file->f_mapping);
  950. }
  951. retval = 0;
  952. out_nomem:
  953. spin_unlock(&info->lock);
  954. return retval;
  955. }
  956. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  957. {
  958. file_accessed(file);
  959. vma->vm_ops = &shmem_vm_ops;
  960. vma->vm_flags |= VM_CAN_NONLINEAR;
  961. return 0;
  962. }
  963. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  964. int mode, dev_t dev, unsigned long flags)
  965. {
  966. struct inode *inode;
  967. struct shmem_inode_info *info;
  968. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  969. if (shmem_reserve_inode(sb))
  970. return NULL;
  971. inode = new_inode(sb);
  972. if (inode) {
  973. inode->i_ino = get_next_ino();
  974. inode_init_owner(inode, dir, mode);
  975. inode->i_blocks = 0;
  976. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  977. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  978. inode->i_generation = get_seconds();
  979. info = SHMEM_I(inode);
  980. memset(info, 0, (char *)inode - (char *)info);
  981. spin_lock_init(&info->lock);
  982. info->flags = flags & VM_NORESERVE;
  983. INIT_LIST_HEAD(&info->swaplist);
  984. INIT_LIST_HEAD(&info->xattr_list);
  985. cache_no_acl(inode);
  986. switch (mode & S_IFMT) {
  987. default:
  988. inode->i_op = &shmem_special_inode_operations;
  989. init_special_inode(inode, mode, dev);
  990. break;
  991. case S_IFREG:
  992. inode->i_mapping->a_ops = &shmem_aops;
  993. inode->i_op = &shmem_inode_operations;
  994. inode->i_fop = &shmem_file_operations;
  995. mpol_shared_policy_init(&info->policy,
  996. shmem_get_sbmpol(sbinfo));
  997. break;
  998. case S_IFDIR:
  999. inc_nlink(inode);
  1000. /* Some things misbehave if size == 0 on a directory */
  1001. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1002. inode->i_op = &shmem_dir_inode_operations;
  1003. inode->i_fop = &simple_dir_operations;
  1004. break;
  1005. case S_IFLNK:
  1006. /*
  1007. * Must not load anything in the rbtree,
  1008. * mpol_free_shared_policy will not be called.
  1009. */
  1010. mpol_shared_policy_init(&info->policy, NULL);
  1011. break;
  1012. }
  1013. } else
  1014. shmem_free_inode(sb);
  1015. return inode;
  1016. }
  1017. #ifdef CONFIG_TMPFS
  1018. static const struct inode_operations shmem_symlink_inode_operations;
  1019. static const struct inode_operations shmem_short_symlink_operations;
  1020. static int
  1021. shmem_write_begin(struct file *file, struct address_space *mapping,
  1022. loff_t pos, unsigned len, unsigned flags,
  1023. struct page **pagep, void **fsdata)
  1024. {
  1025. struct inode *inode = mapping->host;
  1026. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1027. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1028. }
  1029. static int
  1030. shmem_write_end(struct file *file, struct address_space *mapping,
  1031. loff_t pos, unsigned len, unsigned copied,
  1032. struct page *page, void *fsdata)
  1033. {
  1034. struct inode *inode = mapping->host;
  1035. if (pos + copied > inode->i_size)
  1036. i_size_write(inode, pos + copied);
  1037. set_page_dirty(page);
  1038. unlock_page(page);
  1039. page_cache_release(page);
  1040. return copied;
  1041. }
  1042. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1043. {
  1044. struct inode *inode = filp->f_path.dentry->d_inode;
  1045. struct address_space *mapping = inode->i_mapping;
  1046. pgoff_t index;
  1047. unsigned long offset;
  1048. enum sgp_type sgp = SGP_READ;
  1049. /*
  1050. * Might this read be for a stacking filesystem? Then when reading
  1051. * holes of a sparse file, we actually need to allocate those pages,
  1052. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1053. */
  1054. if (segment_eq(get_fs(), KERNEL_DS))
  1055. sgp = SGP_DIRTY;
  1056. index = *ppos >> PAGE_CACHE_SHIFT;
  1057. offset = *ppos & ~PAGE_CACHE_MASK;
  1058. for (;;) {
  1059. struct page *page = NULL;
  1060. pgoff_t end_index;
  1061. unsigned long nr, ret;
  1062. loff_t i_size = i_size_read(inode);
  1063. end_index = i_size >> PAGE_CACHE_SHIFT;
  1064. if (index > end_index)
  1065. break;
  1066. if (index == end_index) {
  1067. nr = i_size & ~PAGE_CACHE_MASK;
  1068. if (nr <= offset)
  1069. break;
  1070. }
  1071. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1072. if (desc->error) {
  1073. if (desc->error == -EINVAL)
  1074. desc->error = 0;
  1075. break;
  1076. }
  1077. if (page)
  1078. unlock_page(page);
  1079. /*
  1080. * We must evaluate after, since reads (unlike writes)
  1081. * are called without i_mutex protection against truncate
  1082. */
  1083. nr = PAGE_CACHE_SIZE;
  1084. i_size = i_size_read(inode);
  1085. end_index = i_size >> PAGE_CACHE_SHIFT;
  1086. if (index == end_index) {
  1087. nr = i_size & ~PAGE_CACHE_MASK;
  1088. if (nr <= offset) {
  1089. if (page)
  1090. page_cache_release(page);
  1091. break;
  1092. }
  1093. }
  1094. nr -= offset;
  1095. if (page) {
  1096. /*
  1097. * If users can be writing to this page using arbitrary
  1098. * virtual addresses, take care about potential aliasing
  1099. * before reading the page on the kernel side.
  1100. */
  1101. if (mapping_writably_mapped(mapping))
  1102. flush_dcache_page(page);
  1103. /*
  1104. * Mark the page accessed if we read the beginning.
  1105. */
  1106. if (!offset)
  1107. mark_page_accessed(page);
  1108. } else {
  1109. page = ZERO_PAGE(0);
  1110. page_cache_get(page);
  1111. }
  1112. /*
  1113. * Ok, we have the page, and it's up-to-date, so
  1114. * now we can copy it to user space...
  1115. *
  1116. * The actor routine returns how many bytes were actually used..
  1117. * NOTE! This may not be the same as how much of a user buffer
  1118. * we filled up (we may be padding etc), so we can only update
  1119. * "pos" here (the actor routine has to update the user buffer
  1120. * pointers and the remaining count).
  1121. */
  1122. ret = actor(desc, page, offset, nr);
  1123. offset += ret;
  1124. index += offset >> PAGE_CACHE_SHIFT;
  1125. offset &= ~PAGE_CACHE_MASK;
  1126. page_cache_release(page);
  1127. if (ret != nr || !desc->count)
  1128. break;
  1129. cond_resched();
  1130. }
  1131. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1132. file_accessed(filp);
  1133. }
  1134. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1135. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1136. {
  1137. struct file *filp = iocb->ki_filp;
  1138. ssize_t retval;
  1139. unsigned long seg;
  1140. size_t count;
  1141. loff_t *ppos = &iocb->ki_pos;
  1142. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1143. if (retval)
  1144. return retval;
  1145. for (seg = 0; seg < nr_segs; seg++) {
  1146. read_descriptor_t desc;
  1147. desc.written = 0;
  1148. desc.arg.buf = iov[seg].iov_base;
  1149. desc.count = iov[seg].iov_len;
  1150. if (desc.count == 0)
  1151. continue;
  1152. desc.error = 0;
  1153. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1154. retval += desc.written;
  1155. if (desc.error) {
  1156. retval = retval ?: desc.error;
  1157. break;
  1158. }
  1159. if (desc.count > 0)
  1160. break;
  1161. }
  1162. return retval;
  1163. }
  1164. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1165. struct pipe_inode_info *pipe, size_t len,
  1166. unsigned int flags)
  1167. {
  1168. struct address_space *mapping = in->f_mapping;
  1169. struct inode *inode = mapping->host;
  1170. unsigned int loff, nr_pages, req_pages;
  1171. struct page *pages[PIPE_DEF_BUFFERS];
  1172. struct partial_page partial[PIPE_DEF_BUFFERS];
  1173. struct page *page;
  1174. pgoff_t index, end_index;
  1175. loff_t isize, left;
  1176. int error, page_nr;
  1177. struct splice_pipe_desc spd = {
  1178. .pages = pages,
  1179. .partial = partial,
  1180. .flags = flags,
  1181. .ops = &page_cache_pipe_buf_ops,
  1182. .spd_release = spd_release_page,
  1183. };
  1184. isize = i_size_read(inode);
  1185. if (unlikely(*ppos >= isize))
  1186. return 0;
  1187. left = isize - *ppos;
  1188. if (unlikely(left < len))
  1189. len = left;
  1190. if (splice_grow_spd(pipe, &spd))
  1191. return -ENOMEM;
  1192. index = *ppos >> PAGE_CACHE_SHIFT;
  1193. loff = *ppos & ~PAGE_CACHE_MASK;
  1194. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1195. nr_pages = min(req_pages, pipe->buffers);
  1196. spd.nr_pages = find_get_pages_contig(mapping, index,
  1197. nr_pages, spd.pages);
  1198. index += spd.nr_pages;
  1199. error = 0;
  1200. while (spd.nr_pages < nr_pages) {
  1201. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1202. if (error)
  1203. break;
  1204. unlock_page(page);
  1205. spd.pages[spd.nr_pages++] = page;
  1206. index++;
  1207. }
  1208. index = *ppos >> PAGE_CACHE_SHIFT;
  1209. nr_pages = spd.nr_pages;
  1210. spd.nr_pages = 0;
  1211. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1212. unsigned int this_len;
  1213. if (!len)
  1214. break;
  1215. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1216. page = spd.pages[page_nr];
  1217. if (!PageUptodate(page) || page->mapping != mapping) {
  1218. error = shmem_getpage(inode, index, &page,
  1219. SGP_CACHE, NULL);
  1220. if (error)
  1221. break;
  1222. unlock_page(page);
  1223. page_cache_release(spd.pages[page_nr]);
  1224. spd.pages[page_nr] = page;
  1225. }
  1226. isize = i_size_read(inode);
  1227. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1228. if (unlikely(!isize || index > end_index))
  1229. break;
  1230. if (end_index == index) {
  1231. unsigned int plen;
  1232. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1233. if (plen <= loff)
  1234. break;
  1235. this_len = min(this_len, plen - loff);
  1236. len = this_len;
  1237. }
  1238. spd.partial[page_nr].offset = loff;
  1239. spd.partial[page_nr].len = this_len;
  1240. len -= this_len;
  1241. loff = 0;
  1242. spd.nr_pages++;
  1243. index++;
  1244. }
  1245. while (page_nr < nr_pages)
  1246. page_cache_release(spd.pages[page_nr++]);
  1247. if (spd.nr_pages)
  1248. error = splice_to_pipe(pipe, &spd);
  1249. splice_shrink_spd(pipe, &spd);
  1250. if (error > 0) {
  1251. *ppos += error;
  1252. file_accessed(in);
  1253. }
  1254. return error;
  1255. }
  1256. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1257. {
  1258. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1259. buf->f_type = TMPFS_MAGIC;
  1260. buf->f_bsize = PAGE_CACHE_SIZE;
  1261. buf->f_namelen = NAME_MAX;
  1262. if (sbinfo->max_blocks) {
  1263. buf->f_blocks = sbinfo->max_blocks;
  1264. buf->f_bavail =
  1265. buf->f_bfree = sbinfo->max_blocks -
  1266. percpu_counter_sum(&sbinfo->used_blocks);
  1267. }
  1268. if (sbinfo->max_inodes) {
  1269. buf->f_files = sbinfo->max_inodes;
  1270. buf->f_ffree = sbinfo->free_inodes;
  1271. }
  1272. /* else leave those fields 0 like simple_statfs */
  1273. return 0;
  1274. }
  1275. /*
  1276. * File creation. Allocate an inode, and we're done..
  1277. */
  1278. static int
  1279. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1280. {
  1281. struct inode *inode;
  1282. int error = -ENOSPC;
  1283. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1284. if (inode) {
  1285. error = security_inode_init_security(inode, dir,
  1286. &dentry->d_name, NULL,
  1287. NULL, NULL);
  1288. if (error) {
  1289. if (error != -EOPNOTSUPP) {
  1290. iput(inode);
  1291. return error;
  1292. }
  1293. }
  1294. #ifdef CONFIG_TMPFS_POSIX_ACL
  1295. error = generic_acl_init(inode, dir);
  1296. if (error) {
  1297. iput(inode);
  1298. return error;
  1299. }
  1300. #else
  1301. error = 0;
  1302. #endif
  1303. dir->i_size += BOGO_DIRENT_SIZE;
  1304. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1305. d_instantiate(dentry, inode);
  1306. dget(dentry); /* Extra count - pin the dentry in core */
  1307. }
  1308. return error;
  1309. }
  1310. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1311. {
  1312. int error;
  1313. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1314. return error;
  1315. inc_nlink(dir);
  1316. return 0;
  1317. }
  1318. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1319. struct nameidata *nd)
  1320. {
  1321. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1322. }
  1323. /*
  1324. * Link a file..
  1325. */
  1326. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1327. {
  1328. struct inode *inode = old_dentry->d_inode;
  1329. int ret;
  1330. /*
  1331. * No ordinary (disk based) filesystem counts links as inodes;
  1332. * but each new link needs a new dentry, pinning lowmem, and
  1333. * tmpfs dentries cannot be pruned until they are unlinked.
  1334. */
  1335. ret = shmem_reserve_inode(inode->i_sb);
  1336. if (ret)
  1337. goto out;
  1338. dir->i_size += BOGO_DIRENT_SIZE;
  1339. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1340. inc_nlink(inode);
  1341. ihold(inode); /* New dentry reference */
  1342. dget(dentry); /* Extra pinning count for the created dentry */
  1343. d_instantiate(dentry, inode);
  1344. out:
  1345. return ret;
  1346. }
  1347. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1348. {
  1349. struct inode *inode = dentry->d_inode;
  1350. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1351. shmem_free_inode(inode->i_sb);
  1352. dir->i_size -= BOGO_DIRENT_SIZE;
  1353. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1354. drop_nlink(inode);
  1355. dput(dentry); /* Undo the count from "create" - this does all the work */
  1356. return 0;
  1357. }
  1358. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1359. {
  1360. if (!simple_empty(dentry))
  1361. return -ENOTEMPTY;
  1362. drop_nlink(dentry->d_inode);
  1363. drop_nlink(dir);
  1364. return shmem_unlink(dir, dentry);
  1365. }
  1366. /*
  1367. * The VFS layer already does all the dentry stuff for rename,
  1368. * we just have to decrement the usage count for the target if
  1369. * it exists so that the VFS layer correctly free's it when it
  1370. * gets overwritten.
  1371. */
  1372. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1373. {
  1374. struct inode *inode = old_dentry->d_inode;
  1375. int they_are_dirs = S_ISDIR(inode->i_mode);
  1376. if (!simple_empty(new_dentry))
  1377. return -ENOTEMPTY;
  1378. if (new_dentry->d_inode) {
  1379. (void) shmem_unlink(new_dir, new_dentry);
  1380. if (they_are_dirs)
  1381. drop_nlink(old_dir);
  1382. } else if (they_are_dirs) {
  1383. drop_nlink(old_dir);
  1384. inc_nlink(new_dir);
  1385. }
  1386. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1387. new_dir->i_size += BOGO_DIRENT_SIZE;
  1388. old_dir->i_ctime = old_dir->i_mtime =
  1389. new_dir->i_ctime = new_dir->i_mtime =
  1390. inode->i_ctime = CURRENT_TIME;
  1391. return 0;
  1392. }
  1393. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1394. {
  1395. int error;
  1396. int len;
  1397. struct inode *inode;
  1398. struct page *page;
  1399. char *kaddr;
  1400. struct shmem_inode_info *info;
  1401. len = strlen(symname) + 1;
  1402. if (len > PAGE_CACHE_SIZE)
  1403. return -ENAMETOOLONG;
  1404. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1405. if (!inode)
  1406. return -ENOSPC;
  1407. error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
  1408. NULL, NULL);
  1409. if (error) {
  1410. if (error != -EOPNOTSUPP) {
  1411. iput(inode);
  1412. return error;
  1413. }
  1414. error = 0;
  1415. }
  1416. info = SHMEM_I(inode);
  1417. inode->i_size = len-1;
  1418. if (len <= SHORT_SYMLINK_LEN) {
  1419. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  1420. if (!info->symlink) {
  1421. iput(inode);
  1422. return -ENOMEM;
  1423. }
  1424. inode->i_op = &shmem_short_symlink_operations;
  1425. } else {
  1426. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1427. if (error) {
  1428. iput(inode);
  1429. return error;
  1430. }
  1431. inode->i_mapping->a_ops = &shmem_aops;
  1432. inode->i_op = &shmem_symlink_inode_operations;
  1433. kaddr = kmap_atomic(page, KM_USER0);
  1434. memcpy(kaddr, symname, len);
  1435. kunmap_atomic(kaddr, KM_USER0);
  1436. set_page_dirty(page);
  1437. unlock_page(page);
  1438. page_cache_release(page);
  1439. }
  1440. dir->i_size += BOGO_DIRENT_SIZE;
  1441. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1442. d_instantiate(dentry, inode);
  1443. dget(dentry);
  1444. return 0;
  1445. }
  1446. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  1447. {
  1448. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  1449. return NULL;
  1450. }
  1451. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1452. {
  1453. struct page *page = NULL;
  1454. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1455. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1456. if (page)
  1457. unlock_page(page);
  1458. return page;
  1459. }
  1460. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1461. {
  1462. if (!IS_ERR(nd_get_link(nd))) {
  1463. struct page *page = cookie;
  1464. kunmap(page);
  1465. mark_page_accessed(page);
  1466. page_cache_release(page);
  1467. }
  1468. }
  1469. #ifdef CONFIG_TMPFS_XATTR
  1470. /*
  1471. * Superblocks without xattr inode operations may get some security.* xattr
  1472. * support from the LSM "for free". As soon as we have any other xattrs
  1473. * like ACLs, we also need to implement the security.* handlers at
  1474. * filesystem level, though.
  1475. */
  1476. static int shmem_xattr_get(struct dentry *dentry, const char *name,
  1477. void *buffer, size_t size)
  1478. {
  1479. struct shmem_inode_info *info;
  1480. struct shmem_xattr *xattr;
  1481. int ret = -ENODATA;
  1482. info = SHMEM_I(dentry->d_inode);
  1483. spin_lock(&info->lock);
  1484. list_for_each_entry(xattr, &info->xattr_list, list) {
  1485. if (strcmp(name, xattr->name))
  1486. continue;
  1487. ret = xattr->size;
  1488. if (buffer) {
  1489. if (size < xattr->size)
  1490. ret = -ERANGE;
  1491. else
  1492. memcpy(buffer, xattr->value, xattr->size);
  1493. }
  1494. break;
  1495. }
  1496. spin_unlock(&info->lock);
  1497. return ret;
  1498. }
  1499. static int shmem_xattr_set(struct dentry *dentry, const char *name,
  1500. const void *value, size_t size, int flags)
  1501. {
  1502. struct inode *inode = dentry->d_inode;
  1503. struct shmem_inode_info *info = SHMEM_I(inode);
  1504. struct shmem_xattr *xattr;
  1505. struct shmem_xattr *new_xattr = NULL;
  1506. size_t len;
  1507. int err = 0;
  1508. /* value == NULL means remove */
  1509. if (value) {
  1510. /* wrap around? */
  1511. len = sizeof(*new_xattr) + size;
  1512. if (len <= sizeof(*new_xattr))
  1513. return -ENOMEM;
  1514. new_xattr = kmalloc(len, GFP_KERNEL);
  1515. if (!new_xattr)
  1516. return -ENOMEM;
  1517. new_xattr->name = kstrdup(name, GFP_KERNEL);
  1518. if (!new_xattr->name) {
  1519. kfree(new_xattr);
  1520. return -ENOMEM;
  1521. }
  1522. new_xattr->size = size;
  1523. memcpy(new_xattr->value, value, size);
  1524. }
  1525. spin_lock(&info->lock);
  1526. list_for_each_entry(xattr, &info->xattr_list, list) {
  1527. if (!strcmp(name, xattr->name)) {
  1528. if (flags & XATTR_CREATE) {
  1529. xattr = new_xattr;
  1530. err = -EEXIST;
  1531. } else if (new_xattr) {
  1532. list_replace(&xattr->list, &new_xattr->list);
  1533. } else {
  1534. list_del(&xattr->list);
  1535. }
  1536. goto out;
  1537. }
  1538. }
  1539. if (flags & XATTR_REPLACE) {
  1540. xattr = new_xattr;
  1541. err = -ENODATA;
  1542. } else {
  1543. list_add(&new_xattr->list, &info->xattr_list);
  1544. xattr = NULL;
  1545. }
  1546. out:
  1547. spin_unlock(&info->lock);
  1548. if (xattr)
  1549. kfree(xattr->name);
  1550. kfree(xattr);
  1551. return err;
  1552. }
  1553. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1554. #ifdef CONFIG_TMPFS_POSIX_ACL
  1555. &generic_acl_access_handler,
  1556. &generic_acl_default_handler,
  1557. #endif
  1558. NULL
  1559. };
  1560. static int shmem_xattr_validate(const char *name)
  1561. {
  1562. struct { const char *prefix; size_t len; } arr[] = {
  1563. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1564. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1565. };
  1566. int i;
  1567. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1568. size_t preflen = arr[i].len;
  1569. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1570. if (!name[preflen])
  1571. return -EINVAL;
  1572. return 0;
  1573. }
  1574. }
  1575. return -EOPNOTSUPP;
  1576. }
  1577. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1578. void *buffer, size_t size)
  1579. {
  1580. int err;
  1581. /*
  1582. * If this is a request for a synthetic attribute in the system.*
  1583. * namespace use the generic infrastructure to resolve a handler
  1584. * for it via sb->s_xattr.
  1585. */
  1586. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1587. return generic_getxattr(dentry, name, buffer, size);
  1588. err = shmem_xattr_validate(name);
  1589. if (err)
  1590. return err;
  1591. return shmem_xattr_get(dentry, name, buffer, size);
  1592. }
  1593. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1594. const void *value, size_t size, int flags)
  1595. {
  1596. int err;
  1597. /*
  1598. * If this is a request for a synthetic attribute in the system.*
  1599. * namespace use the generic infrastructure to resolve a handler
  1600. * for it via sb->s_xattr.
  1601. */
  1602. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1603. return generic_setxattr(dentry, name, value, size, flags);
  1604. err = shmem_xattr_validate(name);
  1605. if (err)
  1606. return err;
  1607. if (size == 0)
  1608. value = ""; /* empty EA, do not remove */
  1609. return shmem_xattr_set(dentry, name, value, size, flags);
  1610. }
  1611. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1612. {
  1613. int err;
  1614. /*
  1615. * If this is a request for a synthetic attribute in the system.*
  1616. * namespace use the generic infrastructure to resolve a handler
  1617. * for it via sb->s_xattr.
  1618. */
  1619. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1620. return generic_removexattr(dentry, name);
  1621. err = shmem_xattr_validate(name);
  1622. if (err)
  1623. return err;
  1624. return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
  1625. }
  1626. static bool xattr_is_trusted(const char *name)
  1627. {
  1628. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  1629. }
  1630. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1631. {
  1632. bool trusted = capable(CAP_SYS_ADMIN);
  1633. struct shmem_xattr *xattr;
  1634. struct shmem_inode_info *info;
  1635. size_t used = 0;
  1636. info = SHMEM_I(dentry->d_inode);
  1637. spin_lock(&info->lock);
  1638. list_for_each_entry(xattr, &info->xattr_list, list) {
  1639. size_t len;
  1640. /* skip "trusted." attributes for unprivileged callers */
  1641. if (!trusted && xattr_is_trusted(xattr->name))
  1642. continue;
  1643. len = strlen(xattr->name) + 1;
  1644. used += len;
  1645. if (buffer) {
  1646. if (size < used) {
  1647. used = -ERANGE;
  1648. break;
  1649. }
  1650. memcpy(buffer, xattr->name, len);
  1651. buffer += len;
  1652. }
  1653. }
  1654. spin_unlock(&info->lock);
  1655. return used;
  1656. }
  1657. #endif /* CONFIG_TMPFS_XATTR */
  1658. static const struct inode_operations shmem_short_symlink_operations = {
  1659. .readlink = generic_readlink,
  1660. .follow_link = shmem_follow_short_symlink,
  1661. #ifdef CONFIG_TMPFS_XATTR
  1662. .setxattr = shmem_setxattr,
  1663. .getxattr = shmem_getxattr,
  1664. .listxattr = shmem_listxattr,
  1665. .removexattr = shmem_removexattr,
  1666. #endif
  1667. };
  1668. static const struct inode_operations shmem_symlink_inode_operations = {
  1669. .readlink = generic_readlink,
  1670. .follow_link = shmem_follow_link,
  1671. .put_link = shmem_put_link,
  1672. #ifdef CONFIG_TMPFS_XATTR
  1673. .setxattr = shmem_setxattr,
  1674. .getxattr = shmem_getxattr,
  1675. .listxattr = shmem_listxattr,
  1676. .removexattr = shmem_removexattr,
  1677. #endif
  1678. };
  1679. static struct dentry *shmem_get_parent(struct dentry *child)
  1680. {
  1681. return ERR_PTR(-ESTALE);
  1682. }
  1683. static int shmem_match(struct inode *ino, void *vfh)
  1684. {
  1685. __u32 *fh = vfh;
  1686. __u64 inum = fh[2];
  1687. inum = (inum << 32) | fh[1];
  1688. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1689. }
  1690. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1691. struct fid *fid, int fh_len, int fh_type)
  1692. {
  1693. struct inode *inode;
  1694. struct dentry *dentry = NULL;
  1695. u64 inum = fid->raw[2];
  1696. inum = (inum << 32) | fid->raw[1];
  1697. if (fh_len < 3)
  1698. return NULL;
  1699. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1700. shmem_match, fid->raw);
  1701. if (inode) {
  1702. dentry = d_find_alias(inode);
  1703. iput(inode);
  1704. }
  1705. return dentry;
  1706. }
  1707. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1708. int connectable)
  1709. {
  1710. struct inode *inode = dentry->d_inode;
  1711. if (*len < 3) {
  1712. *len = 3;
  1713. return 255;
  1714. }
  1715. if (inode_unhashed(inode)) {
  1716. /* Unfortunately insert_inode_hash is not idempotent,
  1717. * so as we hash inodes here rather than at creation
  1718. * time, we need a lock to ensure we only try
  1719. * to do it once
  1720. */
  1721. static DEFINE_SPINLOCK(lock);
  1722. spin_lock(&lock);
  1723. if (inode_unhashed(inode))
  1724. __insert_inode_hash(inode,
  1725. inode->i_ino + inode->i_generation);
  1726. spin_unlock(&lock);
  1727. }
  1728. fh[0] = inode->i_generation;
  1729. fh[1] = inode->i_ino;
  1730. fh[2] = ((__u64)inode->i_ino) >> 32;
  1731. *len = 3;
  1732. return 1;
  1733. }
  1734. static const struct export_operations shmem_export_ops = {
  1735. .get_parent = shmem_get_parent,
  1736. .encode_fh = shmem_encode_fh,
  1737. .fh_to_dentry = shmem_fh_to_dentry,
  1738. };
  1739. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  1740. bool remount)
  1741. {
  1742. char *this_char, *value, *rest;
  1743. while (options != NULL) {
  1744. this_char = options;
  1745. for (;;) {
  1746. /*
  1747. * NUL-terminate this option: unfortunately,
  1748. * mount options form a comma-separated list,
  1749. * but mpol's nodelist may also contain commas.
  1750. */
  1751. options = strchr(options, ',');
  1752. if (options == NULL)
  1753. break;
  1754. options++;
  1755. if (!isdigit(*options)) {
  1756. options[-1] = '\0';
  1757. break;
  1758. }
  1759. }
  1760. if (!*this_char)
  1761. continue;
  1762. if ((value = strchr(this_char,'=')) != NULL) {
  1763. *value++ = 0;
  1764. } else {
  1765. printk(KERN_ERR
  1766. "tmpfs: No value for mount option '%s'\n",
  1767. this_char);
  1768. return 1;
  1769. }
  1770. if (!strcmp(this_char,"size")) {
  1771. unsigned long long size;
  1772. size = memparse(value,&rest);
  1773. if (*rest == '%') {
  1774. size <<= PAGE_SHIFT;
  1775. size *= totalram_pages;
  1776. do_div(size, 100);
  1777. rest++;
  1778. }
  1779. if (*rest)
  1780. goto bad_val;
  1781. sbinfo->max_blocks =
  1782. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  1783. } else if (!strcmp(this_char,"nr_blocks")) {
  1784. sbinfo->max_blocks = memparse(value, &rest);
  1785. if (*rest)
  1786. goto bad_val;
  1787. } else if (!strcmp(this_char,"nr_inodes")) {
  1788. sbinfo->max_inodes = memparse(value, &rest);
  1789. if (*rest)
  1790. goto bad_val;
  1791. } else if (!strcmp(this_char,"mode")) {
  1792. if (remount)
  1793. continue;
  1794. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  1795. if (*rest)
  1796. goto bad_val;
  1797. } else if (!strcmp(this_char,"uid")) {
  1798. if (remount)
  1799. continue;
  1800. sbinfo->uid = simple_strtoul(value, &rest, 0);
  1801. if (*rest)
  1802. goto bad_val;
  1803. } else if (!strcmp(this_char,"gid")) {
  1804. if (remount)
  1805. continue;
  1806. sbinfo->gid = simple_strtoul(value, &rest, 0);
  1807. if (*rest)
  1808. goto bad_val;
  1809. } else if (!strcmp(this_char,"mpol")) {
  1810. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  1811. goto bad_val;
  1812. } else {
  1813. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1814. this_char);
  1815. return 1;
  1816. }
  1817. }
  1818. return 0;
  1819. bad_val:
  1820. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1821. value, this_char);
  1822. return 1;
  1823. }
  1824. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1825. {
  1826. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1827. struct shmem_sb_info config = *sbinfo;
  1828. unsigned long inodes;
  1829. int error = -EINVAL;
  1830. if (shmem_parse_options(data, &config, true))
  1831. return error;
  1832. spin_lock(&sbinfo->stat_lock);
  1833. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1834. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  1835. goto out;
  1836. if (config.max_inodes < inodes)
  1837. goto out;
  1838. /*
  1839. * Those tests disallow limited->unlimited while any are in use;
  1840. * but we must separately disallow unlimited->limited, because
  1841. * in that case we have no record of how much is already in use.
  1842. */
  1843. if (config.max_blocks && !sbinfo->max_blocks)
  1844. goto out;
  1845. if (config.max_inodes && !sbinfo->max_inodes)
  1846. goto out;
  1847. error = 0;
  1848. sbinfo->max_blocks = config.max_blocks;
  1849. sbinfo->max_inodes = config.max_inodes;
  1850. sbinfo->free_inodes = config.max_inodes - inodes;
  1851. mpol_put(sbinfo->mpol);
  1852. sbinfo->mpol = config.mpol; /* transfers initial ref */
  1853. out:
  1854. spin_unlock(&sbinfo->stat_lock);
  1855. return error;
  1856. }
  1857. static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
  1858. {
  1859. struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
  1860. if (sbinfo->max_blocks != shmem_default_max_blocks())
  1861. seq_printf(seq, ",size=%luk",
  1862. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  1863. if (sbinfo->max_inodes != shmem_default_max_inodes())
  1864. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  1865. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  1866. seq_printf(seq, ",mode=%03o", sbinfo->mode);
  1867. if (sbinfo->uid != 0)
  1868. seq_printf(seq, ",uid=%u", sbinfo->uid);
  1869. if (sbinfo->gid != 0)
  1870. seq_printf(seq, ",gid=%u", sbinfo->gid);
  1871. shmem_show_mpol(seq, sbinfo->mpol);
  1872. return 0;
  1873. }
  1874. #endif /* CONFIG_TMPFS */
  1875. static void shmem_put_super(struct super_block *sb)
  1876. {
  1877. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1878. percpu_counter_destroy(&sbinfo->used_blocks);
  1879. kfree(sbinfo);
  1880. sb->s_fs_info = NULL;
  1881. }
  1882. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  1883. {
  1884. struct inode *inode;
  1885. struct dentry *root;
  1886. struct shmem_sb_info *sbinfo;
  1887. int err = -ENOMEM;
  1888. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1889. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  1890. L1_CACHE_BYTES), GFP_KERNEL);
  1891. if (!sbinfo)
  1892. return -ENOMEM;
  1893. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  1894. sbinfo->uid = current_fsuid();
  1895. sbinfo->gid = current_fsgid();
  1896. sb->s_fs_info = sbinfo;
  1897. #ifdef CONFIG_TMPFS
  1898. /*
  1899. * Per default we only allow half of the physical ram per
  1900. * tmpfs instance, limiting inodes to one per page of lowmem;
  1901. * but the internal instance is left unlimited.
  1902. */
  1903. if (!(sb->s_flags & MS_NOUSER)) {
  1904. sbinfo->max_blocks = shmem_default_max_blocks();
  1905. sbinfo->max_inodes = shmem_default_max_inodes();
  1906. if (shmem_parse_options(data, sbinfo, false)) {
  1907. err = -EINVAL;
  1908. goto failed;
  1909. }
  1910. }
  1911. sb->s_export_op = &shmem_export_ops;
  1912. #else
  1913. sb->s_flags |= MS_NOUSER;
  1914. #endif
  1915. spin_lock_init(&sbinfo->stat_lock);
  1916. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  1917. goto failed;
  1918. sbinfo->free_inodes = sbinfo->max_inodes;
  1919. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1920. sb->s_blocksize = PAGE_CACHE_SIZE;
  1921. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1922. sb->s_magic = TMPFS_MAGIC;
  1923. sb->s_op = &shmem_ops;
  1924. sb->s_time_gran = 1;
  1925. #ifdef CONFIG_TMPFS_XATTR
  1926. sb->s_xattr = shmem_xattr_handlers;
  1927. #endif
  1928. #ifdef CONFIG_TMPFS_POSIX_ACL
  1929. sb->s_flags |= MS_POSIXACL;
  1930. #endif
  1931. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  1932. if (!inode)
  1933. goto failed;
  1934. inode->i_uid = sbinfo->uid;
  1935. inode->i_gid = sbinfo->gid;
  1936. root = d_alloc_root(inode);
  1937. if (!root)
  1938. goto failed_iput;
  1939. sb->s_root = root;
  1940. return 0;
  1941. failed_iput:
  1942. iput(inode);
  1943. failed:
  1944. shmem_put_super(sb);
  1945. return err;
  1946. }
  1947. static struct kmem_cache *shmem_inode_cachep;
  1948. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1949. {
  1950. struct shmem_inode_info *info;
  1951. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  1952. if (!info)
  1953. return NULL;
  1954. return &info->vfs_inode;
  1955. }
  1956. static void shmem_destroy_callback(struct rcu_head *head)
  1957. {
  1958. struct inode *inode = container_of(head, struct inode, i_rcu);
  1959. INIT_LIST_HEAD(&inode->i_dentry);
  1960. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  1961. }
  1962. static void shmem_destroy_inode(struct inode *inode)
  1963. {
  1964. if ((inode->i_mode & S_IFMT) == S_IFREG)
  1965. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  1966. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  1967. }
  1968. static void shmem_init_inode(void *foo)
  1969. {
  1970. struct shmem_inode_info *info = foo;
  1971. inode_init_once(&info->vfs_inode);
  1972. }
  1973. static int shmem_init_inodecache(void)
  1974. {
  1975. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  1976. sizeof(struct shmem_inode_info),
  1977. 0, SLAB_PANIC, shmem_init_inode);
  1978. return 0;
  1979. }
  1980. static void shmem_destroy_inodecache(void)
  1981. {
  1982. kmem_cache_destroy(shmem_inode_cachep);
  1983. }
  1984. static const struct address_space_operations shmem_aops = {
  1985. .writepage = shmem_writepage,
  1986. .set_page_dirty = __set_page_dirty_no_writeback,
  1987. #ifdef CONFIG_TMPFS
  1988. .write_begin = shmem_write_begin,
  1989. .write_end = shmem_write_end,
  1990. #endif
  1991. .migratepage = migrate_page,
  1992. .error_remove_page = generic_error_remove_page,
  1993. };
  1994. static const struct file_operations shmem_file_operations = {
  1995. .mmap = shmem_mmap,
  1996. #ifdef CONFIG_TMPFS
  1997. .llseek = generic_file_llseek,
  1998. .read = do_sync_read,
  1999. .write = do_sync_write,
  2000. .aio_read = shmem_file_aio_read,
  2001. .aio_write = generic_file_aio_write,
  2002. .fsync = noop_fsync,
  2003. .splice_read = shmem_file_splice_read,
  2004. .splice_write = generic_file_splice_write,
  2005. #endif
  2006. };
  2007. static const struct inode_operations shmem_inode_operations = {
  2008. .setattr = shmem_setattr,
  2009. .truncate_range = shmem_truncate_range,
  2010. #ifdef CONFIG_TMPFS_XATTR
  2011. .setxattr = shmem_setxattr,
  2012. .getxattr = shmem_getxattr,
  2013. .listxattr = shmem_listxattr,
  2014. .removexattr = shmem_removexattr,
  2015. #endif
  2016. };
  2017. static const struct inode_operations shmem_dir_inode_operations = {
  2018. #ifdef CONFIG_TMPFS
  2019. .create = shmem_create,
  2020. .lookup = simple_lookup,
  2021. .link = shmem_link,
  2022. .unlink = shmem_unlink,
  2023. .symlink = shmem_symlink,
  2024. .mkdir = shmem_mkdir,
  2025. .rmdir = shmem_rmdir,
  2026. .mknod = shmem_mknod,
  2027. .rename = shmem_rename,
  2028. #endif
  2029. #ifdef CONFIG_TMPFS_XATTR
  2030. .setxattr = shmem_setxattr,
  2031. .getxattr = shmem_getxattr,
  2032. .listxattr = shmem_listxattr,
  2033. .removexattr = shmem_removexattr,
  2034. #endif
  2035. #ifdef CONFIG_TMPFS_POSIX_ACL
  2036. .setattr = shmem_setattr,
  2037. #endif
  2038. };
  2039. static const struct inode_operations shmem_special_inode_operations = {
  2040. #ifdef CONFIG_TMPFS_XATTR
  2041. .setxattr = shmem_setxattr,
  2042. .getxattr = shmem_getxattr,
  2043. .listxattr = shmem_listxattr,
  2044. .removexattr = shmem_removexattr,
  2045. #endif
  2046. #ifdef CONFIG_TMPFS_POSIX_ACL
  2047. .setattr = shmem_setattr,
  2048. #endif
  2049. };
  2050. static const struct super_operations shmem_ops = {
  2051. .alloc_inode = shmem_alloc_inode,
  2052. .destroy_inode = shmem_destroy_inode,
  2053. #ifdef CONFIG_TMPFS
  2054. .statfs = shmem_statfs,
  2055. .remount_fs = shmem_remount_fs,
  2056. .show_options = shmem_show_options,
  2057. #endif
  2058. .evict_inode = shmem_evict_inode,
  2059. .drop_inode = generic_delete_inode,
  2060. .put_super = shmem_put_super,
  2061. };
  2062. static const struct vm_operations_struct shmem_vm_ops = {
  2063. .fault = shmem_fault,
  2064. #ifdef CONFIG_NUMA
  2065. .set_policy = shmem_set_policy,
  2066. .get_policy = shmem_get_policy,
  2067. #endif
  2068. };
  2069. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2070. int flags, const char *dev_name, void *data)
  2071. {
  2072. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2073. }
  2074. static struct file_system_type shmem_fs_type = {
  2075. .owner = THIS_MODULE,
  2076. .name = "tmpfs",
  2077. .mount = shmem_mount,
  2078. .kill_sb = kill_litter_super,
  2079. };
  2080. int __init shmem_init(void)
  2081. {
  2082. int error;
  2083. error = bdi_init(&shmem_backing_dev_info);
  2084. if (error)
  2085. goto out4;
  2086. error = shmem_init_inodecache();
  2087. if (error)
  2088. goto out3;
  2089. error = register_filesystem(&shmem_fs_type);
  2090. if (error) {
  2091. printk(KERN_ERR "Could not register tmpfs\n");
  2092. goto out2;
  2093. }
  2094. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2095. shmem_fs_type.name, NULL);
  2096. if (IS_ERR(shm_mnt)) {
  2097. error = PTR_ERR(shm_mnt);
  2098. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2099. goto out1;
  2100. }
  2101. return 0;
  2102. out1:
  2103. unregister_filesystem(&shmem_fs_type);
  2104. out2:
  2105. shmem_destroy_inodecache();
  2106. out3:
  2107. bdi_destroy(&shmem_backing_dev_info);
  2108. out4:
  2109. shm_mnt = ERR_PTR(error);
  2110. return error;
  2111. }
  2112. #else /* !CONFIG_SHMEM */
  2113. /*
  2114. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2115. *
  2116. * This is intended for small system where the benefits of the full
  2117. * shmem code (swap-backed and resource-limited) are outweighed by
  2118. * their complexity. On systems without swap this code should be
  2119. * effectively equivalent, but much lighter weight.
  2120. */
  2121. #include <linux/ramfs.h>
  2122. static struct file_system_type shmem_fs_type = {
  2123. .name = "tmpfs",
  2124. .mount = ramfs_mount,
  2125. .kill_sb = kill_litter_super,
  2126. };
  2127. int __init shmem_init(void)
  2128. {
  2129. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2130. shm_mnt = kern_mount(&shmem_fs_type);
  2131. BUG_ON(IS_ERR(shm_mnt));
  2132. return 0;
  2133. }
  2134. int shmem_unuse(swp_entry_t swap, struct page *page)
  2135. {
  2136. return 0;
  2137. }
  2138. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2139. {
  2140. return 0;
  2141. }
  2142. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2143. {
  2144. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2145. }
  2146. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2147. #define shmem_vm_ops generic_file_vm_ops
  2148. #define shmem_file_operations ramfs_file_operations
  2149. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2150. #define shmem_acct_size(flags, size) 0
  2151. #define shmem_unacct_size(flags, size) do {} while (0)
  2152. #endif /* CONFIG_SHMEM */
  2153. /* common code */
  2154. /**
  2155. * shmem_file_setup - get an unlinked file living in tmpfs
  2156. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2157. * @size: size to be set for the file
  2158. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2159. */
  2160. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2161. {
  2162. int error;
  2163. struct file *file;
  2164. struct inode *inode;
  2165. struct path path;
  2166. struct dentry *root;
  2167. struct qstr this;
  2168. if (IS_ERR(shm_mnt))
  2169. return (void *)shm_mnt;
  2170. if (size < 0 || size > MAX_LFS_FILESIZE)
  2171. return ERR_PTR(-EINVAL);
  2172. if (shmem_acct_size(flags, size))
  2173. return ERR_PTR(-ENOMEM);
  2174. error = -ENOMEM;
  2175. this.name = name;
  2176. this.len = strlen(name);
  2177. this.hash = 0; /* will go */
  2178. root = shm_mnt->mnt_root;
  2179. path.dentry = d_alloc(root, &this);
  2180. if (!path.dentry)
  2181. goto put_memory;
  2182. path.mnt = mntget(shm_mnt);
  2183. error = -ENOSPC;
  2184. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2185. if (!inode)
  2186. goto put_dentry;
  2187. d_instantiate(path.dentry, inode);
  2188. inode->i_size = size;
  2189. inode->i_nlink = 0; /* It is unlinked */
  2190. #ifndef CONFIG_MMU
  2191. error = ramfs_nommu_expand_for_mapping(inode, size);
  2192. if (error)
  2193. goto put_dentry;
  2194. #endif
  2195. error = -ENFILE;
  2196. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2197. &shmem_file_operations);
  2198. if (!file)
  2199. goto put_dentry;
  2200. return file;
  2201. put_dentry:
  2202. path_put(&path);
  2203. put_memory:
  2204. shmem_unacct_size(flags, size);
  2205. return ERR_PTR(error);
  2206. }
  2207. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2208. /**
  2209. * shmem_zero_setup - setup a shared anonymous mapping
  2210. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2211. */
  2212. int shmem_zero_setup(struct vm_area_struct *vma)
  2213. {
  2214. struct file *file;
  2215. loff_t size = vma->vm_end - vma->vm_start;
  2216. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2217. if (IS_ERR(file))
  2218. return PTR_ERR(file);
  2219. if (vma->vm_file)
  2220. fput(vma->vm_file);
  2221. vma->vm_file = file;
  2222. vma->vm_ops = &shmem_vm_ops;
  2223. vma->vm_flags |= VM_CAN_NONLINEAR;
  2224. return 0;
  2225. }
  2226. /**
  2227. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2228. * @mapping: the page's address_space
  2229. * @index: the page index
  2230. * @gfp: the page allocator flags to use if allocating
  2231. *
  2232. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2233. * with any new page allocations done using the specified allocation flags.
  2234. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2235. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2236. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2237. *
  2238. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2239. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2240. */
  2241. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2242. pgoff_t index, gfp_t gfp)
  2243. {
  2244. #ifdef CONFIG_SHMEM
  2245. struct inode *inode = mapping->host;
  2246. struct page *page;
  2247. int error;
  2248. BUG_ON(mapping->a_ops != &shmem_aops);
  2249. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2250. if (error)
  2251. page = ERR_PTR(error);
  2252. else
  2253. unlock_page(page);
  2254. return page;
  2255. #else
  2256. /*
  2257. * The tiny !SHMEM case uses ramfs without swap
  2258. */
  2259. return read_cache_page_gfp(mapping, index, gfp);
  2260. #endif
  2261. }
  2262. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);