shmem.c 63 KB

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