shmem.c 78 KB

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