shmem.c 69 KB

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