shmem.c 58 KB

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