shmem.c 66 KB

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