shmem.c 65 KB

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