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