shmem.c 78 KB

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