shmem.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2005 Hugh Dickins.
  10. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  11. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  12. *
  13. * Extended attribute support for tmpfs:
  14. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  15. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  16. *
  17. * This file is released under the GPL.
  18. */
  19. /*
  20. * This virtual memory filesystem is heavily based on the ramfs. It
  21. * extends ramfs by the ability to use swap and honor resource limits
  22. * which makes it a completely usable filesystem.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/xattr.h>
  28. #include <linux/exportfs.h>
  29. #include <linux/generic_acl.h>
  30. #include <linux/mm.h>
  31. #include <linux/mman.h>
  32. #include <linux/file.h>
  33. #include <linux/swap.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/string.h>
  36. #include <linux/slab.h>
  37. #include <linux/backing-dev.h>
  38. #include <linux/shmem_fs.h>
  39. #include <linux/mount.h>
  40. #include <linux/writeback.h>
  41. #include <linux/vfs.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/security.h>
  44. #include <linux/swapops.h>
  45. #include <linux/mempolicy.h>
  46. #include <linux/namei.h>
  47. #include <linux/ctype.h>
  48. #include <linux/migrate.h>
  49. #include <linux/highmem.h>
  50. #include <linux/backing-dev.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/div64.h>
  53. #include <asm/pgtable.h>
  54. /* This magic number is used in glibc for posix shared memory */
  55. #define TMPFS_MAGIC 0x01021994
  56. #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
  57. #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
  58. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  59. #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
  60. #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
  61. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  62. /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
  63. #define SHMEM_PAGEIN VM_READ
  64. #define SHMEM_TRUNCATE VM_WRITE
  65. /* Definition to limit shmem_truncate's steps between cond_rescheds */
  66. #define LATENCY_LIMIT 64
  67. /* Pretend that each entry is of this size in directory's i_size */
  68. #define BOGO_DIRENT_SIZE 20
  69. /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
  70. enum sgp_type {
  71. SGP_QUICK, /* don't try more than file page cache lookup */
  72. SGP_READ, /* don't exceed i_size, don't allocate page */
  73. SGP_CACHE, /* don't exceed i_size, may allocate page */
  74. SGP_WRITE, /* may exceed i_size, may allocate page */
  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. * __GFP_MOVABLE is masked out as swap vectors cannot move
  86. */
  87. return alloc_pages((gfp_mask & ~__GFP_MOVABLE) | __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. BUG_ON(page_mapped(page));
  843. mapping = page->mapping;
  844. index = page->index;
  845. inode = mapping->host;
  846. info = SHMEM_I(inode);
  847. if (info->flags & VM_LOCKED)
  848. goto redirty;
  849. swap = get_swap_page();
  850. if (!swap.val)
  851. goto redirty;
  852. spin_lock(&info->lock);
  853. shmem_recalc_inode(inode);
  854. if (index >= info->next_index) {
  855. BUG_ON(!(info->flags & SHMEM_TRUNCATE));
  856. goto unlock;
  857. }
  858. entry = shmem_swp_entry(info, index, NULL);
  859. BUG_ON(!entry);
  860. BUG_ON(entry->val);
  861. if (move_to_swap_cache(page, swap) == 0) {
  862. shmem_swp_set(info, entry, swap.val);
  863. shmem_swp_unmap(entry);
  864. spin_unlock(&info->lock);
  865. if (list_empty(&info->swaplist)) {
  866. spin_lock(&shmem_swaplist_lock);
  867. /* move instead of add in case we're racing */
  868. list_move_tail(&info->swaplist, &shmem_swaplist);
  869. spin_unlock(&shmem_swaplist_lock);
  870. }
  871. unlock_page(page);
  872. return 0;
  873. }
  874. shmem_swp_unmap(entry);
  875. unlock:
  876. spin_unlock(&info->lock);
  877. swap_free(swap);
  878. redirty:
  879. set_page_dirty(page);
  880. return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */
  881. }
  882. #ifdef CONFIG_NUMA
  883. static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
  884. {
  885. char *nodelist = strchr(value, ':');
  886. int err = 1;
  887. if (nodelist) {
  888. /* NUL-terminate policy string */
  889. *nodelist++ = '\0';
  890. if (nodelist_parse(nodelist, *policy_nodes))
  891. goto out;
  892. if (!nodes_subset(*policy_nodes, node_online_map))
  893. goto out;
  894. }
  895. if (!strcmp(value, "default")) {
  896. *policy = MPOL_DEFAULT;
  897. /* Don't allow a nodelist */
  898. if (!nodelist)
  899. err = 0;
  900. } else if (!strcmp(value, "prefer")) {
  901. *policy = MPOL_PREFERRED;
  902. /* Insist on a nodelist of one node only */
  903. if (nodelist) {
  904. char *rest = nodelist;
  905. while (isdigit(*rest))
  906. rest++;
  907. if (!*rest)
  908. err = 0;
  909. }
  910. } else if (!strcmp(value, "bind")) {
  911. *policy = MPOL_BIND;
  912. /* Insist on a nodelist */
  913. if (nodelist)
  914. err = 0;
  915. } else if (!strcmp(value, "interleave")) {
  916. *policy = MPOL_INTERLEAVE;
  917. /* Default to nodes online if no nodelist */
  918. if (!nodelist)
  919. *policy_nodes = node_online_map;
  920. err = 0;
  921. }
  922. out:
  923. /* Restore string for error message */
  924. if (nodelist)
  925. *--nodelist = ':';
  926. return err;
  927. }
  928. static struct page *shmem_swapin_async(struct shared_policy *p,
  929. swp_entry_t entry, unsigned long idx)
  930. {
  931. struct page *page;
  932. struct vm_area_struct pvma;
  933. /* Create a pseudo vma that just contains the policy */
  934. memset(&pvma, 0, sizeof(struct vm_area_struct));
  935. pvma.vm_end = PAGE_SIZE;
  936. pvma.vm_pgoff = idx;
  937. pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
  938. page = read_swap_cache_async(entry, &pvma, 0);
  939. mpol_free(pvma.vm_policy);
  940. return page;
  941. }
  942. struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry,
  943. unsigned long idx)
  944. {
  945. struct shared_policy *p = &info->policy;
  946. int i, num;
  947. struct page *page;
  948. unsigned long offset;
  949. num = valid_swaphandles(entry, &offset);
  950. for (i = 0; i < num; offset++, i++) {
  951. page = shmem_swapin_async(p,
  952. swp_entry(swp_type(entry), offset), idx);
  953. if (!page)
  954. break;
  955. page_cache_release(page);
  956. }
  957. lru_add_drain(); /* Push any new pages onto the LRU now */
  958. return shmem_swapin_async(p, entry, idx);
  959. }
  960. static struct page *
  961. shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
  962. unsigned long idx)
  963. {
  964. struct vm_area_struct pvma;
  965. struct page *page;
  966. memset(&pvma, 0, sizeof(struct vm_area_struct));
  967. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  968. pvma.vm_pgoff = idx;
  969. pvma.vm_end = PAGE_SIZE;
  970. page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
  971. mpol_free(pvma.vm_policy);
  972. return page;
  973. }
  974. #else
  975. static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
  976. {
  977. return 1;
  978. }
  979. static inline struct page *
  980. shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
  981. {
  982. swapin_readahead(entry, 0, NULL);
  983. return read_swap_cache_async(entry, NULL, 0);
  984. }
  985. static inline struct page *
  986. shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
  987. {
  988. return alloc_page(gfp | __GFP_ZERO);
  989. }
  990. #endif
  991. /*
  992. * shmem_getpage - either get the page from swap or allocate a new one
  993. *
  994. * If we allocate a new one we do not mark it dirty. That's up to the
  995. * vm. If we swap it in we mark it dirty since we also free the swap
  996. * entry since a page cannot live in both the swap and page cache
  997. */
  998. static int shmem_getpage(struct inode *inode, unsigned long idx,
  999. struct page **pagep, enum sgp_type sgp, int *type)
  1000. {
  1001. struct address_space *mapping = inode->i_mapping;
  1002. struct shmem_inode_info *info = SHMEM_I(inode);
  1003. struct shmem_sb_info *sbinfo;
  1004. struct page *filepage = *pagep;
  1005. struct page *swappage;
  1006. swp_entry_t *entry;
  1007. swp_entry_t swap;
  1008. int error;
  1009. if (idx >= SHMEM_MAX_INDEX)
  1010. return -EFBIG;
  1011. /*
  1012. * Normally, filepage is NULL on entry, and either found
  1013. * uptodate immediately, or allocated and zeroed, or read
  1014. * in under swappage, which is then assigned to filepage.
  1015. * But shmem_readpage and shmem_prepare_write pass in a locked
  1016. * filepage, which may be found not uptodate by other callers
  1017. * too, and may need to be copied from the swappage read in.
  1018. */
  1019. repeat:
  1020. if (!filepage)
  1021. filepage = find_lock_page(mapping, idx);
  1022. if (filepage && PageUptodate(filepage))
  1023. goto done;
  1024. error = 0;
  1025. if (sgp == SGP_QUICK)
  1026. goto failed;
  1027. spin_lock(&info->lock);
  1028. shmem_recalc_inode(inode);
  1029. entry = shmem_swp_alloc(info, idx, sgp);
  1030. if (IS_ERR(entry)) {
  1031. spin_unlock(&info->lock);
  1032. error = PTR_ERR(entry);
  1033. goto failed;
  1034. }
  1035. swap = *entry;
  1036. if (swap.val) {
  1037. /* Look it up and read it in.. */
  1038. swappage = lookup_swap_cache(swap);
  1039. if (!swappage) {
  1040. shmem_swp_unmap(entry);
  1041. /* here we actually do the io */
  1042. if (type && *type == VM_FAULT_MINOR) {
  1043. __count_vm_event(PGMAJFAULT);
  1044. *type = VM_FAULT_MAJOR;
  1045. }
  1046. spin_unlock(&info->lock);
  1047. swappage = shmem_swapin(info, swap, idx);
  1048. if (!swappage) {
  1049. spin_lock(&info->lock);
  1050. entry = shmem_swp_alloc(info, idx, sgp);
  1051. if (IS_ERR(entry))
  1052. error = PTR_ERR(entry);
  1053. else {
  1054. if (entry->val == swap.val)
  1055. error = -ENOMEM;
  1056. shmem_swp_unmap(entry);
  1057. }
  1058. spin_unlock(&info->lock);
  1059. if (error)
  1060. goto failed;
  1061. goto repeat;
  1062. }
  1063. wait_on_page_locked(swappage);
  1064. page_cache_release(swappage);
  1065. goto repeat;
  1066. }
  1067. /* We have to do this with page locked to prevent races */
  1068. if (TestSetPageLocked(swappage)) {
  1069. shmem_swp_unmap(entry);
  1070. spin_unlock(&info->lock);
  1071. wait_on_page_locked(swappage);
  1072. page_cache_release(swappage);
  1073. goto repeat;
  1074. }
  1075. if (PageWriteback(swappage)) {
  1076. shmem_swp_unmap(entry);
  1077. spin_unlock(&info->lock);
  1078. wait_on_page_writeback(swappage);
  1079. unlock_page(swappage);
  1080. page_cache_release(swappage);
  1081. goto repeat;
  1082. }
  1083. if (!PageUptodate(swappage)) {
  1084. shmem_swp_unmap(entry);
  1085. spin_unlock(&info->lock);
  1086. unlock_page(swappage);
  1087. page_cache_release(swappage);
  1088. error = -EIO;
  1089. goto failed;
  1090. }
  1091. if (filepage) {
  1092. shmem_swp_set(info, entry, 0);
  1093. shmem_swp_unmap(entry);
  1094. delete_from_swap_cache(swappage);
  1095. spin_unlock(&info->lock);
  1096. copy_highpage(filepage, swappage);
  1097. unlock_page(swappage);
  1098. page_cache_release(swappage);
  1099. flush_dcache_page(filepage);
  1100. SetPageUptodate(filepage);
  1101. set_page_dirty(filepage);
  1102. swap_free(swap);
  1103. } else if (!(error = move_from_swap_cache(
  1104. swappage, idx, mapping))) {
  1105. info->flags |= SHMEM_PAGEIN;
  1106. shmem_swp_set(info, entry, 0);
  1107. shmem_swp_unmap(entry);
  1108. spin_unlock(&info->lock);
  1109. filepage = swappage;
  1110. swap_free(swap);
  1111. } else {
  1112. shmem_swp_unmap(entry);
  1113. spin_unlock(&info->lock);
  1114. unlock_page(swappage);
  1115. page_cache_release(swappage);
  1116. if (error == -ENOMEM) {
  1117. /* let kswapd refresh zone for GFP_ATOMICs */
  1118. congestion_wait(WRITE, HZ/50);
  1119. }
  1120. goto repeat;
  1121. }
  1122. } else if (sgp == SGP_READ && !filepage) {
  1123. shmem_swp_unmap(entry);
  1124. filepage = find_get_page(mapping, idx);
  1125. if (filepage &&
  1126. (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
  1127. spin_unlock(&info->lock);
  1128. wait_on_page_locked(filepage);
  1129. page_cache_release(filepage);
  1130. filepage = NULL;
  1131. goto repeat;
  1132. }
  1133. spin_unlock(&info->lock);
  1134. } else {
  1135. shmem_swp_unmap(entry);
  1136. sbinfo = SHMEM_SB(inode->i_sb);
  1137. if (sbinfo->max_blocks) {
  1138. spin_lock(&sbinfo->stat_lock);
  1139. if (sbinfo->free_blocks == 0 ||
  1140. shmem_acct_block(info->flags)) {
  1141. spin_unlock(&sbinfo->stat_lock);
  1142. spin_unlock(&info->lock);
  1143. error = -ENOSPC;
  1144. goto failed;
  1145. }
  1146. sbinfo->free_blocks--;
  1147. inode->i_blocks += BLOCKS_PER_PAGE;
  1148. spin_unlock(&sbinfo->stat_lock);
  1149. } else if (shmem_acct_block(info->flags)) {
  1150. spin_unlock(&info->lock);
  1151. error = -ENOSPC;
  1152. goto failed;
  1153. }
  1154. if (!filepage) {
  1155. spin_unlock(&info->lock);
  1156. filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
  1157. info,
  1158. idx);
  1159. if (!filepage) {
  1160. shmem_unacct_blocks(info->flags, 1);
  1161. shmem_free_blocks(inode, 1);
  1162. error = -ENOMEM;
  1163. goto failed;
  1164. }
  1165. spin_lock(&info->lock);
  1166. entry = shmem_swp_alloc(info, idx, sgp);
  1167. if (IS_ERR(entry))
  1168. error = PTR_ERR(entry);
  1169. else {
  1170. swap = *entry;
  1171. shmem_swp_unmap(entry);
  1172. }
  1173. if (error || swap.val || 0 != add_to_page_cache_lru(
  1174. filepage, mapping, idx, GFP_ATOMIC)) {
  1175. spin_unlock(&info->lock);
  1176. page_cache_release(filepage);
  1177. shmem_unacct_blocks(info->flags, 1);
  1178. shmem_free_blocks(inode, 1);
  1179. filepage = NULL;
  1180. if (error)
  1181. goto failed;
  1182. goto repeat;
  1183. }
  1184. info->flags |= SHMEM_PAGEIN;
  1185. }
  1186. info->alloced++;
  1187. spin_unlock(&info->lock);
  1188. flush_dcache_page(filepage);
  1189. SetPageUptodate(filepage);
  1190. }
  1191. done:
  1192. if (*pagep != filepage) {
  1193. unlock_page(filepage);
  1194. *pagep = filepage;
  1195. }
  1196. return 0;
  1197. failed:
  1198. if (*pagep != filepage) {
  1199. unlock_page(filepage);
  1200. page_cache_release(filepage);
  1201. }
  1202. return error;
  1203. }
  1204. static struct page *shmem_nopage(struct vm_area_struct *vma,
  1205. unsigned long address, int *type)
  1206. {
  1207. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1208. struct page *page = NULL;
  1209. unsigned long idx;
  1210. int error;
  1211. idx = (address - vma->vm_start) >> PAGE_SHIFT;
  1212. idx += vma->vm_pgoff;
  1213. idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
  1214. if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1215. return NOPAGE_SIGBUS;
  1216. error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
  1217. if (error)
  1218. return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
  1219. mark_page_accessed(page);
  1220. return page;
  1221. }
  1222. static int shmem_populate(struct vm_area_struct *vma,
  1223. unsigned long addr, unsigned long len,
  1224. pgprot_t prot, unsigned long pgoff, int nonblock)
  1225. {
  1226. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1227. struct mm_struct *mm = vma->vm_mm;
  1228. enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
  1229. unsigned long size;
  1230. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1231. if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
  1232. return -EINVAL;
  1233. while ((long) len > 0) {
  1234. struct page *page = NULL;
  1235. int err;
  1236. /*
  1237. * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
  1238. */
  1239. err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
  1240. if (err)
  1241. return err;
  1242. /* Page may still be null, but only if nonblock was set. */
  1243. if (page) {
  1244. mark_page_accessed(page);
  1245. err = install_page(mm, vma, addr, page, prot);
  1246. if (err) {
  1247. page_cache_release(page);
  1248. return err;
  1249. }
  1250. } else if (vma->vm_flags & VM_NONLINEAR) {
  1251. /* No page was found just because we can't read it in
  1252. * now (being here implies nonblock != 0), but the page
  1253. * may exist, so set the PTE to fault it in later. */
  1254. err = install_file_pte(mm, vma, addr, pgoff, prot);
  1255. if (err)
  1256. return err;
  1257. }
  1258. len -= PAGE_SIZE;
  1259. addr += PAGE_SIZE;
  1260. pgoff++;
  1261. }
  1262. return 0;
  1263. }
  1264. #ifdef CONFIG_NUMA
  1265. int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1266. {
  1267. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1268. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1269. }
  1270. struct mempolicy *
  1271. shmem_get_policy(struct vm_area_struct *vma, unsigned long addr)
  1272. {
  1273. struct inode *i = vma->vm_file->f_path.dentry->d_inode;
  1274. unsigned long idx;
  1275. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1276. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1277. }
  1278. #endif
  1279. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1280. {
  1281. struct inode *inode = file->f_path.dentry->d_inode;
  1282. struct shmem_inode_info *info = SHMEM_I(inode);
  1283. int retval = -ENOMEM;
  1284. spin_lock(&info->lock);
  1285. if (lock && !(info->flags & VM_LOCKED)) {
  1286. if (!user_shm_lock(inode->i_size, user))
  1287. goto out_nomem;
  1288. info->flags |= VM_LOCKED;
  1289. }
  1290. if (!lock && (info->flags & VM_LOCKED) && user) {
  1291. user_shm_unlock(inode->i_size, user);
  1292. info->flags &= ~VM_LOCKED;
  1293. }
  1294. retval = 0;
  1295. out_nomem:
  1296. spin_unlock(&info->lock);
  1297. return retval;
  1298. }
  1299. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1300. {
  1301. file_accessed(file);
  1302. vma->vm_ops = &shmem_vm_ops;
  1303. return 0;
  1304. }
  1305. static struct inode *
  1306. shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
  1307. {
  1308. struct inode *inode;
  1309. struct shmem_inode_info *info;
  1310. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1311. if (sbinfo->max_inodes) {
  1312. spin_lock(&sbinfo->stat_lock);
  1313. if (!sbinfo->free_inodes) {
  1314. spin_unlock(&sbinfo->stat_lock);
  1315. return NULL;
  1316. }
  1317. sbinfo->free_inodes--;
  1318. spin_unlock(&sbinfo->stat_lock);
  1319. }
  1320. inode = new_inode(sb);
  1321. if (inode) {
  1322. inode->i_mode = mode;
  1323. inode->i_uid = current->fsuid;
  1324. inode->i_gid = current->fsgid;
  1325. inode->i_blocks = 0;
  1326. inode->i_mapping->a_ops = &shmem_aops;
  1327. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1328. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1329. inode->i_generation = get_seconds();
  1330. info = SHMEM_I(inode);
  1331. memset(info, 0, (char *)inode - (char *)info);
  1332. spin_lock_init(&info->lock);
  1333. INIT_LIST_HEAD(&info->swaplist);
  1334. switch (mode & S_IFMT) {
  1335. default:
  1336. inode->i_op = &shmem_special_inode_operations;
  1337. init_special_inode(inode, mode, dev);
  1338. break;
  1339. case S_IFREG:
  1340. inode->i_op = &shmem_inode_operations;
  1341. inode->i_fop = &shmem_file_operations;
  1342. mpol_shared_policy_init(&info->policy, sbinfo->policy,
  1343. &sbinfo->policy_nodes);
  1344. break;
  1345. case S_IFDIR:
  1346. inc_nlink(inode);
  1347. /* Some things misbehave if size == 0 on a directory */
  1348. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1349. inode->i_op = &shmem_dir_inode_operations;
  1350. inode->i_fop = &simple_dir_operations;
  1351. break;
  1352. case S_IFLNK:
  1353. /*
  1354. * Must not load anything in the rbtree,
  1355. * mpol_free_shared_policy will not be called.
  1356. */
  1357. mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
  1358. NULL);
  1359. break;
  1360. }
  1361. } else if (sbinfo->max_inodes) {
  1362. spin_lock(&sbinfo->stat_lock);
  1363. sbinfo->free_inodes++;
  1364. spin_unlock(&sbinfo->stat_lock);
  1365. }
  1366. return inode;
  1367. }
  1368. #ifdef CONFIG_TMPFS
  1369. static const struct inode_operations shmem_symlink_inode_operations;
  1370. static const struct inode_operations shmem_symlink_inline_operations;
  1371. /*
  1372. * Normally tmpfs avoids the use of shmem_readpage and shmem_prepare_write;
  1373. * but providing them allows a tmpfs file to be used for splice, sendfile, and
  1374. * below the loop driver, in the generic fashion that many filesystems support.
  1375. */
  1376. static int shmem_readpage(struct file *file, struct page *page)
  1377. {
  1378. struct inode *inode = page->mapping->host;
  1379. int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
  1380. unlock_page(page);
  1381. return error;
  1382. }
  1383. static int
  1384. shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  1385. {
  1386. struct inode *inode = page->mapping->host;
  1387. return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
  1388. }
  1389. static ssize_t
  1390. shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  1391. {
  1392. struct inode *inode = file->f_path.dentry->d_inode;
  1393. loff_t pos;
  1394. unsigned long written;
  1395. ssize_t err;
  1396. if ((ssize_t) count < 0)
  1397. return -EINVAL;
  1398. if (!access_ok(VERIFY_READ, buf, count))
  1399. return -EFAULT;
  1400. mutex_lock(&inode->i_mutex);
  1401. pos = *ppos;
  1402. written = 0;
  1403. err = generic_write_checks(file, &pos, &count, 0);
  1404. if (err || !count)
  1405. goto out;
  1406. err = remove_suid(file->f_path.dentry);
  1407. if (err)
  1408. goto out;
  1409. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1410. do {
  1411. struct page *page = NULL;
  1412. unsigned long bytes, index, offset;
  1413. char *kaddr;
  1414. int left;
  1415. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1416. index = pos >> PAGE_CACHE_SHIFT;
  1417. bytes = PAGE_CACHE_SIZE - offset;
  1418. if (bytes > count)
  1419. bytes = count;
  1420. /*
  1421. * We don't hold page lock across copy from user -
  1422. * what would it guard against? - so no deadlock here.
  1423. * But it still may be a good idea to prefault below.
  1424. */
  1425. err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
  1426. if (err)
  1427. break;
  1428. left = bytes;
  1429. if (PageHighMem(page)) {
  1430. volatile unsigned char dummy;
  1431. __get_user(dummy, buf);
  1432. __get_user(dummy, buf + bytes - 1);
  1433. kaddr = kmap_atomic(page, KM_USER0);
  1434. left = __copy_from_user_inatomic(kaddr + offset,
  1435. buf, bytes);
  1436. kunmap_atomic(kaddr, KM_USER0);
  1437. }
  1438. if (left) {
  1439. kaddr = kmap(page);
  1440. left = __copy_from_user(kaddr + offset, buf, bytes);
  1441. kunmap(page);
  1442. }
  1443. written += bytes;
  1444. count -= bytes;
  1445. pos += bytes;
  1446. buf += bytes;
  1447. if (pos > inode->i_size)
  1448. i_size_write(inode, pos);
  1449. flush_dcache_page(page);
  1450. set_page_dirty(page);
  1451. mark_page_accessed(page);
  1452. page_cache_release(page);
  1453. if (left) {
  1454. pos -= left;
  1455. written -= left;
  1456. err = -EFAULT;
  1457. break;
  1458. }
  1459. /*
  1460. * Our dirty pages are not counted in nr_dirty,
  1461. * and we do not attempt to balance dirty pages.
  1462. */
  1463. cond_resched();
  1464. } while (count);
  1465. *ppos = pos;
  1466. if (written)
  1467. err = written;
  1468. out:
  1469. mutex_unlock(&inode->i_mutex);
  1470. return err;
  1471. }
  1472. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1473. {
  1474. struct inode *inode = filp->f_path.dentry->d_inode;
  1475. struct address_space *mapping = inode->i_mapping;
  1476. unsigned long index, offset;
  1477. index = *ppos >> PAGE_CACHE_SHIFT;
  1478. offset = *ppos & ~PAGE_CACHE_MASK;
  1479. for (;;) {
  1480. struct page *page = NULL;
  1481. unsigned long end_index, nr, ret;
  1482. loff_t i_size = i_size_read(inode);
  1483. end_index = i_size >> PAGE_CACHE_SHIFT;
  1484. if (index > end_index)
  1485. break;
  1486. if (index == end_index) {
  1487. nr = i_size & ~PAGE_CACHE_MASK;
  1488. if (nr <= offset)
  1489. break;
  1490. }
  1491. desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
  1492. if (desc->error) {
  1493. if (desc->error == -EINVAL)
  1494. desc->error = 0;
  1495. break;
  1496. }
  1497. /*
  1498. * We must evaluate after, since reads (unlike writes)
  1499. * are called without i_mutex protection against truncate
  1500. */
  1501. nr = PAGE_CACHE_SIZE;
  1502. i_size = i_size_read(inode);
  1503. end_index = i_size >> PAGE_CACHE_SHIFT;
  1504. if (index == end_index) {
  1505. nr = i_size & ~PAGE_CACHE_MASK;
  1506. if (nr <= offset) {
  1507. if (page)
  1508. page_cache_release(page);
  1509. break;
  1510. }
  1511. }
  1512. nr -= offset;
  1513. if (page) {
  1514. /*
  1515. * If users can be writing to this page using arbitrary
  1516. * virtual addresses, take care about potential aliasing
  1517. * before reading the page on the kernel side.
  1518. */
  1519. if (mapping_writably_mapped(mapping))
  1520. flush_dcache_page(page);
  1521. /*
  1522. * Mark the page accessed if we read the beginning.
  1523. */
  1524. if (!offset)
  1525. mark_page_accessed(page);
  1526. } else {
  1527. page = ZERO_PAGE(0);
  1528. page_cache_get(page);
  1529. }
  1530. /*
  1531. * Ok, we have the page, and it's up-to-date, so
  1532. * now we can copy it to user space...
  1533. *
  1534. * The actor routine returns how many bytes were actually used..
  1535. * NOTE! This may not be the same as how much of a user buffer
  1536. * we filled up (we may be padding etc), so we can only update
  1537. * "pos" here (the actor routine has to update the user buffer
  1538. * pointers and the remaining count).
  1539. */
  1540. ret = actor(desc, page, offset, nr);
  1541. offset += ret;
  1542. index += offset >> PAGE_CACHE_SHIFT;
  1543. offset &= ~PAGE_CACHE_MASK;
  1544. page_cache_release(page);
  1545. if (ret != nr || !desc->count)
  1546. break;
  1547. cond_resched();
  1548. }
  1549. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1550. file_accessed(filp);
  1551. }
  1552. static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  1553. {
  1554. read_descriptor_t desc;
  1555. if ((ssize_t) count < 0)
  1556. return -EINVAL;
  1557. if (!access_ok(VERIFY_WRITE, buf, count))
  1558. return -EFAULT;
  1559. if (!count)
  1560. return 0;
  1561. desc.written = 0;
  1562. desc.count = count;
  1563. desc.arg.buf = buf;
  1564. desc.error = 0;
  1565. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1566. if (desc.written)
  1567. return desc.written;
  1568. return desc.error;
  1569. }
  1570. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1571. {
  1572. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1573. buf->f_type = TMPFS_MAGIC;
  1574. buf->f_bsize = PAGE_CACHE_SIZE;
  1575. buf->f_namelen = NAME_MAX;
  1576. spin_lock(&sbinfo->stat_lock);
  1577. if (sbinfo->max_blocks) {
  1578. buf->f_blocks = sbinfo->max_blocks;
  1579. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  1580. }
  1581. if (sbinfo->max_inodes) {
  1582. buf->f_files = sbinfo->max_inodes;
  1583. buf->f_ffree = sbinfo->free_inodes;
  1584. }
  1585. /* else leave those fields 0 like simple_statfs */
  1586. spin_unlock(&sbinfo->stat_lock);
  1587. return 0;
  1588. }
  1589. /*
  1590. * File creation. Allocate an inode, and we're done..
  1591. */
  1592. static int
  1593. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1594. {
  1595. struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
  1596. int error = -ENOSPC;
  1597. if (inode) {
  1598. error = security_inode_init_security(inode, dir, NULL, NULL,
  1599. NULL);
  1600. if (error) {
  1601. if (error != -EOPNOTSUPP) {
  1602. iput(inode);
  1603. return error;
  1604. }
  1605. }
  1606. error = shmem_acl_init(inode, dir);
  1607. if (error) {
  1608. iput(inode);
  1609. return error;
  1610. }
  1611. if (dir->i_mode & S_ISGID) {
  1612. inode->i_gid = dir->i_gid;
  1613. if (S_ISDIR(mode))
  1614. inode->i_mode |= S_ISGID;
  1615. }
  1616. dir->i_size += BOGO_DIRENT_SIZE;
  1617. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1618. d_instantiate(dentry, inode);
  1619. dget(dentry); /* Extra count - pin the dentry in core */
  1620. }
  1621. return error;
  1622. }
  1623. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1624. {
  1625. int error;
  1626. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1627. return error;
  1628. inc_nlink(dir);
  1629. return 0;
  1630. }
  1631. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1632. struct nameidata *nd)
  1633. {
  1634. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1635. }
  1636. /*
  1637. * Link a file..
  1638. */
  1639. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1640. {
  1641. struct inode *inode = old_dentry->d_inode;
  1642. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1643. /*
  1644. * No ordinary (disk based) filesystem counts links as inodes;
  1645. * but each new link needs a new dentry, pinning lowmem, and
  1646. * tmpfs dentries cannot be pruned until they are unlinked.
  1647. */
  1648. if (sbinfo->max_inodes) {
  1649. spin_lock(&sbinfo->stat_lock);
  1650. if (!sbinfo->free_inodes) {
  1651. spin_unlock(&sbinfo->stat_lock);
  1652. return -ENOSPC;
  1653. }
  1654. sbinfo->free_inodes--;
  1655. spin_unlock(&sbinfo->stat_lock);
  1656. }
  1657. dir->i_size += BOGO_DIRENT_SIZE;
  1658. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1659. inc_nlink(inode);
  1660. atomic_inc(&inode->i_count); /* New dentry reference */
  1661. dget(dentry); /* Extra pinning count for the created dentry */
  1662. d_instantiate(dentry, inode);
  1663. return 0;
  1664. }
  1665. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1666. {
  1667. struct inode *inode = dentry->d_inode;
  1668. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
  1669. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1670. if (sbinfo->max_inodes) {
  1671. spin_lock(&sbinfo->stat_lock);
  1672. sbinfo->free_inodes++;
  1673. spin_unlock(&sbinfo->stat_lock);
  1674. }
  1675. }
  1676. dir->i_size -= BOGO_DIRENT_SIZE;
  1677. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1678. drop_nlink(inode);
  1679. dput(dentry); /* Undo the count from "create" - this does all the work */
  1680. return 0;
  1681. }
  1682. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1683. {
  1684. if (!simple_empty(dentry))
  1685. return -ENOTEMPTY;
  1686. drop_nlink(dentry->d_inode);
  1687. drop_nlink(dir);
  1688. return shmem_unlink(dir, dentry);
  1689. }
  1690. /*
  1691. * The VFS layer already does all the dentry stuff for rename,
  1692. * we just have to decrement the usage count for the target if
  1693. * it exists so that the VFS layer correctly free's it when it
  1694. * gets overwritten.
  1695. */
  1696. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1697. {
  1698. struct inode *inode = old_dentry->d_inode;
  1699. int they_are_dirs = S_ISDIR(inode->i_mode);
  1700. if (!simple_empty(new_dentry))
  1701. return -ENOTEMPTY;
  1702. if (new_dentry->d_inode) {
  1703. (void) shmem_unlink(new_dir, new_dentry);
  1704. if (they_are_dirs)
  1705. drop_nlink(old_dir);
  1706. } else if (they_are_dirs) {
  1707. drop_nlink(old_dir);
  1708. inc_nlink(new_dir);
  1709. }
  1710. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1711. new_dir->i_size += BOGO_DIRENT_SIZE;
  1712. old_dir->i_ctime = old_dir->i_mtime =
  1713. new_dir->i_ctime = new_dir->i_mtime =
  1714. inode->i_ctime = CURRENT_TIME;
  1715. return 0;
  1716. }
  1717. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1718. {
  1719. int error;
  1720. int len;
  1721. struct inode *inode;
  1722. struct page *page = NULL;
  1723. char *kaddr;
  1724. struct shmem_inode_info *info;
  1725. len = strlen(symname) + 1;
  1726. if (len > PAGE_CACHE_SIZE)
  1727. return -ENAMETOOLONG;
  1728. inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  1729. if (!inode)
  1730. return -ENOSPC;
  1731. error = security_inode_init_security(inode, dir, NULL, NULL,
  1732. NULL);
  1733. if (error) {
  1734. if (error != -EOPNOTSUPP) {
  1735. iput(inode);
  1736. return error;
  1737. }
  1738. error = 0;
  1739. }
  1740. info = SHMEM_I(inode);
  1741. inode->i_size = len-1;
  1742. if (len <= (char *)inode - (char *)info) {
  1743. /* do it inline */
  1744. memcpy(info, symname, len);
  1745. inode->i_op = &shmem_symlink_inline_operations;
  1746. } else {
  1747. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1748. if (error) {
  1749. iput(inode);
  1750. return error;
  1751. }
  1752. inode->i_op = &shmem_symlink_inode_operations;
  1753. kaddr = kmap_atomic(page, KM_USER0);
  1754. memcpy(kaddr, symname, len);
  1755. kunmap_atomic(kaddr, KM_USER0);
  1756. set_page_dirty(page);
  1757. page_cache_release(page);
  1758. }
  1759. if (dir->i_mode & S_ISGID)
  1760. inode->i_gid = dir->i_gid;
  1761. dir->i_size += BOGO_DIRENT_SIZE;
  1762. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1763. d_instantiate(dentry, inode);
  1764. dget(dentry);
  1765. return 0;
  1766. }
  1767. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1768. {
  1769. nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
  1770. return NULL;
  1771. }
  1772. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1773. {
  1774. struct page *page = NULL;
  1775. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1776. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1777. return page;
  1778. }
  1779. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1780. {
  1781. if (!IS_ERR(nd_get_link(nd))) {
  1782. struct page *page = cookie;
  1783. kunmap(page);
  1784. mark_page_accessed(page);
  1785. page_cache_release(page);
  1786. }
  1787. }
  1788. static const struct inode_operations shmem_symlink_inline_operations = {
  1789. .readlink = generic_readlink,
  1790. .follow_link = shmem_follow_link_inline,
  1791. };
  1792. static const struct inode_operations shmem_symlink_inode_operations = {
  1793. .truncate = shmem_truncate,
  1794. .readlink = generic_readlink,
  1795. .follow_link = shmem_follow_link,
  1796. .put_link = shmem_put_link,
  1797. };
  1798. #ifdef CONFIG_TMPFS_POSIX_ACL
  1799. /**
  1800. * Superblocks without xattr inode operations will get security.* xattr
  1801. * support from the VFS "for free". As soon as we have any other xattrs
  1802. * like ACLs, we also need to implement the security.* handlers at
  1803. * filesystem level, though.
  1804. */
  1805. static size_t shmem_xattr_security_list(struct inode *inode, char *list,
  1806. size_t list_len, const char *name,
  1807. size_t name_len)
  1808. {
  1809. return security_inode_listsecurity(inode, list, list_len);
  1810. }
  1811. static int shmem_xattr_security_get(struct inode *inode, const char *name,
  1812. void *buffer, size_t size)
  1813. {
  1814. if (strcmp(name, "") == 0)
  1815. return -EINVAL;
  1816. return security_inode_getsecurity(inode, name, buffer, size,
  1817. -EOPNOTSUPP);
  1818. }
  1819. static int shmem_xattr_security_set(struct inode *inode, const char *name,
  1820. const void *value, size_t size, int flags)
  1821. {
  1822. if (strcmp(name, "") == 0)
  1823. return -EINVAL;
  1824. return security_inode_setsecurity(inode, name, value, size, flags);
  1825. }
  1826. static struct xattr_handler shmem_xattr_security_handler = {
  1827. .prefix = XATTR_SECURITY_PREFIX,
  1828. .list = shmem_xattr_security_list,
  1829. .get = shmem_xattr_security_get,
  1830. .set = shmem_xattr_security_set,
  1831. };
  1832. static struct xattr_handler *shmem_xattr_handlers[] = {
  1833. &shmem_xattr_acl_access_handler,
  1834. &shmem_xattr_acl_default_handler,
  1835. &shmem_xattr_security_handler,
  1836. NULL
  1837. };
  1838. #endif
  1839. static struct dentry *shmem_get_parent(struct dentry *child)
  1840. {
  1841. return ERR_PTR(-ESTALE);
  1842. }
  1843. static int shmem_match(struct inode *ino, void *vfh)
  1844. {
  1845. __u32 *fh = vfh;
  1846. __u64 inum = fh[2];
  1847. inum = (inum << 32) | fh[1];
  1848. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1849. }
  1850. static struct dentry *shmem_get_dentry(struct super_block *sb, void *vfh)
  1851. {
  1852. struct dentry *de = NULL;
  1853. struct inode *inode;
  1854. __u32 *fh = vfh;
  1855. __u64 inum = fh[2];
  1856. inum = (inum << 32) | fh[1];
  1857. inode = ilookup5(sb, (unsigned long)(inum+fh[0]), shmem_match, vfh);
  1858. if (inode) {
  1859. de = d_find_alias(inode);
  1860. iput(inode);
  1861. }
  1862. return de? de: ERR_PTR(-ESTALE);
  1863. }
  1864. static struct dentry *shmem_decode_fh(struct super_block *sb, __u32 *fh,
  1865. int len, int type,
  1866. int (*acceptable)(void *context, struct dentry *de),
  1867. void *context)
  1868. {
  1869. if (len < 3)
  1870. return ERR_PTR(-ESTALE);
  1871. return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable,
  1872. context);
  1873. }
  1874. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1875. int connectable)
  1876. {
  1877. struct inode *inode = dentry->d_inode;
  1878. if (*len < 3)
  1879. return 255;
  1880. if (hlist_unhashed(&inode->i_hash)) {
  1881. /* Unfortunately insert_inode_hash is not idempotent,
  1882. * so as we hash inodes here rather than at creation
  1883. * time, we need a lock to ensure we only try
  1884. * to do it once
  1885. */
  1886. static DEFINE_SPINLOCK(lock);
  1887. spin_lock(&lock);
  1888. if (hlist_unhashed(&inode->i_hash))
  1889. __insert_inode_hash(inode,
  1890. inode->i_ino + inode->i_generation);
  1891. spin_unlock(&lock);
  1892. }
  1893. fh[0] = inode->i_generation;
  1894. fh[1] = inode->i_ino;
  1895. fh[2] = ((__u64)inode->i_ino) >> 32;
  1896. *len = 3;
  1897. return 1;
  1898. }
  1899. static struct export_operations shmem_export_ops = {
  1900. .get_parent = shmem_get_parent,
  1901. .get_dentry = shmem_get_dentry,
  1902. .encode_fh = shmem_encode_fh,
  1903. .decode_fh = shmem_decode_fh,
  1904. };
  1905. static int shmem_parse_options(char *options, int *mode, uid_t *uid,
  1906. gid_t *gid, unsigned long *blocks, unsigned long *inodes,
  1907. int *policy, nodemask_t *policy_nodes)
  1908. {
  1909. char *this_char, *value, *rest;
  1910. while (options != NULL) {
  1911. this_char = options;
  1912. for (;;) {
  1913. /*
  1914. * NUL-terminate this option: unfortunately,
  1915. * mount options form a comma-separated list,
  1916. * but mpol's nodelist may also contain commas.
  1917. */
  1918. options = strchr(options, ',');
  1919. if (options == NULL)
  1920. break;
  1921. options++;
  1922. if (!isdigit(*options)) {
  1923. options[-1] = '\0';
  1924. break;
  1925. }
  1926. }
  1927. if (!*this_char)
  1928. continue;
  1929. if ((value = strchr(this_char,'=')) != NULL) {
  1930. *value++ = 0;
  1931. } else {
  1932. printk(KERN_ERR
  1933. "tmpfs: No value for mount option '%s'\n",
  1934. this_char);
  1935. return 1;
  1936. }
  1937. if (!strcmp(this_char,"size")) {
  1938. unsigned long long size;
  1939. size = memparse(value,&rest);
  1940. if (*rest == '%') {
  1941. size <<= PAGE_SHIFT;
  1942. size *= totalram_pages;
  1943. do_div(size, 100);
  1944. rest++;
  1945. }
  1946. if (*rest)
  1947. goto bad_val;
  1948. *blocks = size >> PAGE_CACHE_SHIFT;
  1949. } else if (!strcmp(this_char,"nr_blocks")) {
  1950. *blocks = memparse(value,&rest);
  1951. if (*rest)
  1952. goto bad_val;
  1953. } else if (!strcmp(this_char,"nr_inodes")) {
  1954. *inodes = memparse(value,&rest);
  1955. if (*rest)
  1956. goto bad_val;
  1957. } else if (!strcmp(this_char,"mode")) {
  1958. if (!mode)
  1959. continue;
  1960. *mode = simple_strtoul(value,&rest,8);
  1961. if (*rest)
  1962. goto bad_val;
  1963. } else if (!strcmp(this_char,"uid")) {
  1964. if (!uid)
  1965. continue;
  1966. *uid = simple_strtoul(value,&rest,0);
  1967. if (*rest)
  1968. goto bad_val;
  1969. } else if (!strcmp(this_char,"gid")) {
  1970. if (!gid)
  1971. continue;
  1972. *gid = simple_strtoul(value,&rest,0);
  1973. if (*rest)
  1974. goto bad_val;
  1975. } else if (!strcmp(this_char,"mpol")) {
  1976. if (shmem_parse_mpol(value,policy,policy_nodes))
  1977. goto bad_val;
  1978. } else {
  1979. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1980. this_char);
  1981. return 1;
  1982. }
  1983. }
  1984. return 0;
  1985. bad_val:
  1986. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1987. value, this_char);
  1988. return 1;
  1989. }
  1990. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1991. {
  1992. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1993. unsigned long max_blocks = sbinfo->max_blocks;
  1994. unsigned long max_inodes = sbinfo->max_inodes;
  1995. int policy = sbinfo->policy;
  1996. nodemask_t policy_nodes = sbinfo->policy_nodes;
  1997. unsigned long blocks;
  1998. unsigned long inodes;
  1999. int error = -EINVAL;
  2000. if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
  2001. &max_inodes, &policy, &policy_nodes))
  2002. return error;
  2003. spin_lock(&sbinfo->stat_lock);
  2004. blocks = sbinfo->max_blocks - sbinfo->free_blocks;
  2005. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2006. if (max_blocks < blocks)
  2007. goto out;
  2008. if (max_inodes < inodes)
  2009. goto out;
  2010. /*
  2011. * Those tests also disallow limited->unlimited while any are in
  2012. * use, so i_blocks will always be zero when max_blocks is zero;
  2013. * but we must separately disallow unlimited->limited, because
  2014. * in that case we have no record of how much is already in use.
  2015. */
  2016. if (max_blocks && !sbinfo->max_blocks)
  2017. goto out;
  2018. if (max_inodes && !sbinfo->max_inodes)
  2019. goto out;
  2020. error = 0;
  2021. sbinfo->max_blocks = max_blocks;
  2022. sbinfo->free_blocks = max_blocks - blocks;
  2023. sbinfo->max_inodes = max_inodes;
  2024. sbinfo->free_inodes = max_inodes - inodes;
  2025. sbinfo->policy = policy;
  2026. sbinfo->policy_nodes = policy_nodes;
  2027. out:
  2028. spin_unlock(&sbinfo->stat_lock);
  2029. return error;
  2030. }
  2031. #endif
  2032. static void shmem_put_super(struct super_block *sb)
  2033. {
  2034. kfree(sb->s_fs_info);
  2035. sb->s_fs_info = NULL;
  2036. }
  2037. static int shmem_fill_super(struct super_block *sb,
  2038. void *data, int silent)
  2039. {
  2040. struct inode *inode;
  2041. struct dentry *root;
  2042. int mode = S_IRWXUGO | S_ISVTX;
  2043. uid_t uid = current->fsuid;
  2044. gid_t gid = current->fsgid;
  2045. int err = -ENOMEM;
  2046. struct shmem_sb_info *sbinfo;
  2047. unsigned long blocks = 0;
  2048. unsigned long inodes = 0;
  2049. int policy = MPOL_DEFAULT;
  2050. nodemask_t policy_nodes = node_online_map;
  2051. #ifdef CONFIG_TMPFS
  2052. /*
  2053. * Per default we only allow half of the physical ram per
  2054. * tmpfs instance, limiting inodes to one per page of lowmem;
  2055. * but the internal instance is left unlimited.
  2056. */
  2057. if (!(sb->s_flags & MS_NOUSER)) {
  2058. blocks = totalram_pages / 2;
  2059. inodes = totalram_pages - totalhigh_pages;
  2060. if (inodes > blocks)
  2061. inodes = blocks;
  2062. if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
  2063. &inodes, &policy, &policy_nodes))
  2064. return -EINVAL;
  2065. }
  2066. sb->s_export_op = &shmem_export_ops;
  2067. #else
  2068. sb->s_flags |= MS_NOUSER;
  2069. #endif
  2070. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2071. sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
  2072. L1_CACHE_BYTES), GFP_KERNEL);
  2073. if (!sbinfo)
  2074. return -ENOMEM;
  2075. spin_lock_init(&sbinfo->stat_lock);
  2076. sbinfo->max_blocks = blocks;
  2077. sbinfo->free_blocks = blocks;
  2078. sbinfo->max_inodes = inodes;
  2079. sbinfo->free_inodes = inodes;
  2080. sbinfo->policy = policy;
  2081. sbinfo->policy_nodes = policy_nodes;
  2082. sb->s_fs_info = sbinfo;
  2083. sb->s_maxbytes = SHMEM_MAX_BYTES;
  2084. sb->s_blocksize = PAGE_CACHE_SIZE;
  2085. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2086. sb->s_magic = TMPFS_MAGIC;
  2087. sb->s_op = &shmem_ops;
  2088. sb->s_time_gran = 1;
  2089. #ifdef CONFIG_TMPFS_POSIX_ACL
  2090. sb->s_xattr = shmem_xattr_handlers;
  2091. sb->s_flags |= MS_POSIXACL;
  2092. #endif
  2093. inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
  2094. if (!inode)
  2095. goto failed;
  2096. inode->i_uid = uid;
  2097. inode->i_gid = gid;
  2098. root = d_alloc_root(inode);
  2099. if (!root)
  2100. goto failed_iput;
  2101. sb->s_root = root;
  2102. return 0;
  2103. failed_iput:
  2104. iput(inode);
  2105. failed:
  2106. shmem_put_super(sb);
  2107. return err;
  2108. }
  2109. static struct kmem_cache *shmem_inode_cachep;
  2110. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2111. {
  2112. struct shmem_inode_info *p;
  2113. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2114. if (!p)
  2115. return NULL;
  2116. return &p->vfs_inode;
  2117. }
  2118. static void shmem_destroy_inode(struct inode *inode)
  2119. {
  2120. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  2121. /* only struct inode is valid if it's an inline symlink */
  2122. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2123. }
  2124. shmem_acl_destroy_inode(inode);
  2125. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2126. }
  2127. static void init_once(void *foo, struct kmem_cache *cachep,
  2128. unsigned long flags)
  2129. {
  2130. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  2131. inode_init_once(&p->vfs_inode);
  2132. #ifdef CONFIG_TMPFS_POSIX_ACL
  2133. p->i_acl = NULL;
  2134. p->i_default_acl = NULL;
  2135. #endif
  2136. }
  2137. static int init_inodecache(void)
  2138. {
  2139. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2140. sizeof(struct shmem_inode_info),
  2141. 0, 0, init_once, NULL);
  2142. if (shmem_inode_cachep == NULL)
  2143. return -ENOMEM;
  2144. return 0;
  2145. }
  2146. static void destroy_inodecache(void)
  2147. {
  2148. kmem_cache_destroy(shmem_inode_cachep);
  2149. }
  2150. static const struct address_space_operations shmem_aops = {
  2151. .writepage = shmem_writepage,
  2152. .set_page_dirty = __set_page_dirty_no_writeback,
  2153. #ifdef CONFIG_TMPFS
  2154. .readpage = shmem_readpage,
  2155. .prepare_write = shmem_prepare_write,
  2156. .commit_write = simple_commit_write,
  2157. #endif
  2158. .migratepage = migrate_page,
  2159. };
  2160. static const struct file_operations shmem_file_operations = {
  2161. .mmap = shmem_mmap,
  2162. #ifdef CONFIG_TMPFS
  2163. .llseek = generic_file_llseek,
  2164. .read = shmem_file_read,
  2165. .write = shmem_file_write,
  2166. .fsync = simple_sync_file,
  2167. .splice_read = generic_file_splice_read,
  2168. .splice_write = generic_file_splice_write,
  2169. #endif
  2170. };
  2171. static const struct inode_operations shmem_inode_operations = {
  2172. .truncate = shmem_truncate,
  2173. .setattr = shmem_notify_change,
  2174. .truncate_range = shmem_truncate_range,
  2175. #ifdef CONFIG_TMPFS_POSIX_ACL
  2176. .setxattr = generic_setxattr,
  2177. .getxattr = generic_getxattr,
  2178. .listxattr = generic_listxattr,
  2179. .removexattr = generic_removexattr,
  2180. .permission = shmem_permission,
  2181. #endif
  2182. };
  2183. static const struct inode_operations shmem_dir_inode_operations = {
  2184. #ifdef CONFIG_TMPFS
  2185. .create = shmem_create,
  2186. .lookup = simple_lookup,
  2187. .link = shmem_link,
  2188. .unlink = shmem_unlink,
  2189. .symlink = shmem_symlink,
  2190. .mkdir = shmem_mkdir,
  2191. .rmdir = shmem_rmdir,
  2192. .mknod = shmem_mknod,
  2193. .rename = shmem_rename,
  2194. #endif
  2195. #ifdef CONFIG_TMPFS_POSIX_ACL
  2196. .setattr = shmem_notify_change,
  2197. .setxattr = generic_setxattr,
  2198. .getxattr = generic_getxattr,
  2199. .listxattr = generic_listxattr,
  2200. .removexattr = generic_removexattr,
  2201. .permission = shmem_permission,
  2202. #endif
  2203. };
  2204. static const struct inode_operations shmem_special_inode_operations = {
  2205. #ifdef CONFIG_TMPFS_POSIX_ACL
  2206. .setattr = shmem_notify_change,
  2207. .setxattr = generic_setxattr,
  2208. .getxattr = generic_getxattr,
  2209. .listxattr = generic_listxattr,
  2210. .removexattr = generic_removexattr,
  2211. .permission = shmem_permission,
  2212. #endif
  2213. };
  2214. static const struct super_operations shmem_ops = {
  2215. .alloc_inode = shmem_alloc_inode,
  2216. .destroy_inode = shmem_destroy_inode,
  2217. #ifdef CONFIG_TMPFS
  2218. .statfs = shmem_statfs,
  2219. .remount_fs = shmem_remount_fs,
  2220. #endif
  2221. .delete_inode = shmem_delete_inode,
  2222. .drop_inode = generic_delete_inode,
  2223. .put_super = shmem_put_super,
  2224. };
  2225. static struct vm_operations_struct shmem_vm_ops = {
  2226. .nopage = shmem_nopage,
  2227. .populate = shmem_populate,
  2228. #ifdef CONFIG_NUMA
  2229. .set_policy = shmem_set_policy,
  2230. .get_policy = shmem_get_policy,
  2231. #endif
  2232. };
  2233. static int shmem_get_sb(struct file_system_type *fs_type,
  2234. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2235. {
  2236. return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
  2237. }
  2238. static struct file_system_type tmpfs_fs_type = {
  2239. .owner = THIS_MODULE,
  2240. .name = "tmpfs",
  2241. .get_sb = shmem_get_sb,
  2242. .kill_sb = kill_litter_super,
  2243. };
  2244. static struct vfsmount *shm_mnt;
  2245. static int __init init_tmpfs(void)
  2246. {
  2247. int error;
  2248. error = init_inodecache();
  2249. if (error)
  2250. goto out3;
  2251. error = register_filesystem(&tmpfs_fs_type);
  2252. if (error) {
  2253. printk(KERN_ERR "Could not register tmpfs\n");
  2254. goto out2;
  2255. }
  2256. shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
  2257. tmpfs_fs_type.name, NULL);
  2258. if (IS_ERR(shm_mnt)) {
  2259. error = PTR_ERR(shm_mnt);
  2260. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2261. goto out1;
  2262. }
  2263. return 0;
  2264. out1:
  2265. unregister_filesystem(&tmpfs_fs_type);
  2266. out2:
  2267. destroy_inodecache();
  2268. out3:
  2269. shm_mnt = ERR_PTR(error);
  2270. return error;
  2271. }
  2272. module_init(init_tmpfs)
  2273. /*
  2274. * shmem_file_setup - get an unlinked file living in tmpfs
  2275. *
  2276. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2277. * @size: size to be set for the file
  2278. *
  2279. */
  2280. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  2281. {
  2282. int error;
  2283. struct file *file;
  2284. struct inode *inode;
  2285. struct dentry *dentry, *root;
  2286. struct qstr this;
  2287. if (IS_ERR(shm_mnt))
  2288. return (void *)shm_mnt;
  2289. if (size < 0 || size > SHMEM_MAX_BYTES)
  2290. return ERR_PTR(-EINVAL);
  2291. if (shmem_acct_size(flags, size))
  2292. return ERR_PTR(-ENOMEM);
  2293. error = -ENOMEM;
  2294. this.name = name;
  2295. this.len = strlen(name);
  2296. this.hash = 0; /* will go */
  2297. root = shm_mnt->mnt_root;
  2298. dentry = d_alloc(root, &this);
  2299. if (!dentry)
  2300. goto put_memory;
  2301. error = -ENFILE;
  2302. file = get_empty_filp();
  2303. if (!file)
  2304. goto put_dentry;
  2305. error = -ENOSPC;
  2306. inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  2307. if (!inode)
  2308. goto close_file;
  2309. SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
  2310. d_instantiate(dentry, inode);
  2311. inode->i_size = size;
  2312. inode->i_nlink = 0; /* It is unlinked */
  2313. file->f_path.mnt = mntget(shm_mnt);
  2314. file->f_path.dentry = dentry;
  2315. file->f_mapping = inode->i_mapping;
  2316. file->f_op = &shmem_file_operations;
  2317. file->f_mode = FMODE_WRITE | FMODE_READ;
  2318. return file;
  2319. close_file:
  2320. put_filp(file);
  2321. put_dentry:
  2322. dput(dentry);
  2323. put_memory:
  2324. shmem_unacct_size(flags, size);
  2325. return ERR_PTR(error);
  2326. }
  2327. /*
  2328. * shmem_zero_setup - setup a shared anonymous mapping
  2329. *
  2330. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2331. */
  2332. int shmem_zero_setup(struct vm_area_struct *vma)
  2333. {
  2334. struct file *file;
  2335. loff_t size = vma->vm_end - vma->vm_start;
  2336. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2337. if (IS_ERR(file))
  2338. return PTR_ERR(file);
  2339. if (vma->vm_file)
  2340. fput(vma->vm_file);
  2341. vma->vm_file = file;
  2342. vma->vm_ops = &shmem_vm_ops;
  2343. return 0;
  2344. }