shmem.c 56 KB

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