shmem.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879
  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-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/file.h>
  29. #include <linux/mm.h>
  30. #include <linux/export.h>
  31. #include <linux/swap.h>
  32. static struct vfsmount *shm_mnt;
  33. #ifdef CONFIG_SHMEM
  34. /*
  35. * This virtual memory filesystem is heavily based on the ramfs. It
  36. * extends ramfs by the ability to use swap and honor resource limits
  37. * which makes it a completely usable filesystem.
  38. */
  39. #include <linux/xattr.h>
  40. #include <linux/exportfs.h>
  41. #include <linux/posix_acl.h>
  42. #include <linux/generic_acl.h>
  43. #include <linux/mman.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/backing-dev.h>
  47. #include <linux/shmem_fs.h>
  48. #include <linux/writeback.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/pagevec.h>
  51. #include <linux/percpu_counter.h>
  52. #include <linux/falloc.h>
  53. #include <linux/splice.h>
  54. #include <linux/security.h>
  55. #include <linux/swapops.h>
  56. #include <linux/mempolicy.h>
  57. #include <linux/namei.h>
  58. #include <linux/ctype.h>
  59. #include <linux/migrate.h>
  60. #include <linux/highmem.h>
  61. #include <linux/seq_file.h>
  62. #include <linux/magic.h>
  63. #include <asm/uaccess.h>
  64. #include <asm/pgtable.h>
  65. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  66. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  67. /* Pretend that each entry is of this size in directory's i_size */
  68. #define BOGO_DIRENT_SIZE 20
  69. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  70. #define SHORT_SYMLINK_LEN 128
  71. /*
  72. * shmem_fallocate and shmem_writepage communicate via inode->i_private
  73. * (with i_mutex making sure that it has only one user at a time):
  74. * we would prefer not to enlarge the shmem inode just for that.
  75. */
  76. struct shmem_falloc {
  77. pgoff_t start; /* start of range currently being fallocated */
  78. pgoff_t next; /* the next page offset to be fallocated */
  79. pgoff_t nr_falloced; /* how many new pages have been fallocated */
  80. pgoff_t nr_unswapped; /* how often writepage refused to swap out */
  81. };
  82. /* Flag allocation requirements to shmem_getpage */
  83. enum sgp_type {
  84. SGP_READ, /* don't exceed i_size, don't allocate page */
  85. SGP_CACHE, /* don't exceed i_size, may allocate page */
  86. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  87. SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
  88. SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
  89. };
  90. #ifdef CONFIG_TMPFS
  91. static unsigned long shmem_default_max_blocks(void)
  92. {
  93. return totalram_pages / 2;
  94. }
  95. static unsigned long shmem_default_max_inodes(void)
  96. {
  97. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  98. }
  99. #endif
  100. static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
  101. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  102. struct shmem_inode_info *info, pgoff_t index);
  103. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  104. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  105. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  106. struct page **pagep, enum sgp_type sgp, int *fault_type)
  107. {
  108. return shmem_getpage_gfp(inode, index, pagep, sgp,
  109. mapping_gfp_mask(inode->i_mapping), fault_type);
  110. }
  111. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  112. {
  113. return sb->s_fs_info;
  114. }
  115. /*
  116. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  117. * for shared memory and for shared anonymous (/dev/zero) mappings
  118. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  119. * consistent with the pre-accounting of private mappings ...
  120. */
  121. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  122. {
  123. return (flags & VM_NORESERVE) ?
  124. 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
  125. }
  126. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  127. {
  128. if (!(flags & VM_NORESERVE))
  129. vm_unacct_memory(VM_ACCT(size));
  130. }
  131. /*
  132. * ... whereas tmpfs objects are accounted incrementally as
  133. * pages are allocated, in order to allow huge sparse files.
  134. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  135. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  136. */
  137. static inline int shmem_acct_block(unsigned long flags)
  138. {
  139. return (flags & VM_NORESERVE) ?
  140. security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  141. }
  142. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  143. {
  144. if (flags & VM_NORESERVE)
  145. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  146. }
  147. static const struct super_operations shmem_ops;
  148. static const struct address_space_operations shmem_aops;
  149. static const struct file_operations shmem_file_operations;
  150. static const struct inode_operations shmem_inode_operations;
  151. static const struct inode_operations shmem_dir_inode_operations;
  152. static const struct inode_operations shmem_special_inode_operations;
  153. static const struct vm_operations_struct shmem_vm_ops;
  154. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  155. .ra_pages = 0, /* No readahead */
  156. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  157. };
  158. static LIST_HEAD(shmem_swaplist);
  159. static DEFINE_MUTEX(shmem_swaplist_mutex);
  160. static int shmem_reserve_inode(struct super_block *sb)
  161. {
  162. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  163. if (sbinfo->max_inodes) {
  164. spin_lock(&sbinfo->stat_lock);
  165. if (!sbinfo->free_inodes) {
  166. spin_unlock(&sbinfo->stat_lock);
  167. return -ENOSPC;
  168. }
  169. sbinfo->free_inodes--;
  170. spin_unlock(&sbinfo->stat_lock);
  171. }
  172. return 0;
  173. }
  174. static void shmem_free_inode(struct super_block *sb)
  175. {
  176. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  177. if (sbinfo->max_inodes) {
  178. spin_lock(&sbinfo->stat_lock);
  179. sbinfo->free_inodes++;
  180. spin_unlock(&sbinfo->stat_lock);
  181. }
  182. }
  183. /**
  184. * shmem_recalc_inode - recalculate the block usage of an inode
  185. * @inode: inode to recalc
  186. *
  187. * We have to calculate the free blocks since the mm can drop
  188. * undirtied hole pages behind our back.
  189. *
  190. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  191. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  192. *
  193. * It has to be called with the spinlock held.
  194. */
  195. static void shmem_recalc_inode(struct inode *inode)
  196. {
  197. struct shmem_inode_info *info = SHMEM_I(inode);
  198. long freed;
  199. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  200. if (freed > 0) {
  201. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  202. if (sbinfo->max_blocks)
  203. percpu_counter_add(&sbinfo->used_blocks, -freed);
  204. info->alloced -= freed;
  205. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  206. shmem_unacct_blocks(info->flags, freed);
  207. }
  208. }
  209. /*
  210. * Replace item expected in radix tree by a new item, while holding tree lock.
  211. */
  212. static int shmem_radix_tree_replace(struct address_space *mapping,
  213. pgoff_t index, void *expected, void *replacement)
  214. {
  215. void **pslot;
  216. void *item = NULL;
  217. VM_BUG_ON(!expected);
  218. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  219. if (pslot)
  220. item = radix_tree_deref_slot_protected(pslot,
  221. &mapping->tree_lock);
  222. if (item != expected)
  223. return -ENOENT;
  224. if (replacement)
  225. radix_tree_replace_slot(pslot, replacement);
  226. else
  227. radix_tree_delete(&mapping->page_tree, index);
  228. return 0;
  229. }
  230. /*
  231. * Sometimes, before we decide whether to proceed or to fail, we must check
  232. * that an entry was not already brought back from swap by a racing thread.
  233. *
  234. * Checking page is not enough: by the time a SwapCache page is locked, it
  235. * might be reused, and again be SwapCache, using the same swap as before.
  236. */
  237. static bool shmem_confirm_swap(struct address_space *mapping,
  238. pgoff_t index, swp_entry_t swap)
  239. {
  240. void *item;
  241. rcu_read_lock();
  242. item = radix_tree_lookup(&mapping->page_tree, index);
  243. rcu_read_unlock();
  244. return item == swp_to_radix_entry(swap);
  245. }
  246. /*
  247. * Like add_to_page_cache_locked, but error if expected item has gone.
  248. */
  249. static int shmem_add_to_page_cache(struct page *page,
  250. struct address_space *mapping,
  251. pgoff_t index, gfp_t gfp, void *expected)
  252. {
  253. int error;
  254. VM_BUG_ON(!PageLocked(page));
  255. VM_BUG_ON(!PageSwapBacked(page));
  256. page_cache_get(page);
  257. page->mapping = mapping;
  258. page->index = index;
  259. spin_lock_irq(&mapping->tree_lock);
  260. if (!expected)
  261. error = radix_tree_insert(&mapping->page_tree, index, page);
  262. else
  263. error = shmem_radix_tree_replace(mapping, index, expected,
  264. page);
  265. if (!error) {
  266. mapping->nrpages++;
  267. __inc_zone_page_state(page, NR_FILE_PAGES);
  268. __inc_zone_page_state(page, NR_SHMEM);
  269. spin_unlock_irq(&mapping->tree_lock);
  270. } else {
  271. page->mapping = NULL;
  272. spin_unlock_irq(&mapping->tree_lock);
  273. page_cache_release(page);
  274. }
  275. return error;
  276. }
  277. /*
  278. * Like delete_from_page_cache, but substitutes swap for page.
  279. */
  280. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  281. {
  282. struct address_space *mapping = page->mapping;
  283. int error;
  284. spin_lock_irq(&mapping->tree_lock);
  285. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  286. page->mapping = NULL;
  287. mapping->nrpages--;
  288. __dec_zone_page_state(page, NR_FILE_PAGES);
  289. __dec_zone_page_state(page, NR_SHMEM);
  290. spin_unlock_irq(&mapping->tree_lock);
  291. page_cache_release(page);
  292. BUG_ON(error);
  293. }
  294. /*
  295. * Like find_get_pages, but collecting swap entries as well as pages.
  296. */
  297. static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
  298. pgoff_t start, unsigned int nr_pages,
  299. struct page **pages, pgoff_t *indices)
  300. {
  301. unsigned int i;
  302. unsigned int ret;
  303. unsigned int nr_found;
  304. rcu_read_lock();
  305. restart:
  306. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  307. (void ***)pages, indices, start, nr_pages);
  308. ret = 0;
  309. for (i = 0; i < nr_found; i++) {
  310. struct page *page;
  311. repeat:
  312. page = radix_tree_deref_slot((void **)pages[i]);
  313. if (unlikely(!page))
  314. continue;
  315. if (radix_tree_exception(page)) {
  316. if (radix_tree_deref_retry(page))
  317. goto restart;
  318. /*
  319. * Otherwise, we must be storing a swap entry
  320. * here as an exceptional entry: so return it
  321. * without attempting to raise page count.
  322. */
  323. goto export;
  324. }
  325. if (!page_cache_get_speculative(page))
  326. goto repeat;
  327. /* Has the page moved? */
  328. if (unlikely(page != *((void **)pages[i]))) {
  329. page_cache_release(page);
  330. goto repeat;
  331. }
  332. export:
  333. indices[ret] = indices[i];
  334. pages[ret] = page;
  335. ret++;
  336. }
  337. if (unlikely(!ret && nr_found))
  338. goto restart;
  339. rcu_read_unlock();
  340. return ret;
  341. }
  342. /*
  343. * Remove swap entry from radix tree, free the swap and its page cache.
  344. */
  345. static int shmem_free_swap(struct address_space *mapping,
  346. pgoff_t index, void *radswap)
  347. {
  348. int error;
  349. spin_lock_irq(&mapping->tree_lock);
  350. error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
  351. spin_unlock_irq(&mapping->tree_lock);
  352. if (!error)
  353. free_swap_and_cache(radix_to_swp_entry(radswap));
  354. return error;
  355. }
  356. /*
  357. * Pagevec may contain swap entries, so shuffle up pages before releasing.
  358. */
  359. static void shmem_deswap_pagevec(struct pagevec *pvec)
  360. {
  361. int i, j;
  362. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  363. struct page *page = pvec->pages[i];
  364. if (!radix_tree_exceptional_entry(page))
  365. pvec->pages[j++] = page;
  366. }
  367. pvec->nr = j;
  368. }
  369. /*
  370. * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
  371. */
  372. void shmem_unlock_mapping(struct address_space *mapping)
  373. {
  374. struct pagevec pvec;
  375. pgoff_t indices[PAGEVEC_SIZE];
  376. pgoff_t index = 0;
  377. pagevec_init(&pvec, 0);
  378. /*
  379. * Minor point, but we might as well stop if someone else SHM_LOCKs it.
  380. */
  381. while (!mapping_unevictable(mapping)) {
  382. /*
  383. * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
  384. * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
  385. */
  386. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  387. PAGEVEC_SIZE, pvec.pages, indices);
  388. if (!pvec.nr)
  389. break;
  390. index = indices[pvec.nr - 1] + 1;
  391. shmem_deswap_pagevec(&pvec);
  392. check_move_unevictable_pages(pvec.pages, pvec.nr);
  393. pagevec_release(&pvec);
  394. cond_resched();
  395. }
  396. }
  397. /*
  398. * Remove range of pages and swap entries from radix tree, and free them.
  399. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
  400. */
  401. static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  402. bool unfalloc)
  403. {
  404. struct address_space *mapping = inode->i_mapping;
  405. struct shmem_inode_info *info = SHMEM_I(inode);
  406. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  407. pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
  408. unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
  409. unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
  410. struct pagevec pvec;
  411. pgoff_t indices[PAGEVEC_SIZE];
  412. long nr_swaps_freed = 0;
  413. pgoff_t index;
  414. int i;
  415. if (lend == -1)
  416. end = -1; /* unsigned, so actually very big */
  417. pagevec_init(&pvec, 0);
  418. index = start;
  419. while (index < end) {
  420. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  421. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  422. pvec.pages, indices);
  423. if (!pvec.nr)
  424. break;
  425. mem_cgroup_uncharge_start();
  426. for (i = 0; i < pagevec_count(&pvec); i++) {
  427. struct page *page = pvec.pages[i];
  428. index = indices[i];
  429. if (index >= end)
  430. break;
  431. if (radix_tree_exceptional_entry(page)) {
  432. if (unfalloc)
  433. continue;
  434. nr_swaps_freed += !shmem_free_swap(mapping,
  435. index, page);
  436. continue;
  437. }
  438. if (!trylock_page(page))
  439. continue;
  440. if (!unfalloc || !PageUptodate(page)) {
  441. if (page->mapping == mapping) {
  442. VM_BUG_ON(PageWriteback(page));
  443. truncate_inode_page(mapping, page);
  444. }
  445. }
  446. unlock_page(page);
  447. }
  448. shmem_deswap_pagevec(&pvec);
  449. pagevec_release(&pvec);
  450. mem_cgroup_uncharge_end();
  451. cond_resched();
  452. index++;
  453. }
  454. if (partial_start) {
  455. struct page *page = NULL;
  456. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  457. if (page) {
  458. unsigned int top = PAGE_CACHE_SIZE;
  459. if (start > end) {
  460. top = partial_end;
  461. partial_end = 0;
  462. }
  463. zero_user_segment(page, partial_start, top);
  464. set_page_dirty(page);
  465. unlock_page(page);
  466. page_cache_release(page);
  467. }
  468. }
  469. if (partial_end) {
  470. struct page *page = NULL;
  471. shmem_getpage(inode, end, &page, SGP_READ, NULL);
  472. if (page) {
  473. zero_user_segment(page, 0, partial_end);
  474. set_page_dirty(page);
  475. unlock_page(page);
  476. page_cache_release(page);
  477. }
  478. }
  479. if (start >= end)
  480. return;
  481. index = start;
  482. for ( ; ; ) {
  483. cond_resched();
  484. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  485. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  486. pvec.pages, indices);
  487. if (!pvec.nr) {
  488. if (index == start || unfalloc)
  489. break;
  490. index = start;
  491. continue;
  492. }
  493. if ((index == start || unfalloc) && indices[0] >= end) {
  494. shmem_deswap_pagevec(&pvec);
  495. pagevec_release(&pvec);
  496. break;
  497. }
  498. mem_cgroup_uncharge_start();
  499. for (i = 0; i < pagevec_count(&pvec); i++) {
  500. struct page *page = pvec.pages[i];
  501. index = indices[i];
  502. if (index >= end)
  503. break;
  504. if (radix_tree_exceptional_entry(page)) {
  505. if (unfalloc)
  506. continue;
  507. nr_swaps_freed += !shmem_free_swap(mapping,
  508. index, page);
  509. continue;
  510. }
  511. lock_page(page);
  512. if (!unfalloc || !PageUptodate(page)) {
  513. if (page->mapping == mapping) {
  514. VM_BUG_ON(PageWriteback(page));
  515. truncate_inode_page(mapping, page);
  516. }
  517. }
  518. unlock_page(page);
  519. }
  520. shmem_deswap_pagevec(&pvec);
  521. pagevec_release(&pvec);
  522. mem_cgroup_uncharge_end();
  523. index++;
  524. }
  525. spin_lock(&info->lock);
  526. info->swapped -= nr_swaps_freed;
  527. shmem_recalc_inode(inode);
  528. spin_unlock(&info->lock);
  529. }
  530. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  531. {
  532. shmem_undo_range(inode, lstart, lend, false);
  533. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  534. }
  535. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  536. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  537. {
  538. struct inode *inode = dentry->d_inode;
  539. int error;
  540. error = inode_change_ok(inode, attr);
  541. if (error)
  542. return error;
  543. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  544. loff_t oldsize = inode->i_size;
  545. loff_t newsize = attr->ia_size;
  546. if (newsize != oldsize) {
  547. i_size_write(inode, newsize);
  548. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  549. }
  550. if (newsize < oldsize) {
  551. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  552. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  553. shmem_truncate_range(inode, newsize, (loff_t)-1);
  554. /* unmap again to remove racily COWed private pages */
  555. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  556. }
  557. }
  558. setattr_copy(inode, attr);
  559. #ifdef CONFIG_TMPFS_POSIX_ACL
  560. if (attr->ia_valid & ATTR_MODE)
  561. error = generic_acl_chmod(inode);
  562. #endif
  563. return error;
  564. }
  565. static void shmem_evict_inode(struct inode *inode)
  566. {
  567. struct shmem_inode_info *info = SHMEM_I(inode);
  568. if (inode->i_mapping->a_ops == &shmem_aops) {
  569. shmem_unacct_size(info->flags, inode->i_size);
  570. inode->i_size = 0;
  571. shmem_truncate_range(inode, 0, (loff_t)-1);
  572. if (!list_empty(&info->swaplist)) {
  573. mutex_lock(&shmem_swaplist_mutex);
  574. list_del_init(&info->swaplist);
  575. mutex_unlock(&shmem_swaplist_mutex);
  576. }
  577. } else
  578. kfree(info->symlink);
  579. simple_xattrs_free(&info->xattrs);
  580. BUG_ON(inode->i_blocks);
  581. shmem_free_inode(inode->i_sb);
  582. clear_inode(inode);
  583. }
  584. /*
  585. * If swap found in inode, free it and move page from swapcache to filecache.
  586. */
  587. static int shmem_unuse_inode(struct shmem_inode_info *info,
  588. swp_entry_t swap, struct page **pagep)
  589. {
  590. struct address_space *mapping = info->vfs_inode.i_mapping;
  591. void *radswap;
  592. pgoff_t index;
  593. gfp_t gfp;
  594. int error = 0;
  595. radswap = swp_to_radix_entry(swap);
  596. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  597. if (index == -1)
  598. return 0;
  599. /*
  600. * Move _head_ to start search for next from here.
  601. * But be careful: shmem_evict_inode checks list_empty without taking
  602. * mutex, and there's an instant in list_move_tail when info->swaplist
  603. * would appear empty, if it were the only one on shmem_swaplist.
  604. */
  605. if (shmem_swaplist.next != &info->swaplist)
  606. list_move_tail(&shmem_swaplist, &info->swaplist);
  607. gfp = mapping_gfp_mask(mapping);
  608. if (shmem_should_replace_page(*pagep, gfp)) {
  609. mutex_unlock(&shmem_swaplist_mutex);
  610. error = shmem_replace_page(pagep, gfp, info, index);
  611. mutex_lock(&shmem_swaplist_mutex);
  612. /*
  613. * We needed to drop mutex to make that restrictive page
  614. * allocation, but the inode might have been freed while we
  615. * dropped it: although a racing shmem_evict_inode() cannot
  616. * complete without emptying the radix_tree, our page lock
  617. * on this swapcache page is not enough to prevent that -
  618. * free_swap_and_cache() of our swap entry will only
  619. * trylock_page(), removing swap from radix_tree whatever.
  620. *
  621. * We must not proceed to shmem_add_to_page_cache() if the
  622. * inode has been freed, but of course we cannot rely on
  623. * inode or mapping or info to check that. However, we can
  624. * safely check if our swap entry is still in use (and here
  625. * it can't have got reused for another page): if it's still
  626. * in use, then the inode cannot have been freed yet, and we
  627. * can safely proceed (if it's no longer in use, that tells
  628. * nothing about the inode, but we don't need to unuse swap).
  629. */
  630. if (!page_swapcount(*pagep))
  631. error = -ENOENT;
  632. }
  633. /*
  634. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  635. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  636. * beneath us (pagelock doesn't help until the page is in pagecache).
  637. */
  638. if (!error)
  639. error = shmem_add_to_page_cache(*pagep, mapping, index,
  640. GFP_NOWAIT, radswap);
  641. if (error != -ENOMEM) {
  642. /*
  643. * Truncation and eviction use free_swap_and_cache(), which
  644. * only does trylock page: if we raced, best clean up here.
  645. */
  646. delete_from_swap_cache(*pagep);
  647. set_page_dirty(*pagep);
  648. if (!error) {
  649. spin_lock(&info->lock);
  650. info->swapped--;
  651. spin_unlock(&info->lock);
  652. swap_free(swap);
  653. }
  654. error = 1; /* not an error, but entry was found */
  655. }
  656. return error;
  657. }
  658. /*
  659. * Search through swapped inodes to find and replace swap by page.
  660. */
  661. int shmem_unuse(swp_entry_t swap, struct page *page)
  662. {
  663. struct list_head *this, *next;
  664. struct shmem_inode_info *info;
  665. int found = 0;
  666. int error = 0;
  667. /*
  668. * There's a faint possibility that swap page was replaced before
  669. * caller locked it: caller will come back later with the right page.
  670. */
  671. if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
  672. goto out;
  673. /*
  674. * Charge page using GFP_KERNEL while we can wait, before taking
  675. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  676. * Charged back to the user (not to caller) when swap account is used.
  677. */
  678. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  679. if (error)
  680. goto out;
  681. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  682. mutex_lock(&shmem_swaplist_mutex);
  683. list_for_each_safe(this, next, &shmem_swaplist) {
  684. info = list_entry(this, struct shmem_inode_info, swaplist);
  685. if (info->swapped)
  686. found = shmem_unuse_inode(info, swap, &page);
  687. else
  688. list_del_init(&info->swaplist);
  689. cond_resched();
  690. if (found)
  691. break;
  692. }
  693. mutex_unlock(&shmem_swaplist_mutex);
  694. if (found < 0)
  695. error = found;
  696. out:
  697. unlock_page(page);
  698. page_cache_release(page);
  699. return error;
  700. }
  701. /*
  702. * Move the page from the page cache to the swap cache.
  703. */
  704. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  705. {
  706. struct shmem_inode_info *info;
  707. struct address_space *mapping;
  708. struct inode *inode;
  709. swp_entry_t swap;
  710. pgoff_t index;
  711. BUG_ON(!PageLocked(page));
  712. mapping = page->mapping;
  713. index = page->index;
  714. inode = mapping->host;
  715. info = SHMEM_I(inode);
  716. if (info->flags & VM_LOCKED)
  717. goto redirty;
  718. if (!total_swap_pages)
  719. goto redirty;
  720. /*
  721. * shmem_backing_dev_info's capabilities prevent regular writeback or
  722. * sync from ever calling shmem_writepage; but a stacking filesystem
  723. * might use ->writepage of its underlying filesystem, in which case
  724. * tmpfs should write out to swap only in response to memory pressure,
  725. * and not for the writeback threads or sync.
  726. */
  727. if (!wbc->for_reclaim) {
  728. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  729. goto redirty;
  730. }
  731. /*
  732. * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
  733. * value into swapfile.c, the only way we can correctly account for a
  734. * fallocated page arriving here is now to initialize it and write it.
  735. *
  736. * That's okay for a page already fallocated earlier, but if we have
  737. * not yet completed the fallocation, then (a) we want to keep track
  738. * of this page in case we have to undo it, and (b) it may not be a
  739. * good idea to continue anyway, once we're pushing into swap. So
  740. * reactivate the page, and let shmem_fallocate() quit when too many.
  741. */
  742. if (!PageUptodate(page)) {
  743. if (inode->i_private) {
  744. struct shmem_falloc *shmem_falloc;
  745. spin_lock(&inode->i_lock);
  746. shmem_falloc = inode->i_private;
  747. if (shmem_falloc &&
  748. index >= shmem_falloc->start &&
  749. index < shmem_falloc->next)
  750. shmem_falloc->nr_unswapped++;
  751. else
  752. shmem_falloc = NULL;
  753. spin_unlock(&inode->i_lock);
  754. if (shmem_falloc)
  755. goto redirty;
  756. }
  757. clear_highpage(page);
  758. flush_dcache_page(page);
  759. SetPageUptodate(page);
  760. }
  761. swap = get_swap_page();
  762. if (!swap.val)
  763. goto redirty;
  764. /*
  765. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  766. * if it's not already there. Do it now before the page is
  767. * moved to swap cache, when its pagelock no longer protects
  768. * the inode from eviction. But don't unlock the mutex until
  769. * we've incremented swapped, because shmem_unuse_inode() will
  770. * prune a !swapped inode from the swaplist under this mutex.
  771. */
  772. mutex_lock(&shmem_swaplist_mutex);
  773. if (list_empty(&info->swaplist))
  774. list_add_tail(&info->swaplist, &shmem_swaplist);
  775. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  776. swap_shmem_alloc(swap);
  777. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  778. spin_lock(&info->lock);
  779. info->swapped++;
  780. shmem_recalc_inode(inode);
  781. spin_unlock(&info->lock);
  782. mutex_unlock(&shmem_swaplist_mutex);
  783. BUG_ON(page_mapped(page));
  784. swap_writepage(page, wbc);
  785. return 0;
  786. }
  787. mutex_unlock(&shmem_swaplist_mutex);
  788. swapcache_free(swap, NULL);
  789. redirty:
  790. set_page_dirty(page);
  791. if (wbc->for_reclaim)
  792. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  793. unlock_page(page);
  794. return 0;
  795. }
  796. #ifdef CONFIG_NUMA
  797. #ifdef CONFIG_TMPFS
  798. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  799. {
  800. char buffer[64];
  801. if (!mpol || mpol->mode == MPOL_DEFAULT)
  802. return; /* show nothing */
  803. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  804. seq_printf(seq, ",mpol=%s", buffer);
  805. }
  806. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  807. {
  808. struct mempolicy *mpol = NULL;
  809. if (sbinfo->mpol) {
  810. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  811. mpol = sbinfo->mpol;
  812. mpol_get(mpol);
  813. spin_unlock(&sbinfo->stat_lock);
  814. }
  815. return mpol;
  816. }
  817. #endif /* CONFIG_TMPFS */
  818. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  819. struct shmem_inode_info *info, pgoff_t index)
  820. {
  821. struct mempolicy mpol, *spol;
  822. struct vm_area_struct pvma;
  823. spol = mpol_cond_copy(&mpol,
  824. mpol_shared_policy_lookup(&info->policy, index));
  825. /* Create a pseudo vma that just contains the policy */
  826. pvma.vm_start = 0;
  827. /* Bias interleave by inode number to distribute better across nodes */
  828. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  829. pvma.vm_ops = NULL;
  830. pvma.vm_policy = spol;
  831. return swapin_readahead(swap, gfp, &pvma, 0);
  832. }
  833. static struct page *shmem_alloc_page(gfp_t gfp,
  834. struct shmem_inode_info *info, pgoff_t index)
  835. {
  836. struct vm_area_struct pvma;
  837. /* Create a pseudo vma that just contains the policy */
  838. pvma.vm_start = 0;
  839. /* Bias interleave by inode number to distribute better across nodes */
  840. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  841. pvma.vm_ops = NULL;
  842. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  843. /*
  844. * alloc_page_vma() will drop the shared policy reference
  845. */
  846. return alloc_page_vma(gfp, &pvma, 0);
  847. }
  848. #else /* !CONFIG_NUMA */
  849. #ifdef CONFIG_TMPFS
  850. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  851. {
  852. }
  853. #endif /* CONFIG_TMPFS */
  854. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  855. struct shmem_inode_info *info, pgoff_t index)
  856. {
  857. return swapin_readahead(swap, gfp, NULL, 0);
  858. }
  859. static inline struct page *shmem_alloc_page(gfp_t gfp,
  860. struct shmem_inode_info *info, pgoff_t index)
  861. {
  862. return alloc_page(gfp);
  863. }
  864. #endif /* CONFIG_NUMA */
  865. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  866. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  867. {
  868. return NULL;
  869. }
  870. #endif
  871. /*
  872. * When a page is moved from swapcache to shmem filecache (either by the
  873. * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
  874. * shmem_unuse_inode()), it may have been read in earlier from swap, in
  875. * ignorance of the mapping it belongs to. If that mapping has special
  876. * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
  877. * we may need to copy to a suitable page before moving to filecache.
  878. *
  879. * In a future release, this may well be extended to respect cpuset and
  880. * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
  881. * but for now it is a simple matter of zone.
  882. */
  883. static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
  884. {
  885. return page_zonenum(page) > gfp_zone(gfp);
  886. }
  887. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  888. struct shmem_inode_info *info, pgoff_t index)
  889. {
  890. struct page *oldpage, *newpage;
  891. struct address_space *swap_mapping;
  892. pgoff_t swap_index;
  893. int error;
  894. oldpage = *pagep;
  895. swap_index = page_private(oldpage);
  896. swap_mapping = page_mapping(oldpage);
  897. /*
  898. * We have arrived here because our zones are constrained, so don't
  899. * limit chance of success by further cpuset and node constraints.
  900. */
  901. gfp &= ~GFP_CONSTRAINT_MASK;
  902. newpage = shmem_alloc_page(gfp, info, index);
  903. if (!newpage)
  904. return -ENOMEM;
  905. page_cache_get(newpage);
  906. copy_highpage(newpage, oldpage);
  907. flush_dcache_page(newpage);
  908. __set_page_locked(newpage);
  909. SetPageUptodate(newpage);
  910. SetPageSwapBacked(newpage);
  911. set_page_private(newpage, swap_index);
  912. SetPageSwapCache(newpage);
  913. /*
  914. * Our caller will very soon move newpage out of swapcache, but it's
  915. * a nice clean interface for us to replace oldpage by newpage there.
  916. */
  917. spin_lock_irq(&swap_mapping->tree_lock);
  918. error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
  919. newpage);
  920. if (!error) {
  921. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  922. __dec_zone_page_state(oldpage, NR_FILE_PAGES);
  923. }
  924. spin_unlock_irq(&swap_mapping->tree_lock);
  925. if (unlikely(error)) {
  926. /*
  927. * Is this possible? I think not, now that our callers check
  928. * both PageSwapCache and page_private after getting page lock;
  929. * but be defensive. Reverse old to newpage for clear and free.
  930. */
  931. oldpage = newpage;
  932. } else {
  933. mem_cgroup_replace_page_cache(oldpage, newpage);
  934. lru_cache_add_anon(newpage);
  935. *pagep = newpage;
  936. }
  937. ClearPageSwapCache(oldpage);
  938. set_page_private(oldpage, 0);
  939. unlock_page(oldpage);
  940. page_cache_release(oldpage);
  941. page_cache_release(oldpage);
  942. return error;
  943. }
  944. /*
  945. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  946. *
  947. * If we allocate a new one we do not mark it dirty. That's up to the
  948. * vm. If we swap it in we mark it dirty since we also free the swap
  949. * entry since a page cannot live in both the swap and page cache
  950. */
  951. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  952. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  953. {
  954. struct address_space *mapping = inode->i_mapping;
  955. struct shmem_inode_info *info;
  956. struct shmem_sb_info *sbinfo;
  957. struct page *page;
  958. swp_entry_t swap;
  959. int error;
  960. int once = 0;
  961. int alloced = 0;
  962. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  963. return -EFBIG;
  964. repeat:
  965. swap.val = 0;
  966. page = find_lock_page(mapping, index);
  967. if (radix_tree_exceptional_entry(page)) {
  968. swap = radix_to_swp_entry(page);
  969. page = NULL;
  970. }
  971. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  972. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  973. error = -EINVAL;
  974. goto failed;
  975. }
  976. /* fallocated page? */
  977. if (page && !PageUptodate(page)) {
  978. if (sgp != SGP_READ)
  979. goto clear;
  980. unlock_page(page);
  981. page_cache_release(page);
  982. page = NULL;
  983. }
  984. if (page || (sgp == SGP_READ && !swap.val)) {
  985. *pagep = page;
  986. return 0;
  987. }
  988. /*
  989. * Fast cache lookup did not find it:
  990. * bring it back from swap or allocate.
  991. */
  992. info = SHMEM_I(inode);
  993. sbinfo = SHMEM_SB(inode->i_sb);
  994. if (swap.val) {
  995. /* Look it up and read it in.. */
  996. page = lookup_swap_cache(swap);
  997. if (!page) {
  998. /* here we actually do the io */
  999. if (fault_type)
  1000. *fault_type |= VM_FAULT_MAJOR;
  1001. page = shmem_swapin(swap, gfp, info, index);
  1002. if (!page) {
  1003. error = -ENOMEM;
  1004. goto failed;
  1005. }
  1006. }
  1007. /* We have to do this with page locked to prevent races */
  1008. lock_page(page);
  1009. if (!PageSwapCache(page) || page_private(page) != swap.val ||
  1010. !shmem_confirm_swap(mapping, index, swap)) {
  1011. error = -EEXIST; /* try again */
  1012. goto unlock;
  1013. }
  1014. if (!PageUptodate(page)) {
  1015. error = -EIO;
  1016. goto failed;
  1017. }
  1018. wait_on_page_writeback(page);
  1019. if (shmem_should_replace_page(page, gfp)) {
  1020. error = shmem_replace_page(&page, gfp, info, index);
  1021. if (error)
  1022. goto failed;
  1023. }
  1024. error = mem_cgroup_cache_charge(page, current->mm,
  1025. gfp & GFP_RECLAIM_MASK);
  1026. if (!error) {
  1027. error = shmem_add_to_page_cache(page, mapping, index,
  1028. gfp, swp_to_radix_entry(swap));
  1029. /* We already confirmed swap, and make no allocation */
  1030. VM_BUG_ON(error);
  1031. }
  1032. if (error)
  1033. goto failed;
  1034. spin_lock(&info->lock);
  1035. info->swapped--;
  1036. shmem_recalc_inode(inode);
  1037. spin_unlock(&info->lock);
  1038. delete_from_swap_cache(page);
  1039. set_page_dirty(page);
  1040. swap_free(swap);
  1041. } else {
  1042. if (shmem_acct_block(info->flags)) {
  1043. error = -ENOSPC;
  1044. goto failed;
  1045. }
  1046. if (sbinfo->max_blocks) {
  1047. if (percpu_counter_compare(&sbinfo->used_blocks,
  1048. sbinfo->max_blocks) >= 0) {
  1049. error = -ENOSPC;
  1050. goto unacct;
  1051. }
  1052. percpu_counter_inc(&sbinfo->used_blocks);
  1053. }
  1054. page = shmem_alloc_page(gfp, info, index);
  1055. if (!page) {
  1056. error = -ENOMEM;
  1057. goto decused;
  1058. }
  1059. SetPageSwapBacked(page);
  1060. __set_page_locked(page);
  1061. error = mem_cgroup_cache_charge(page, current->mm,
  1062. gfp & GFP_RECLAIM_MASK);
  1063. if (error)
  1064. goto decused;
  1065. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  1066. if (!error) {
  1067. error = shmem_add_to_page_cache(page, mapping, index,
  1068. gfp, NULL);
  1069. radix_tree_preload_end();
  1070. }
  1071. if (error) {
  1072. mem_cgroup_uncharge_cache_page(page);
  1073. goto decused;
  1074. }
  1075. lru_cache_add_anon(page);
  1076. spin_lock(&info->lock);
  1077. info->alloced++;
  1078. inode->i_blocks += BLOCKS_PER_PAGE;
  1079. shmem_recalc_inode(inode);
  1080. spin_unlock(&info->lock);
  1081. alloced = true;
  1082. /*
  1083. * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
  1084. */
  1085. if (sgp == SGP_FALLOC)
  1086. sgp = SGP_WRITE;
  1087. clear:
  1088. /*
  1089. * Let SGP_WRITE caller clear ends if write does not fill page;
  1090. * but SGP_FALLOC on a page fallocated earlier must initialize
  1091. * it now, lest undo on failure cancel our earlier guarantee.
  1092. */
  1093. if (sgp != SGP_WRITE) {
  1094. clear_highpage(page);
  1095. flush_dcache_page(page);
  1096. SetPageUptodate(page);
  1097. }
  1098. if (sgp == SGP_DIRTY)
  1099. set_page_dirty(page);
  1100. }
  1101. /* Perhaps the file has been truncated since we checked */
  1102. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  1103. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  1104. error = -EINVAL;
  1105. if (alloced)
  1106. goto trunc;
  1107. else
  1108. goto failed;
  1109. }
  1110. *pagep = page;
  1111. return 0;
  1112. /*
  1113. * Error recovery.
  1114. */
  1115. trunc:
  1116. info = SHMEM_I(inode);
  1117. ClearPageDirty(page);
  1118. delete_from_page_cache(page);
  1119. spin_lock(&info->lock);
  1120. info->alloced--;
  1121. inode->i_blocks -= BLOCKS_PER_PAGE;
  1122. spin_unlock(&info->lock);
  1123. decused:
  1124. sbinfo = SHMEM_SB(inode->i_sb);
  1125. if (sbinfo->max_blocks)
  1126. percpu_counter_add(&sbinfo->used_blocks, -1);
  1127. unacct:
  1128. shmem_unacct_blocks(info->flags, 1);
  1129. failed:
  1130. if (swap.val && error != -EINVAL &&
  1131. !shmem_confirm_swap(mapping, index, swap))
  1132. error = -EEXIST;
  1133. unlock:
  1134. if (page) {
  1135. unlock_page(page);
  1136. page_cache_release(page);
  1137. }
  1138. if (error == -ENOSPC && !once++) {
  1139. info = SHMEM_I(inode);
  1140. spin_lock(&info->lock);
  1141. shmem_recalc_inode(inode);
  1142. spin_unlock(&info->lock);
  1143. goto repeat;
  1144. }
  1145. if (error == -EEXIST) /* from above or from radix_tree_insert */
  1146. goto repeat;
  1147. return error;
  1148. }
  1149. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1150. {
  1151. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1152. int error;
  1153. int ret = VM_FAULT_LOCKED;
  1154. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1155. if (error)
  1156. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1157. if (ret & VM_FAULT_MAJOR) {
  1158. count_vm_event(PGMAJFAULT);
  1159. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1160. }
  1161. return ret;
  1162. }
  1163. #ifdef CONFIG_NUMA
  1164. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  1165. {
  1166. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1167. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  1168. }
  1169. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1170. unsigned long addr)
  1171. {
  1172. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1173. pgoff_t index;
  1174. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1175. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  1176. }
  1177. #endif
  1178. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1179. {
  1180. struct inode *inode = file->f_path.dentry->d_inode;
  1181. struct shmem_inode_info *info = SHMEM_I(inode);
  1182. int retval = -ENOMEM;
  1183. spin_lock(&info->lock);
  1184. if (lock && !(info->flags & VM_LOCKED)) {
  1185. if (!user_shm_lock(inode->i_size, user))
  1186. goto out_nomem;
  1187. info->flags |= VM_LOCKED;
  1188. mapping_set_unevictable(file->f_mapping);
  1189. }
  1190. if (!lock && (info->flags & VM_LOCKED) && user) {
  1191. user_shm_unlock(inode->i_size, user);
  1192. info->flags &= ~VM_LOCKED;
  1193. mapping_clear_unevictable(file->f_mapping);
  1194. }
  1195. retval = 0;
  1196. out_nomem:
  1197. spin_unlock(&info->lock);
  1198. return retval;
  1199. }
  1200. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1201. {
  1202. file_accessed(file);
  1203. vma->vm_ops = &shmem_vm_ops;
  1204. return 0;
  1205. }
  1206. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1207. umode_t mode, dev_t dev, unsigned long flags)
  1208. {
  1209. struct inode *inode;
  1210. struct shmem_inode_info *info;
  1211. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1212. if (shmem_reserve_inode(sb))
  1213. return NULL;
  1214. inode = new_inode(sb);
  1215. if (inode) {
  1216. inode->i_ino = get_next_ino();
  1217. inode_init_owner(inode, dir, mode);
  1218. inode->i_blocks = 0;
  1219. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1220. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1221. inode->i_generation = get_seconds();
  1222. info = SHMEM_I(inode);
  1223. memset(info, 0, (char *)inode - (char *)info);
  1224. spin_lock_init(&info->lock);
  1225. info->flags = flags & VM_NORESERVE;
  1226. INIT_LIST_HEAD(&info->swaplist);
  1227. simple_xattrs_init(&info->xattrs);
  1228. cache_no_acl(inode);
  1229. switch (mode & S_IFMT) {
  1230. default:
  1231. inode->i_op = &shmem_special_inode_operations;
  1232. init_special_inode(inode, mode, dev);
  1233. break;
  1234. case S_IFREG:
  1235. inode->i_mapping->a_ops = &shmem_aops;
  1236. inode->i_op = &shmem_inode_operations;
  1237. inode->i_fop = &shmem_file_operations;
  1238. mpol_shared_policy_init(&info->policy,
  1239. shmem_get_sbmpol(sbinfo));
  1240. break;
  1241. case S_IFDIR:
  1242. inc_nlink(inode);
  1243. /* Some things misbehave if size == 0 on a directory */
  1244. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1245. inode->i_op = &shmem_dir_inode_operations;
  1246. inode->i_fop = &simple_dir_operations;
  1247. break;
  1248. case S_IFLNK:
  1249. /*
  1250. * Must not load anything in the rbtree,
  1251. * mpol_free_shared_policy will not be called.
  1252. */
  1253. mpol_shared_policy_init(&info->policy, NULL);
  1254. break;
  1255. }
  1256. } else
  1257. shmem_free_inode(sb);
  1258. return inode;
  1259. }
  1260. #ifdef CONFIG_TMPFS
  1261. static const struct inode_operations shmem_symlink_inode_operations;
  1262. static const struct inode_operations shmem_short_symlink_operations;
  1263. #ifdef CONFIG_TMPFS_XATTR
  1264. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1265. #else
  1266. #define shmem_initxattrs NULL
  1267. #endif
  1268. static int
  1269. shmem_write_begin(struct file *file, struct address_space *mapping,
  1270. loff_t pos, unsigned len, unsigned flags,
  1271. struct page **pagep, void **fsdata)
  1272. {
  1273. struct inode *inode = mapping->host;
  1274. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1275. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1276. }
  1277. static int
  1278. shmem_write_end(struct file *file, struct address_space *mapping,
  1279. loff_t pos, unsigned len, unsigned copied,
  1280. struct page *page, void *fsdata)
  1281. {
  1282. struct inode *inode = mapping->host;
  1283. if (pos + copied > inode->i_size)
  1284. i_size_write(inode, pos + copied);
  1285. if (!PageUptodate(page)) {
  1286. if (copied < PAGE_CACHE_SIZE) {
  1287. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1288. zero_user_segments(page, 0, from,
  1289. from + copied, PAGE_CACHE_SIZE);
  1290. }
  1291. SetPageUptodate(page);
  1292. }
  1293. set_page_dirty(page);
  1294. unlock_page(page);
  1295. page_cache_release(page);
  1296. return copied;
  1297. }
  1298. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1299. {
  1300. struct inode *inode = filp->f_path.dentry->d_inode;
  1301. struct address_space *mapping = inode->i_mapping;
  1302. pgoff_t index;
  1303. unsigned long offset;
  1304. enum sgp_type sgp = SGP_READ;
  1305. /*
  1306. * Might this read be for a stacking filesystem? Then when reading
  1307. * holes of a sparse file, we actually need to allocate those pages,
  1308. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1309. */
  1310. if (segment_eq(get_fs(), KERNEL_DS))
  1311. sgp = SGP_DIRTY;
  1312. index = *ppos >> PAGE_CACHE_SHIFT;
  1313. offset = *ppos & ~PAGE_CACHE_MASK;
  1314. for (;;) {
  1315. struct page *page = NULL;
  1316. pgoff_t end_index;
  1317. unsigned long nr, ret;
  1318. loff_t i_size = i_size_read(inode);
  1319. end_index = i_size >> PAGE_CACHE_SHIFT;
  1320. if (index > end_index)
  1321. break;
  1322. if (index == end_index) {
  1323. nr = i_size & ~PAGE_CACHE_MASK;
  1324. if (nr <= offset)
  1325. break;
  1326. }
  1327. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1328. if (desc->error) {
  1329. if (desc->error == -EINVAL)
  1330. desc->error = 0;
  1331. break;
  1332. }
  1333. if (page)
  1334. unlock_page(page);
  1335. /*
  1336. * We must evaluate after, since reads (unlike writes)
  1337. * are called without i_mutex protection against truncate
  1338. */
  1339. nr = PAGE_CACHE_SIZE;
  1340. i_size = i_size_read(inode);
  1341. end_index = i_size >> PAGE_CACHE_SHIFT;
  1342. if (index == end_index) {
  1343. nr = i_size & ~PAGE_CACHE_MASK;
  1344. if (nr <= offset) {
  1345. if (page)
  1346. page_cache_release(page);
  1347. break;
  1348. }
  1349. }
  1350. nr -= offset;
  1351. if (page) {
  1352. /*
  1353. * If users can be writing to this page using arbitrary
  1354. * virtual addresses, take care about potential aliasing
  1355. * before reading the page on the kernel side.
  1356. */
  1357. if (mapping_writably_mapped(mapping))
  1358. flush_dcache_page(page);
  1359. /*
  1360. * Mark the page accessed if we read the beginning.
  1361. */
  1362. if (!offset)
  1363. mark_page_accessed(page);
  1364. } else {
  1365. page = ZERO_PAGE(0);
  1366. page_cache_get(page);
  1367. }
  1368. /*
  1369. * Ok, we have the page, and it's up-to-date, so
  1370. * now we can copy it to user space...
  1371. *
  1372. * The actor routine returns how many bytes were actually used..
  1373. * NOTE! This may not be the same as how much of a user buffer
  1374. * we filled up (we may be padding etc), so we can only update
  1375. * "pos" here (the actor routine has to update the user buffer
  1376. * pointers and the remaining count).
  1377. */
  1378. ret = actor(desc, page, offset, nr);
  1379. offset += ret;
  1380. index += offset >> PAGE_CACHE_SHIFT;
  1381. offset &= ~PAGE_CACHE_MASK;
  1382. page_cache_release(page);
  1383. if (ret != nr || !desc->count)
  1384. break;
  1385. cond_resched();
  1386. }
  1387. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1388. file_accessed(filp);
  1389. }
  1390. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1391. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1392. {
  1393. struct file *filp = iocb->ki_filp;
  1394. ssize_t retval;
  1395. unsigned long seg;
  1396. size_t count;
  1397. loff_t *ppos = &iocb->ki_pos;
  1398. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1399. if (retval)
  1400. return retval;
  1401. for (seg = 0; seg < nr_segs; seg++) {
  1402. read_descriptor_t desc;
  1403. desc.written = 0;
  1404. desc.arg.buf = iov[seg].iov_base;
  1405. desc.count = iov[seg].iov_len;
  1406. if (desc.count == 0)
  1407. continue;
  1408. desc.error = 0;
  1409. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1410. retval += desc.written;
  1411. if (desc.error) {
  1412. retval = retval ?: desc.error;
  1413. break;
  1414. }
  1415. if (desc.count > 0)
  1416. break;
  1417. }
  1418. return retval;
  1419. }
  1420. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1421. struct pipe_inode_info *pipe, size_t len,
  1422. unsigned int flags)
  1423. {
  1424. struct address_space *mapping = in->f_mapping;
  1425. struct inode *inode = mapping->host;
  1426. unsigned int loff, nr_pages, req_pages;
  1427. struct page *pages[PIPE_DEF_BUFFERS];
  1428. struct partial_page partial[PIPE_DEF_BUFFERS];
  1429. struct page *page;
  1430. pgoff_t index, end_index;
  1431. loff_t isize, left;
  1432. int error, page_nr;
  1433. struct splice_pipe_desc spd = {
  1434. .pages = pages,
  1435. .partial = partial,
  1436. .nr_pages_max = PIPE_DEF_BUFFERS,
  1437. .flags = flags,
  1438. .ops = &page_cache_pipe_buf_ops,
  1439. .spd_release = spd_release_page,
  1440. };
  1441. isize = i_size_read(inode);
  1442. if (unlikely(*ppos >= isize))
  1443. return 0;
  1444. left = isize - *ppos;
  1445. if (unlikely(left < len))
  1446. len = left;
  1447. if (splice_grow_spd(pipe, &spd))
  1448. return -ENOMEM;
  1449. index = *ppos >> PAGE_CACHE_SHIFT;
  1450. loff = *ppos & ~PAGE_CACHE_MASK;
  1451. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1452. nr_pages = min(req_pages, pipe->buffers);
  1453. spd.nr_pages = find_get_pages_contig(mapping, index,
  1454. nr_pages, spd.pages);
  1455. index += spd.nr_pages;
  1456. error = 0;
  1457. while (spd.nr_pages < nr_pages) {
  1458. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1459. if (error)
  1460. break;
  1461. unlock_page(page);
  1462. spd.pages[spd.nr_pages++] = page;
  1463. index++;
  1464. }
  1465. index = *ppos >> PAGE_CACHE_SHIFT;
  1466. nr_pages = spd.nr_pages;
  1467. spd.nr_pages = 0;
  1468. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1469. unsigned int this_len;
  1470. if (!len)
  1471. break;
  1472. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1473. page = spd.pages[page_nr];
  1474. if (!PageUptodate(page) || page->mapping != mapping) {
  1475. error = shmem_getpage(inode, index, &page,
  1476. SGP_CACHE, NULL);
  1477. if (error)
  1478. break;
  1479. unlock_page(page);
  1480. page_cache_release(spd.pages[page_nr]);
  1481. spd.pages[page_nr] = page;
  1482. }
  1483. isize = i_size_read(inode);
  1484. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1485. if (unlikely(!isize || index > end_index))
  1486. break;
  1487. if (end_index == index) {
  1488. unsigned int plen;
  1489. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1490. if (plen <= loff)
  1491. break;
  1492. this_len = min(this_len, plen - loff);
  1493. len = this_len;
  1494. }
  1495. spd.partial[page_nr].offset = loff;
  1496. spd.partial[page_nr].len = this_len;
  1497. len -= this_len;
  1498. loff = 0;
  1499. spd.nr_pages++;
  1500. index++;
  1501. }
  1502. while (page_nr < nr_pages)
  1503. page_cache_release(spd.pages[page_nr++]);
  1504. if (spd.nr_pages)
  1505. error = splice_to_pipe(pipe, &spd);
  1506. splice_shrink_spd(&spd);
  1507. if (error > 0) {
  1508. *ppos += error;
  1509. file_accessed(in);
  1510. }
  1511. return error;
  1512. }
  1513. static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  1514. loff_t len)
  1515. {
  1516. struct inode *inode = file->f_path.dentry->d_inode;
  1517. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1518. struct shmem_falloc shmem_falloc;
  1519. pgoff_t start, index, end;
  1520. int error;
  1521. mutex_lock(&inode->i_mutex);
  1522. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1523. struct address_space *mapping = file->f_mapping;
  1524. loff_t unmap_start = round_up(offset, PAGE_SIZE);
  1525. loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
  1526. if ((u64)unmap_end > (u64)unmap_start)
  1527. unmap_mapping_range(mapping, unmap_start,
  1528. 1 + unmap_end - unmap_start, 0);
  1529. shmem_truncate_range(inode, offset, offset + len - 1);
  1530. /* No need to unmap again: hole-punching leaves COWed pages */
  1531. error = 0;
  1532. goto out;
  1533. }
  1534. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  1535. error = inode_newsize_ok(inode, offset + len);
  1536. if (error)
  1537. goto out;
  1538. start = offset >> PAGE_CACHE_SHIFT;
  1539. end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1540. /* Try to avoid a swapstorm if len is impossible to satisfy */
  1541. if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
  1542. error = -ENOSPC;
  1543. goto out;
  1544. }
  1545. shmem_falloc.start = start;
  1546. shmem_falloc.next = start;
  1547. shmem_falloc.nr_falloced = 0;
  1548. shmem_falloc.nr_unswapped = 0;
  1549. spin_lock(&inode->i_lock);
  1550. inode->i_private = &shmem_falloc;
  1551. spin_unlock(&inode->i_lock);
  1552. for (index = start; index < end; index++) {
  1553. struct page *page;
  1554. /*
  1555. * Good, the fallocate(2) manpage permits EINTR: we may have
  1556. * been interrupted because we are using up too much memory.
  1557. */
  1558. if (signal_pending(current))
  1559. error = -EINTR;
  1560. else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
  1561. error = -ENOMEM;
  1562. else
  1563. error = shmem_getpage(inode, index, &page, SGP_FALLOC,
  1564. NULL);
  1565. if (error) {
  1566. /* Remove the !PageUptodate pages we added */
  1567. shmem_undo_range(inode,
  1568. (loff_t)start << PAGE_CACHE_SHIFT,
  1569. (loff_t)index << PAGE_CACHE_SHIFT, true);
  1570. goto undone;
  1571. }
  1572. /*
  1573. * Inform shmem_writepage() how far we have reached.
  1574. * No need for lock or barrier: we have the page lock.
  1575. */
  1576. shmem_falloc.next++;
  1577. if (!PageUptodate(page))
  1578. shmem_falloc.nr_falloced++;
  1579. /*
  1580. * If !PageUptodate, leave it that way so that freeable pages
  1581. * can be recognized if we need to rollback on error later.
  1582. * But set_page_dirty so that memory pressure will swap rather
  1583. * than free the pages we are allocating (and SGP_CACHE pages
  1584. * might still be clean: we now need to mark those dirty too).
  1585. */
  1586. set_page_dirty(page);
  1587. unlock_page(page);
  1588. page_cache_release(page);
  1589. cond_resched();
  1590. }
  1591. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  1592. i_size_write(inode, offset + len);
  1593. inode->i_ctime = CURRENT_TIME;
  1594. undone:
  1595. spin_lock(&inode->i_lock);
  1596. inode->i_private = NULL;
  1597. spin_unlock(&inode->i_lock);
  1598. out:
  1599. mutex_unlock(&inode->i_mutex);
  1600. return error;
  1601. }
  1602. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1603. {
  1604. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1605. buf->f_type = TMPFS_MAGIC;
  1606. buf->f_bsize = PAGE_CACHE_SIZE;
  1607. buf->f_namelen = NAME_MAX;
  1608. if (sbinfo->max_blocks) {
  1609. buf->f_blocks = sbinfo->max_blocks;
  1610. buf->f_bavail =
  1611. buf->f_bfree = sbinfo->max_blocks -
  1612. percpu_counter_sum(&sbinfo->used_blocks);
  1613. }
  1614. if (sbinfo->max_inodes) {
  1615. buf->f_files = sbinfo->max_inodes;
  1616. buf->f_ffree = sbinfo->free_inodes;
  1617. }
  1618. /* else leave those fields 0 like simple_statfs */
  1619. return 0;
  1620. }
  1621. /*
  1622. * File creation. Allocate an inode, and we're done..
  1623. */
  1624. static int
  1625. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  1626. {
  1627. struct inode *inode;
  1628. int error = -ENOSPC;
  1629. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1630. if (inode) {
  1631. error = security_inode_init_security(inode, dir,
  1632. &dentry->d_name,
  1633. shmem_initxattrs, NULL);
  1634. if (error) {
  1635. if (error != -EOPNOTSUPP) {
  1636. iput(inode);
  1637. return error;
  1638. }
  1639. }
  1640. #ifdef CONFIG_TMPFS_POSIX_ACL
  1641. error = generic_acl_init(inode, dir);
  1642. if (error) {
  1643. iput(inode);
  1644. return error;
  1645. }
  1646. #else
  1647. error = 0;
  1648. #endif
  1649. dir->i_size += BOGO_DIRENT_SIZE;
  1650. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1651. d_instantiate(dentry, inode);
  1652. dget(dentry); /* Extra count - pin the dentry in core */
  1653. }
  1654. return error;
  1655. }
  1656. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1657. {
  1658. int error;
  1659. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1660. return error;
  1661. inc_nlink(dir);
  1662. return 0;
  1663. }
  1664. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1665. bool excl)
  1666. {
  1667. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1668. }
  1669. /*
  1670. * Link a file..
  1671. */
  1672. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1673. {
  1674. struct inode *inode = old_dentry->d_inode;
  1675. int ret;
  1676. /*
  1677. * No ordinary (disk based) filesystem counts links as inodes;
  1678. * but each new link needs a new dentry, pinning lowmem, and
  1679. * tmpfs dentries cannot be pruned until they are unlinked.
  1680. */
  1681. ret = shmem_reserve_inode(inode->i_sb);
  1682. if (ret)
  1683. goto out;
  1684. dir->i_size += BOGO_DIRENT_SIZE;
  1685. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1686. inc_nlink(inode);
  1687. ihold(inode); /* New dentry reference */
  1688. dget(dentry); /* Extra pinning count for the created dentry */
  1689. d_instantiate(dentry, inode);
  1690. out:
  1691. return ret;
  1692. }
  1693. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1694. {
  1695. struct inode *inode = dentry->d_inode;
  1696. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1697. shmem_free_inode(inode->i_sb);
  1698. dir->i_size -= BOGO_DIRENT_SIZE;
  1699. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1700. drop_nlink(inode);
  1701. dput(dentry); /* Undo the count from "create" - this does all the work */
  1702. return 0;
  1703. }
  1704. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1705. {
  1706. if (!simple_empty(dentry))
  1707. return -ENOTEMPTY;
  1708. drop_nlink(dentry->d_inode);
  1709. drop_nlink(dir);
  1710. return shmem_unlink(dir, dentry);
  1711. }
  1712. /*
  1713. * The VFS layer already does all the dentry stuff for rename,
  1714. * we just have to decrement the usage count for the target if
  1715. * it exists so that the VFS layer correctly free's it when it
  1716. * gets overwritten.
  1717. */
  1718. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1719. {
  1720. struct inode *inode = old_dentry->d_inode;
  1721. int they_are_dirs = S_ISDIR(inode->i_mode);
  1722. if (!simple_empty(new_dentry))
  1723. return -ENOTEMPTY;
  1724. if (new_dentry->d_inode) {
  1725. (void) shmem_unlink(new_dir, new_dentry);
  1726. if (they_are_dirs)
  1727. drop_nlink(old_dir);
  1728. } else if (they_are_dirs) {
  1729. drop_nlink(old_dir);
  1730. inc_nlink(new_dir);
  1731. }
  1732. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1733. new_dir->i_size += BOGO_DIRENT_SIZE;
  1734. old_dir->i_ctime = old_dir->i_mtime =
  1735. new_dir->i_ctime = new_dir->i_mtime =
  1736. inode->i_ctime = CURRENT_TIME;
  1737. return 0;
  1738. }
  1739. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1740. {
  1741. int error;
  1742. int len;
  1743. struct inode *inode;
  1744. struct page *page;
  1745. char *kaddr;
  1746. struct shmem_inode_info *info;
  1747. len = strlen(symname) + 1;
  1748. if (len > PAGE_CACHE_SIZE)
  1749. return -ENAMETOOLONG;
  1750. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1751. if (!inode)
  1752. return -ENOSPC;
  1753. error = security_inode_init_security(inode, dir, &dentry->d_name,
  1754. shmem_initxattrs, NULL);
  1755. if (error) {
  1756. if (error != -EOPNOTSUPP) {
  1757. iput(inode);
  1758. return error;
  1759. }
  1760. error = 0;
  1761. }
  1762. info = SHMEM_I(inode);
  1763. inode->i_size = len-1;
  1764. if (len <= SHORT_SYMLINK_LEN) {
  1765. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  1766. if (!info->symlink) {
  1767. iput(inode);
  1768. return -ENOMEM;
  1769. }
  1770. inode->i_op = &shmem_short_symlink_operations;
  1771. } else {
  1772. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1773. if (error) {
  1774. iput(inode);
  1775. return error;
  1776. }
  1777. inode->i_mapping->a_ops = &shmem_aops;
  1778. inode->i_op = &shmem_symlink_inode_operations;
  1779. kaddr = kmap_atomic(page);
  1780. memcpy(kaddr, symname, len);
  1781. kunmap_atomic(kaddr);
  1782. SetPageUptodate(page);
  1783. set_page_dirty(page);
  1784. unlock_page(page);
  1785. page_cache_release(page);
  1786. }
  1787. dir->i_size += BOGO_DIRENT_SIZE;
  1788. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1789. d_instantiate(dentry, inode);
  1790. dget(dentry);
  1791. return 0;
  1792. }
  1793. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  1794. {
  1795. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  1796. return NULL;
  1797. }
  1798. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1799. {
  1800. struct page *page = NULL;
  1801. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1802. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1803. if (page)
  1804. unlock_page(page);
  1805. return page;
  1806. }
  1807. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1808. {
  1809. if (!IS_ERR(nd_get_link(nd))) {
  1810. struct page *page = cookie;
  1811. kunmap(page);
  1812. mark_page_accessed(page);
  1813. page_cache_release(page);
  1814. }
  1815. }
  1816. #ifdef CONFIG_TMPFS_XATTR
  1817. /*
  1818. * Superblocks without xattr inode operations may get some security.* xattr
  1819. * support from the LSM "for free". As soon as we have any other xattrs
  1820. * like ACLs, we also need to implement the security.* handlers at
  1821. * filesystem level, though.
  1822. */
  1823. /*
  1824. * Callback for security_inode_init_security() for acquiring xattrs.
  1825. */
  1826. static int shmem_initxattrs(struct inode *inode,
  1827. const struct xattr *xattr_array,
  1828. void *fs_info)
  1829. {
  1830. struct shmem_inode_info *info = SHMEM_I(inode);
  1831. const struct xattr *xattr;
  1832. struct simple_xattr *new_xattr;
  1833. size_t len;
  1834. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  1835. new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
  1836. if (!new_xattr)
  1837. return -ENOMEM;
  1838. len = strlen(xattr->name) + 1;
  1839. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  1840. GFP_KERNEL);
  1841. if (!new_xattr->name) {
  1842. kfree(new_xattr);
  1843. return -ENOMEM;
  1844. }
  1845. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  1846. XATTR_SECURITY_PREFIX_LEN);
  1847. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  1848. xattr->name, len);
  1849. simple_xattr_list_add(&info->xattrs, new_xattr);
  1850. }
  1851. return 0;
  1852. }
  1853. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1854. #ifdef CONFIG_TMPFS_POSIX_ACL
  1855. &generic_acl_access_handler,
  1856. &generic_acl_default_handler,
  1857. #endif
  1858. NULL
  1859. };
  1860. static int shmem_xattr_validate(const char *name)
  1861. {
  1862. struct { const char *prefix; size_t len; } arr[] = {
  1863. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1864. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1865. };
  1866. int i;
  1867. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1868. size_t preflen = arr[i].len;
  1869. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1870. if (!name[preflen])
  1871. return -EINVAL;
  1872. return 0;
  1873. }
  1874. }
  1875. return -EOPNOTSUPP;
  1876. }
  1877. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1878. void *buffer, size_t size)
  1879. {
  1880. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1881. int err;
  1882. /*
  1883. * If this is a request for a synthetic attribute in the system.*
  1884. * namespace use the generic infrastructure to resolve a handler
  1885. * for it via sb->s_xattr.
  1886. */
  1887. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1888. return generic_getxattr(dentry, name, buffer, size);
  1889. err = shmem_xattr_validate(name);
  1890. if (err)
  1891. return err;
  1892. return simple_xattr_get(&info->xattrs, name, buffer, size);
  1893. }
  1894. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1895. const void *value, size_t size, int flags)
  1896. {
  1897. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1898. int err;
  1899. /*
  1900. * If this is a request for a synthetic attribute in the system.*
  1901. * namespace use the generic infrastructure to resolve a handler
  1902. * for it via sb->s_xattr.
  1903. */
  1904. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1905. return generic_setxattr(dentry, name, value, size, flags);
  1906. err = shmem_xattr_validate(name);
  1907. if (err)
  1908. return err;
  1909. return simple_xattr_set(&info->xattrs, name, value, size, flags);
  1910. }
  1911. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1912. {
  1913. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1914. int err;
  1915. /*
  1916. * If this is a request for a synthetic attribute in the system.*
  1917. * namespace use the generic infrastructure to resolve a handler
  1918. * for it via sb->s_xattr.
  1919. */
  1920. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1921. return generic_removexattr(dentry, name);
  1922. err = shmem_xattr_validate(name);
  1923. if (err)
  1924. return err;
  1925. return simple_xattr_remove(&info->xattrs, name);
  1926. }
  1927. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1928. {
  1929. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1930. return simple_xattr_list(&info->xattrs, buffer, size);
  1931. }
  1932. #endif /* CONFIG_TMPFS_XATTR */
  1933. static const struct inode_operations shmem_short_symlink_operations = {
  1934. .readlink = generic_readlink,
  1935. .follow_link = shmem_follow_short_symlink,
  1936. #ifdef CONFIG_TMPFS_XATTR
  1937. .setxattr = shmem_setxattr,
  1938. .getxattr = shmem_getxattr,
  1939. .listxattr = shmem_listxattr,
  1940. .removexattr = shmem_removexattr,
  1941. #endif
  1942. };
  1943. static const struct inode_operations shmem_symlink_inode_operations = {
  1944. .readlink = generic_readlink,
  1945. .follow_link = shmem_follow_link,
  1946. .put_link = shmem_put_link,
  1947. #ifdef CONFIG_TMPFS_XATTR
  1948. .setxattr = shmem_setxattr,
  1949. .getxattr = shmem_getxattr,
  1950. .listxattr = shmem_listxattr,
  1951. .removexattr = shmem_removexattr,
  1952. #endif
  1953. };
  1954. static struct dentry *shmem_get_parent(struct dentry *child)
  1955. {
  1956. return ERR_PTR(-ESTALE);
  1957. }
  1958. static int shmem_match(struct inode *ino, void *vfh)
  1959. {
  1960. __u32 *fh = vfh;
  1961. __u64 inum = fh[2];
  1962. inum = (inum << 32) | fh[1];
  1963. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1964. }
  1965. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1966. struct fid *fid, int fh_len, int fh_type)
  1967. {
  1968. struct inode *inode;
  1969. struct dentry *dentry = NULL;
  1970. u64 inum = fid->raw[2];
  1971. inum = (inum << 32) | fid->raw[1];
  1972. if (fh_len < 3)
  1973. return NULL;
  1974. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1975. shmem_match, fid->raw);
  1976. if (inode) {
  1977. dentry = d_find_alias(inode);
  1978. iput(inode);
  1979. }
  1980. return dentry;
  1981. }
  1982. static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
  1983. struct inode *parent)
  1984. {
  1985. if (*len < 3) {
  1986. *len = 3;
  1987. return 255;
  1988. }
  1989. if (inode_unhashed(inode)) {
  1990. /* Unfortunately insert_inode_hash is not idempotent,
  1991. * so as we hash inodes here rather than at creation
  1992. * time, we need a lock to ensure we only try
  1993. * to do it once
  1994. */
  1995. static DEFINE_SPINLOCK(lock);
  1996. spin_lock(&lock);
  1997. if (inode_unhashed(inode))
  1998. __insert_inode_hash(inode,
  1999. inode->i_ino + inode->i_generation);
  2000. spin_unlock(&lock);
  2001. }
  2002. fh[0] = inode->i_generation;
  2003. fh[1] = inode->i_ino;
  2004. fh[2] = ((__u64)inode->i_ino) >> 32;
  2005. *len = 3;
  2006. return 1;
  2007. }
  2008. static const struct export_operations shmem_export_ops = {
  2009. .get_parent = shmem_get_parent,
  2010. .encode_fh = shmem_encode_fh,
  2011. .fh_to_dentry = shmem_fh_to_dentry,
  2012. };
  2013. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2014. bool remount)
  2015. {
  2016. char *this_char, *value, *rest;
  2017. uid_t uid;
  2018. gid_t gid;
  2019. while (options != NULL) {
  2020. this_char = options;
  2021. for (;;) {
  2022. /*
  2023. * NUL-terminate this option: unfortunately,
  2024. * mount options form a comma-separated list,
  2025. * but mpol's nodelist may also contain commas.
  2026. */
  2027. options = strchr(options, ',');
  2028. if (options == NULL)
  2029. break;
  2030. options++;
  2031. if (!isdigit(*options)) {
  2032. options[-1] = '\0';
  2033. break;
  2034. }
  2035. }
  2036. if (!*this_char)
  2037. continue;
  2038. if ((value = strchr(this_char,'=')) != NULL) {
  2039. *value++ = 0;
  2040. } else {
  2041. printk(KERN_ERR
  2042. "tmpfs: No value for mount option '%s'\n",
  2043. this_char);
  2044. return 1;
  2045. }
  2046. if (!strcmp(this_char,"size")) {
  2047. unsigned long long size;
  2048. size = memparse(value,&rest);
  2049. if (*rest == '%') {
  2050. size <<= PAGE_SHIFT;
  2051. size *= totalram_pages;
  2052. do_div(size, 100);
  2053. rest++;
  2054. }
  2055. if (*rest)
  2056. goto bad_val;
  2057. sbinfo->max_blocks =
  2058. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  2059. } else if (!strcmp(this_char,"nr_blocks")) {
  2060. sbinfo->max_blocks = memparse(value, &rest);
  2061. if (*rest)
  2062. goto bad_val;
  2063. } else if (!strcmp(this_char,"nr_inodes")) {
  2064. sbinfo->max_inodes = memparse(value, &rest);
  2065. if (*rest)
  2066. goto bad_val;
  2067. } else if (!strcmp(this_char,"mode")) {
  2068. if (remount)
  2069. continue;
  2070. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2071. if (*rest)
  2072. goto bad_val;
  2073. } else if (!strcmp(this_char,"uid")) {
  2074. if (remount)
  2075. continue;
  2076. uid = simple_strtoul(value, &rest, 0);
  2077. if (*rest)
  2078. goto bad_val;
  2079. sbinfo->uid = make_kuid(current_user_ns(), uid);
  2080. if (!uid_valid(sbinfo->uid))
  2081. goto bad_val;
  2082. } else if (!strcmp(this_char,"gid")) {
  2083. if (remount)
  2084. continue;
  2085. gid = simple_strtoul(value, &rest, 0);
  2086. if (*rest)
  2087. goto bad_val;
  2088. sbinfo->gid = make_kgid(current_user_ns(), gid);
  2089. if (!gid_valid(sbinfo->gid))
  2090. goto bad_val;
  2091. } else if (!strcmp(this_char,"mpol")) {
  2092. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  2093. goto bad_val;
  2094. } else {
  2095. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2096. this_char);
  2097. return 1;
  2098. }
  2099. }
  2100. return 0;
  2101. bad_val:
  2102. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2103. value, this_char);
  2104. return 1;
  2105. }
  2106. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2107. {
  2108. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2109. struct shmem_sb_info config = *sbinfo;
  2110. unsigned long inodes;
  2111. int error = -EINVAL;
  2112. if (shmem_parse_options(data, &config, true))
  2113. return error;
  2114. spin_lock(&sbinfo->stat_lock);
  2115. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2116. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  2117. goto out;
  2118. if (config.max_inodes < inodes)
  2119. goto out;
  2120. /*
  2121. * Those tests disallow limited->unlimited while any are in use;
  2122. * but we must separately disallow unlimited->limited, because
  2123. * in that case we have no record of how much is already in use.
  2124. */
  2125. if (config.max_blocks && !sbinfo->max_blocks)
  2126. goto out;
  2127. if (config.max_inodes && !sbinfo->max_inodes)
  2128. goto out;
  2129. error = 0;
  2130. sbinfo->max_blocks = config.max_blocks;
  2131. sbinfo->max_inodes = config.max_inodes;
  2132. sbinfo->free_inodes = config.max_inodes - inodes;
  2133. mpol_put(sbinfo->mpol);
  2134. sbinfo->mpol = config.mpol; /* transfers initial ref */
  2135. out:
  2136. spin_unlock(&sbinfo->stat_lock);
  2137. return error;
  2138. }
  2139. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  2140. {
  2141. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  2142. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2143. seq_printf(seq, ",size=%luk",
  2144. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2145. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2146. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2147. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2148. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  2149. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  2150. seq_printf(seq, ",uid=%u",
  2151. from_kuid_munged(&init_user_ns, sbinfo->uid));
  2152. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  2153. seq_printf(seq, ",gid=%u",
  2154. from_kgid_munged(&init_user_ns, sbinfo->gid));
  2155. shmem_show_mpol(seq, sbinfo->mpol);
  2156. return 0;
  2157. }
  2158. #endif /* CONFIG_TMPFS */
  2159. static void shmem_put_super(struct super_block *sb)
  2160. {
  2161. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2162. percpu_counter_destroy(&sbinfo->used_blocks);
  2163. kfree(sbinfo);
  2164. sb->s_fs_info = NULL;
  2165. }
  2166. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  2167. {
  2168. struct inode *inode;
  2169. struct shmem_sb_info *sbinfo;
  2170. int err = -ENOMEM;
  2171. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2172. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  2173. L1_CACHE_BYTES), GFP_KERNEL);
  2174. if (!sbinfo)
  2175. return -ENOMEM;
  2176. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2177. sbinfo->uid = current_fsuid();
  2178. sbinfo->gid = current_fsgid();
  2179. sb->s_fs_info = sbinfo;
  2180. #ifdef CONFIG_TMPFS
  2181. /*
  2182. * Per default we only allow half of the physical ram per
  2183. * tmpfs instance, limiting inodes to one per page of lowmem;
  2184. * but the internal instance is left unlimited.
  2185. */
  2186. if (!(sb->s_flags & MS_NOUSER)) {
  2187. sbinfo->max_blocks = shmem_default_max_blocks();
  2188. sbinfo->max_inodes = shmem_default_max_inodes();
  2189. if (shmem_parse_options(data, sbinfo, false)) {
  2190. err = -EINVAL;
  2191. goto failed;
  2192. }
  2193. }
  2194. sb->s_export_op = &shmem_export_ops;
  2195. sb->s_flags |= MS_NOSEC;
  2196. #else
  2197. sb->s_flags |= MS_NOUSER;
  2198. #endif
  2199. spin_lock_init(&sbinfo->stat_lock);
  2200. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  2201. goto failed;
  2202. sbinfo->free_inodes = sbinfo->max_inodes;
  2203. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2204. sb->s_blocksize = PAGE_CACHE_SIZE;
  2205. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2206. sb->s_magic = TMPFS_MAGIC;
  2207. sb->s_op = &shmem_ops;
  2208. sb->s_time_gran = 1;
  2209. #ifdef CONFIG_TMPFS_XATTR
  2210. sb->s_xattr = shmem_xattr_handlers;
  2211. #endif
  2212. #ifdef CONFIG_TMPFS_POSIX_ACL
  2213. sb->s_flags |= MS_POSIXACL;
  2214. #endif
  2215. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2216. if (!inode)
  2217. goto failed;
  2218. inode->i_uid = sbinfo->uid;
  2219. inode->i_gid = sbinfo->gid;
  2220. sb->s_root = d_make_root(inode);
  2221. if (!sb->s_root)
  2222. goto failed;
  2223. return 0;
  2224. failed:
  2225. shmem_put_super(sb);
  2226. return err;
  2227. }
  2228. static struct kmem_cache *shmem_inode_cachep;
  2229. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2230. {
  2231. struct shmem_inode_info *info;
  2232. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2233. if (!info)
  2234. return NULL;
  2235. return &info->vfs_inode;
  2236. }
  2237. static void shmem_destroy_callback(struct rcu_head *head)
  2238. {
  2239. struct inode *inode = container_of(head, struct inode, i_rcu);
  2240. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2241. }
  2242. static void shmem_destroy_inode(struct inode *inode)
  2243. {
  2244. if (S_ISREG(inode->i_mode))
  2245. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2246. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2247. }
  2248. static void shmem_init_inode(void *foo)
  2249. {
  2250. struct shmem_inode_info *info = foo;
  2251. inode_init_once(&info->vfs_inode);
  2252. }
  2253. static int shmem_init_inodecache(void)
  2254. {
  2255. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2256. sizeof(struct shmem_inode_info),
  2257. 0, SLAB_PANIC, shmem_init_inode);
  2258. return 0;
  2259. }
  2260. static void shmem_destroy_inodecache(void)
  2261. {
  2262. kmem_cache_destroy(shmem_inode_cachep);
  2263. }
  2264. static const struct address_space_operations shmem_aops = {
  2265. .writepage = shmem_writepage,
  2266. .set_page_dirty = __set_page_dirty_no_writeback,
  2267. #ifdef CONFIG_TMPFS
  2268. .write_begin = shmem_write_begin,
  2269. .write_end = shmem_write_end,
  2270. #endif
  2271. .migratepage = migrate_page,
  2272. .error_remove_page = generic_error_remove_page,
  2273. };
  2274. static const struct file_operations shmem_file_operations = {
  2275. .mmap = shmem_mmap,
  2276. #ifdef CONFIG_TMPFS
  2277. .llseek = generic_file_llseek,
  2278. .read = do_sync_read,
  2279. .write = do_sync_write,
  2280. .aio_read = shmem_file_aio_read,
  2281. .aio_write = generic_file_aio_write,
  2282. .fsync = noop_fsync,
  2283. .splice_read = shmem_file_splice_read,
  2284. .splice_write = generic_file_splice_write,
  2285. .fallocate = shmem_fallocate,
  2286. #endif
  2287. };
  2288. static const struct inode_operations shmem_inode_operations = {
  2289. .setattr = shmem_setattr,
  2290. #ifdef CONFIG_TMPFS_XATTR
  2291. .setxattr = shmem_setxattr,
  2292. .getxattr = shmem_getxattr,
  2293. .listxattr = shmem_listxattr,
  2294. .removexattr = shmem_removexattr,
  2295. #endif
  2296. };
  2297. static const struct inode_operations shmem_dir_inode_operations = {
  2298. #ifdef CONFIG_TMPFS
  2299. .create = shmem_create,
  2300. .lookup = simple_lookup,
  2301. .link = shmem_link,
  2302. .unlink = shmem_unlink,
  2303. .symlink = shmem_symlink,
  2304. .mkdir = shmem_mkdir,
  2305. .rmdir = shmem_rmdir,
  2306. .mknod = shmem_mknod,
  2307. .rename = shmem_rename,
  2308. #endif
  2309. #ifdef CONFIG_TMPFS_XATTR
  2310. .setxattr = shmem_setxattr,
  2311. .getxattr = shmem_getxattr,
  2312. .listxattr = shmem_listxattr,
  2313. .removexattr = shmem_removexattr,
  2314. #endif
  2315. #ifdef CONFIG_TMPFS_POSIX_ACL
  2316. .setattr = shmem_setattr,
  2317. #endif
  2318. };
  2319. static const struct inode_operations shmem_special_inode_operations = {
  2320. #ifdef CONFIG_TMPFS_XATTR
  2321. .setxattr = shmem_setxattr,
  2322. .getxattr = shmem_getxattr,
  2323. .listxattr = shmem_listxattr,
  2324. .removexattr = shmem_removexattr,
  2325. #endif
  2326. #ifdef CONFIG_TMPFS_POSIX_ACL
  2327. .setattr = shmem_setattr,
  2328. #endif
  2329. };
  2330. static const struct super_operations shmem_ops = {
  2331. .alloc_inode = shmem_alloc_inode,
  2332. .destroy_inode = shmem_destroy_inode,
  2333. #ifdef CONFIG_TMPFS
  2334. .statfs = shmem_statfs,
  2335. .remount_fs = shmem_remount_fs,
  2336. .show_options = shmem_show_options,
  2337. #endif
  2338. .evict_inode = shmem_evict_inode,
  2339. .drop_inode = generic_delete_inode,
  2340. .put_super = shmem_put_super,
  2341. };
  2342. static const struct vm_operations_struct shmem_vm_ops = {
  2343. .fault = shmem_fault,
  2344. #ifdef CONFIG_NUMA
  2345. .set_policy = shmem_set_policy,
  2346. .get_policy = shmem_get_policy,
  2347. #endif
  2348. .remap_pages = generic_file_remap_pages,
  2349. };
  2350. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2351. int flags, const char *dev_name, void *data)
  2352. {
  2353. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2354. }
  2355. static struct file_system_type shmem_fs_type = {
  2356. .owner = THIS_MODULE,
  2357. .name = "tmpfs",
  2358. .mount = shmem_mount,
  2359. .kill_sb = kill_litter_super,
  2360. };
  2361. int __init shmem_init(void)
  2362. {
  2363. int error;
  2364. error = bdi_init(&shmem_backing_dev_info);
  2365. if (error)
  2366. goto out4;
  2367. error = shmem_init_inodecache();
  2368. if (error)
  2369. goto out3;
  2370. error = register_filesystem(&shmem_fs_type);
  2371. if (error) {
  2372. printk(KERN_ERR "Could not register tmpfs\n");
  2373. goto out2;
  2374. }
  2375. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2376. shmem_fs_type.name, NULL);
  2377. if (IS_ERR(shm_mnt)) {
  2378. error = PTR_ERR(shm_mnt);
  2379. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2380. goto out1;
  2381. }
  2382. return 0;
  2383. out1:
  2384. unregister_filesystem(&shmem_fs_type);
  2385. out2:
  2386. shmem_destroy_inodecache();
  2387. out3:
  2388. bdi_destroy(&shmem_backing_dev_info);
  2389. out4:
  2390. shm_mnt = ERR_PTR(error);
  2391. return error;
  2392. }
  2393. #else /* !CONFIG_SHMEM */
  2394. /*
  2395. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2396. *
  2397. * This is intended for small system where the benefits of the full
  2398. * shmem code (swap-backed and resource-limited) are outweighed by
  2399. * their complexity. On systems without swap this code should be
  2400. * effectively equivalent, but much lighter weight.
  2401. */
  2402. #include <linux/ramfs.h>
  2403. static struct file_system_type shmem_fs_type = {
  2404. .name = "tmpfs",
  2405. .mount = ramfs_mount,
  2406. .kill_sb = kill_litter_super,
  2407. };
  2408. int __init shmem_init(void)
  2409. {
  2410. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2411. shm_mnt = kern_mount(&shmem_fs_type);
  2412. BUG_ON(IS_ERR(shm_mnt));
  2413. return 0;
  2414. }
  2415. int shmem_unuse(swp_entry_t swap, struct page *page)
  2416. {
  2417. return 0;
  2418. }
  2419. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2420. {
  2421. return 0;
  2422. }
  2423. void shmem_unlock_mapping(struct address_space *mapping)
  2424. {
  2425. }
  2426. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2427. {
  2428. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2429. }
  2430. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2431. #define shmem_vm_ops generic_file_vm_ops
  2432. #define shmem_file_operations ramfs_file_operations
  2433. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2434. #define shmem_acct_size(flags, size) 0
  2435. #define shmem_unacct_size(flags, size) do {} while (0)
  2436. #endif /* CONFIG_SHMEM */
  2437. /* common code */
  2438. /**
  2439. * shmem_file_setup - get an unlinked file living in tmpfs
  2440. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2441. * @size: size to be set for the file
  2442. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2443. */
  2444. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2445. {
  2446. int error;
  2447. struct file *file;
  2448. struct inode *inode;
  2449. struct path path;
  2450. struct dentry *root;
  2451. struct qstr this;
  2452. if (IS_ERR(shm_mnt))
  2453. return (void *)shm_mnt;
  2454. if (size < 0 || size > MAX_LFS_FILESIZE)
  2455. return ERR_PTR(-EINVAL);
  2456. if (shmem_acct_size(flags, size))
  2457. return ERR_PTR(-ENOMEM);
  2458. error = -ENOMEM;
  2459. this.name = name;
  2460. this.len = strlen(name);
  2461. this.hash = 0; /* will go */
  2462. root = shm_mnt->mnt_root;
  2463. path.dentry = d_alloc(root, &this);
  2464. if (!path.dentry)
  2465. goto put_memory;
  2466. path.mnt = mntget(shm_mnt);
  2467. error = -ENOSPC;
  2468. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2469. if (!inode)
  2470. goto put_dentry;
  2471. d_instantiate(path.dentry, inode);
  2472. inode->i_size = size;
  2473. clear_nlink(inode); /* It is unlinked */
  2474. #ifndef CONFIG_MMU
  2475. error = ramfs_nommu_expand_for_mapping(inode, size);
  2476. if (error)
  2477. goto put_dentry;
  2478. #endif
  2479. error = -ENFILE;
  2480. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2481. &shmem_file_operations);
  2482. if (!file)
  2483. goto put_dentry;
  2484. return file;
  2485. put_dentry:
  2486. path_put(&path);
  2487. put_memory:
  2488. shmem_unacct_size(flags, size);
  2489. return ERR_PTR(error);
  2490. }
  2491. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2492. /**
  2493. * shmem_zero_setup - setup a shared anonymous mapping
  2494. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2495. */
  2496. int shmem_zero_setup(struct vm_area_struct *vma)
  2497. {
  2498. struct file *file;
  2499. loff_t size = vma->vm_end - vma->vm_start;
  2500. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2501. if (IS_ERR(file))
  2502. return PTR_ERR(file);
  2503. if (vma->vm_file)
  2504. fput(vma->vm_file);
  2505. vma->vm_file = file;
  2506. vma->vm_ops = &shmem_vm_ops;
  2507. return 0;
  2508. }
  2509. /**
  2510. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2511. * @mapping: the page's address_space
  2512. * @index: the page index
  2513. * @gfp: the page allocator flags to use if allocating
  2514. *
  2515. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2516. * with any new page allocations done using the specified allocation flags.
  2517. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2518. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2519. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2520. *
  2521. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2522. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2523. */
  2524. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2525. pgoff_t index, gfp_t gfp)
  2526. {
  2527. #ifdef CONFIG_SHMEM
  2528. struct inode *inode = mapping->host;
  2529. struct page *page;
  2530. int error;
  2531. BUG_ON(mapping->a_ops != &shmem_aops);
  2532. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2533. if (error)
  2534. page = ERR_PTR(error);
  2535. else
  2536. unlock_page(page);
  2537. return page;
  2538. #else
  2539. /*
  2540. * The tiny !SHMEM case uses ramfs without swap
  2541. */
  2542. return read_cache_page_gfp(mapping, index, gfp);
  2543. #endif
  2544. }
  2545. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);