shmem.c 69 KB

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