shmem.c 90 KB

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