shmem.c 73 KB

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