shmem.c 63 KB

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