shmem.c 78 KB

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