shmem.c 77 KB

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