shmem.c 77 KB

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