shmem.c 74 KB

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