shmem.c 68 KB

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