shmem.c 65 KB

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