shmem.c 65 KB

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