shmem.c 77 KB

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