shmem.c 58 KB

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