filemap.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203
  1. /*
  2. * linux/mm/filemap.c
  3. *
  4. * Copyright (C) 1994-1999 Linus Torvalds
  5. */
  6. /*
  7. * This file handles the generic file mmap semantics used by
  8. * most "normal" filesystems (but you don't /have/ to use this:
  9. * the NFS filesystem used to do this differently, for example)
  10. */
  11. #include <linux/export.h>
  12. #include <linux/compiler.h>
  13. #include <linux/dax.h>
  14. #include <linux/fs.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/capability.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/gfp.h>
  20. #include <linux/mm.h>
  21. #include <linux/swap.h>
  22. #include <linux/mman.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/file.h>
  25. #include <linux/uio.h>
  26. #include <linux/hash.h>
  27. #include <linux/writeback.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/security.h>
  32. #include <linux/cpuset.h>
  33. #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */
  34. #include <linux/hugetlb.h>
  35. #include <linux/memcontrol.h>
  36. #include <linux/cleancache.h>
  37. #include <linux/rmap.h>
  38. #include "internal.h"
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/filemap.h>
  41. /*
  42. * FIXME: remove all knowledge of the buffer layer from the core VM
  43. */
  44. #include <linux/buffer_head.h> /* for try_to_free_buffers */
  45. #include <asm/mman.h>
  46. /*
  47. * Shared mappings implemented 30.11.1994. It's not fully working yet,
  48. * though.
  49. *
  50. * Shared mappings now work. 15.8.1995 Bruno.
  51. *
  52. * finished 'unifying' the page and buffer cache and SMP-threaded the
  53. * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
  54. *
  55. * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
  56. */
  57. /*
  58. * Lock ordering:
  59. *
  60. * ->i_mmap_rwsem (truncate_pagecache)
  61. * ->private_lock (__free_pte->__set_page_dirty_buffers)
  62. * ->swap_lock (exclusive_swap_page, others)
  63. * ->mapping->tree_lock
  64. *
  65. * ->i_mutex
  66. * ->i_mmap_rwsem (truncate->unmap_mapping_range)
  67. *
  68. * ->mmap_sem
  69. * ->i_mmap_rwsem
  70. * ->page_table_lock or pte_lock (various, mainly in memory.c)
  71. * ->mapping->tree_lock (arch-dependent flush_dcache_mmap_lock)
  72. *
  73. * ->mmap_sem
  74. * ->lock_page (access_process_vm)
  75. *
  76. * ->i_mutex (generic_perform_write)
  77. * ->mmap_sem (fault_in_pages_readable->do_page_fault)
  78. *
  79. * bdi->wb.list_lock
  80. * sb_lock (fs/fs-writeback.c)
  81. * ->mapping->tree_lock (__sync_single_inode)
  82. *
  83. * ->i_mmap_rwsem
  84. * ->anon_vma.lock (vma_adjust)
  85. *
  86. * ->anon_vma.lock
  87. * ->page_table_lock or pte_lock (anon_vma_prepare and various)
  88. *
  89. * ->page_table_lock or pte_lock
  90. * ->swap_lock (try_to_unmap_one)
  91. * ->private_lock (try_to_unmap_one)
  92. * ->tree_lock (try_to_unmap_one)
  93. * ->zone_lru_lock(zone) (follow_page->mark_page_accessed)
  94. * ->zone_lru_lock(zone) (check_pte_range->isolate_lru_page)
  95. * ->private_lock (page_remove_rmap->set_page_dirty)
  96. * ->tree_lock (page_remove_rmap->set_page_dirty)
  97. * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
  98. * ->inode->i_lock (page_remove_rmap->set_page_dirty)
  99. * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
  100. * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
  101. * ->inode->i_lock (zap_pte_range->set_page_dirty)
  102. * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
  103. *
  104. * ->i_mmap_rwsem
  105. * ->tasklist_lock (memory_failure, collect_procs_ao)
  106. */
  107. static int page_cache_tree_insert(struct address_space *mapping,
  108. struct page *page, void **shadowp)
  109. {
  110. struct radix_tree_node *node;
  111. void **slot;
  112. int error;
  113. error = __radix_tree_create(&mapping->page_tree, page->index, 0,
  114. &node, &slot);
  115. if (error)
  116. return error;
  117. if (*slot) {
  118. void *p;
  119. p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  120. if (!radix_tree_exceptional_entry(p))
  121. return -EEXIST;
  122. mapping->nrexceptional--;
  123. if (shadowp)
  124. *shadowp = p;
  125. }
  126. __radix_tree_replace(&mapping->page_tree, node, slot, page,
  127. workingset_update_node, mapping);
  128. mapping->nrpages++;
  129. return 0;
  130. }
  131. static void page_cache_tree_delete(struct address_space *mapping,
  132. struct page *page, void *shadow)
  133. {
  134. int i, nr;
  135. /* hugetlb pages are represented by one entry in the radix tree */
  136. nr = PageHuge(page) ? 1 : hpage_nr_pages(page);
  137. VM_BUG_ON_PAGE(!PageLocked(page), page);
  138. VM_BUG_ON_PAGE(PageTail(page), page);
  139. VM_BUG_ON_PAGE(nr != 1 && shadow, page);
  140. for (i = 0; i < nr; i++) {
  141. struct radix_tree_node *node;
  142. void **slot;
  143. __radix_tree_lookup(&mapping->page_tree, page->index + i,
  144. &node, &slot);
  145. VM_BUG_ON_PAGE(!node && nr != 1, page);
  146. radix_tree_clear_tags(&mapping->page_tree, node, slot);
  147. __radix_tree_replace(&mapping->page_tree, node, slot, shadow,
  148. workingset_update_node, mapping);
  149. }
  150. if (shadow) {
  151. mapping->nrexceptional += nr;
  152. /*
  153. * Make sure the nrexceptional update is committed before
  154. * the nrpages update so that final truncate racing
  155. * with reclaim does not see both counters 0 at the
  156. * same time and miss a shadow entry.
  157. */
  158. smp_wmb();
  159. }
  160. mapping->nrpages -= nr;
  161. }
  162. /*
  163. * Delete a page from the page cache and free it. Caller has to make
  164. * sure the page is locked and that nobody else uses it - or that usage
  165. * is safe. The caller must hold the mapping's tree_lock.
  166. */
  167. void __delete_from_page_cache(struct page *page, void *shadow)
  168. {
  169. struct address_space *mapping = page->mapping;
  170. int nr = hpage_nr_pages(page);
  171. trace_mm_filemap_delete_from_page_cache(page);
  172. /*
  173. * if we're uptodate, flush out into the cleancache, otherwise
  174. * invalidate any existing cleancache entries. We can't leave
  175. * stale data around in the cleancache once our page is gone
  176. */
  177. if (PageUptodate(page) && PageMappedToDisk(page))
  178. cleancache_put_page(page);
  179. else
  180. cleancache_invalidate_page(mapping, page);
  181. VM_BUG_ON_PAGE(PageTail(page), page);
  182. VM_BUG_ON_PAGE(page_mapped(page), page);
  183. if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
  184. int mapcount;
  185. pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
  186. current->comm, page_to_pfn(page));
  187. dump_page(page, "still mapped when deleted");
  188. dump_stack();
  189. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  190. mapcount = page_mapcount(page);
  191. if (mapping_exiting(mapping) &&
  192. page_count(page) >= mapcount + 2) {
  193. /*
  194. * All vmas have already been torn down, so it's
  195. * a good bet that actually the page is unmapped,
  196. * and we'd prefer not to leak it: if we're wrong,
  197. * some other bad page check should catch it later.
  198. */
  199. page_mapcount_reset(page);
  200. page_ref_sub(page, mapcount);
  201. }
  202. }
  203. page_cache_tree_delete(mapping, page, shadow);
  204. page->mapping = NULL;
  205. /* Leave page->index set: truncation lookup relies upon it */
  206. /* hugetlb pages do not participate in page cache accounting. */
  207. if (PageHuge(page))
  208. return;
  209. __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
  210. if (PageSwapBacked(page)) {
  211. __mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);
  212. if (PageTransHuge(page))
  213. __dec_node_page_state(page, NR_SHMEM_THPS);
  214. } else {
  215. VM_BUG_ON_PAGE(PageTransHuge(page), page);
  216. }
  217. /*
  218. * At this point page must be either written or cleaned by truncate.
  219. * Dirty page here signals a bug and loss of unwritten data.
  220. *
  221. * This fixes dirty accounting after removing the page entirely but
  222. * leaves PageDirty set: it has no effect for truncated page and
  223. * anyway will be cleared before returning page into buddy allocator.
  224. */
  225. if (WARN_ON_ONCE(PageDirty(page)))
  226. account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
  227. }
  228. /**
  229. * delete_from_page_cache - delete page from page cache
  230. * @page: the page which the kernel is trying to remove from page cache
  231. *
  232. * This must be called only on pages that have been verified to be in the page
  233. * cache and locked. It will never put the page into the free list, the caller
  234. * has a reference on the page.
  235. */
  236. void delete_from_page_cache(struct page *page)
  237. {
  238. struct address_space *mapping = page_mapping(page);
  239. unsigned long flags;
  240. void (*freepage)(struct page *);
  241. BUG_ON(!PageLocked(page));
  242. freepage = mapping->a_ops->freepage;
  243. spin_lock_irqsave(&mapping->tree_lock, flags);
  244. __delete_from_page_cache(page, NULL);
  245. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  246. if (freepage)
  247. freepage(page);
  248. if (PageTransHuge(page) && !PageHuge(page)) {
  249. page_ref_sub(page, HPAGE_PMD_NR);
  250. VM_BUG_ON_PAGE(page_count(page) <= 0, page);
  251. } else {
  252. put_page(page);
  253. }
  254. }
  255. EXPORT_SYMBOL(delete_from_page_cache);
  256. int filemap_check_errors(struct address_space *mapping)
  257. {
  258. int ret = 0;
  259. /* Check for outstanding write errors */
  260. if (test_bit(AS_ENOSPC, &mapping->flags) &&
  261. test_and_clear_bit(AS_ENOSPC, &mapping->flags))
  262. ret = -ENOSPC;
  263. if (test_bit(AS_EIO, &mapping->flags) &&
  264. test_and_clear_bit(AS_EIO, &mapping->flags))
  265. ret = -EIO;
  266. return ret;
  267. }
  268. EXPORT_SYMBOL(filemap_check_errors);
  269. static int filemap_check_and_keep_errors(struct address_space *mapping)
  270. {
  271. /* Check for outstanding write errors */
  272. if (test_bit(AS_EIO, &mapping->flags))
  273. return -EIO;
  274. if (test_bit(AS_ENOSPC, &mapping->flags))
  275. return -ENOSPC;
  276. return 0;
  277. }
  278. /**
  279. * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
  280. * @mapping: address space structure to write
  281. * @start: offset in bytes where the range starts
  282. * @end: offset in bytes where the range ends (inclusive)
  283. * @sync_mode: enable synchronous operation
  284. *
  285. * Start writeback against all of a mapping's dirty pages that lie
  286. * within the byte offsets <start, end> inclusive.
  287. *
  288. * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
  289. * opposed to a regular memory cleansing writeback. The difference between
  290. * these two operations is that if a dirty page/buffer is encountered, it must
  291. * be waited upon, and not just skipped over.
  292. */
  293. int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
  294. loff_t end, int sync_mode)
  295. {
  296. int ret;
  297. struct writeback_control wbc = {
  298. .sync_mode = sync_mode,
  299. .nr_to_write = LONG_MAX,
  300. .range_start = start,
  301. .range_end = end,
  302. };
  303. if (!mapping_cap_writeback_dirty(mapping))
  304. return 0;
  305. wbc_attach_fdatawrite_inode(&wbc, mapping->host);
  306. ret = do_writepages(mapping, &wbc);
  307. wbc_detach_inode(&wbc);
  308. return ret;
  309. }
  310. static inline int __filemap_fdatawrite(struct address_space *mapping,
  311. int sync_mode)
  312. {
  313. return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
  314. }
  315. int filemap_fdatawrite(struct address_space *mapping)
  316. {
  317. return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
  318. }
  319. EXPORT_SYMBOL(filemap_fdatawrite);
  320. int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
  321. loff_t end)
  322. {
  323. return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
  324. }
  325. EXPORT_SYMBOL(filemap_fdatawrite_range);
  326. /**
  327. * filemap_flush - mostly a non-blocking flush
  328. * @mapping: target address_space
  329. *
  330. * This is a mostly non-blocking flush. Not suitable for data-integrity
  331. * purposes - I/O may not be started against all dirty pages.
  332. */
  333. int filemap_flush(struct address_space *mapping)
  334. {
  335. return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
  336. }
  337. EXPORT_SYMBOL(filemap_flush);
  338. /**
  339. * filemap_range_has_page - check if a page exists in range.
  340. * @mapping: address space within which to check
  341. * @start_byte: offset in bytes where the range starts
  342. * @end_byte: offset in bytes where the range ends (inclusive)
  343. *
  344. * Find at least one page in the range supplied, usually used to check if
  345. * direct writing in this range will trigger a writeback.
  346. */
  347. bool filemap_range_has_page(struct address_space *mapping,
  348. loff_t start_byte, loff_t end_byte)
  349. {
  350. pgoff_t index = start_byte >> PAGE_SHIFT;
  351. pgoff_t end = end_byte >> PAGE_SHIFT;
  352. struct page *page;
  353. if (end_byte < start_byte)
  354. return false;
  355. if (mapping->nrpages == 0)
  356. return false;
  357. if (!find_get_pages_range(mapping, &index, end, 1, &page))
  358. return false;
  359. put_page(page);
  360. return true;
  361. }
  362. EXPORT_SYMBOL(filemap_range_has_page);
  363. static void __filemap_fdatawait_range(struct address_space *mapping,
  364. loff_t start_byte, loff_t end_byte)
  365. {
  366. pgoff_t index = start_byte >> PAGE_SHIFT;
  367. pgoff_t end = end_byte >> PAGE_SHIFT;
  368. struct pagevec pvec;
  369. int nr_pages;
  370. if (end_byte < start_byte)
  371. return;
  372. pagevec_init(&pvec, 0);
  373. while ((index <= end) &&
  374. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  375. PAGECACHE_TAG_WRITEBACK,
  376. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
  377. unsigned i;
  378. for (i = 0; i < nr_pages; i++) {
  379. struct page *page = pvec.pages[i];
  380. /* until radix tree lookup accepts end_index */
  381. if (page->index > end)
  382. continue;
  383. wait_on_page_writeback(page);
  384. ClearPageError(page);
  385. }
  386. pagevec_release(&pvec);
  387. cond_resched();
  388. }
  389. }
  390. /**
  391. * filemap_fdatawait_range - wait for writeback to complete
  392. * @mapping: address space structure to wait for
  393. * @start_byte: offset in bytes where the range starts
  394. * @end_byte: offset in bytes where the range ends (inclusive)
  395. *
  396. * Walk the list of under-writeback pages of the given address space
  397. * in the given range and wait for all of them. Check error status of
  398. * the address space and return it.
  399. *
  400. * Since the error status of the address space is cleared by this function,
  401. * callers are responsible for checking the return value and handling and/or
  402. * reporting the error.
  403. */
  404. int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
  405. loff_t end_byte)
  406. {
  407. __filemap_fdatawait_range(mapping, start_byte, end_byte);
  408. return filemap_check_errors(mapping);
  409. }
  410. EXPORT_SYMBOL(filemap_fdatawait_range);
  411. /**
  412. * file_fdatawait_range - wait for writeback to complete
  413. * @file: file pointing to address space structure to wait for
  414. * @start_byte: offset in bytes where the range starts
  415. * @end_byte: offset in bytes where the range ends (inclusive)
  416. *
  417. * Walk the list of under-writeback pages of the address space that file
  418. * refers to, in the given range and wait for all of them. Check error
  419. * status of the address space vs. the file->f_wb_err cursor and return it.
  420. *
  421. * Since the error status of the file is advanced by this function,
  422. * callers are responsible for checking the return value and handling and/or
  423. * reporting the error.
  424. */
  425. int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
  426. {
  427. struct address_space *mapping = file->f_mapping;
  428. __filemap_fdatawait_range(mapping, start_byte, end_byte);
  429. return file_check_and_advance_wb_err(file);
  430. }
  431. EXPORT_SYMBOL(file_fdatawait_range);
  432. /**
  433. * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
  434. * @mapping: address space structure to wait for
  435. *
  436. * Walk the list of under-writeback pages of the given address space
  437. * and wait for all of them. Unlike filemap_fdatawait(), this function
  438. * does not clear error status of the address space.
  439. *
  440. * Use this function if callers don't handle errors themselves. Expected
  441. * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
  442. * fsfreeze(8)
  443. */
  444. int filemap_fdatawait_keep_errors(struct address_space *mapping)
  445. {
  446. __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
  447. return filemap_check_and_keep_errors(mapping);
  448. }
  449. EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
  450. static bool mapping_needs_writeback(struct address_space *mapping)
  451. {
  452. return (!dax_mapping(mapping) && mapping->nrpages) ||
  453. (dax_mapping(mapping) && mapping->nrexceptional);
  454. }
  455. int filemap_write_and_wait(struct address_space *mapping)
  456. {
  457. int err = 0;
  458. if (mapping_needs_writeback(mapping)) {
  459. err = filemap_fdatawrite(mapping);
  460. /*
  461. * Even if the above returned error, the pages may be
  462. * written partially (e.g. -ENOSPC), so we wait for it.
  463. * But the -EIO is special case, it may indicate the worst
  464. * thing (e.g. bug) happened, so we avoid waiting for it.
  465. */
  466. if (err != -EIO) {
  467. int err2 = filemap_fdatawait(mapping);
  468. if (!err)
  469. err = err2;
  470. } else {
  471. /* Clear any previously stored errors */
  472. filemap_check_errors(mapping);
  473. }
  474. } else {
  475. err = filemap_check_errors(mapping);
  476. }
  477. return err;
  478. }
  479. EXPORT_SYMBOL(filemap_write_and_wait);
  480. /**
  481. * filemap_write_and_wait_range - write out & wait on a file range
  482. * @mapping: the address_space for the pages
  483. * @lstart: offset in bytes where the range starts
  484. * @lend: offset in bytes where the range ends (inclusive)
  485. *
  486. * Write out and wait upon file offsets lstart->lend, inclusive.
  487. *
  488. * Note that @lend is inclusive (describes the last byte to be written) so
  489. * that this function can be used to write to the very end-of-file (end = -1).
  490. */
  491. int filemap_write_and_wait_range(struct address_space *mapping,
  492. loff_t lstart, loff_t lend)
  493. {
  494. int err = 0;
  495. if (mapping_needs_writeback(mapping)) {
  496. err = __filemap_fdatawrite_range(mapping, lstart, lend,
  497. WB_SYNC_ALL);
  498. /* See comment of filemap_write_and_wait() */
  499. if (err != -EIO) {
  500. int err2 = filemap_fdatawait_range(mapping,
  501. lstart, lend);
  502. if (!err)
  503. err = err2;
  504. } else {
  505. /* Clear any previously stored errors */
  506. filemap_check_errors(mapping);
  507. }
  508. } else {
  509. err = filemap_check_errors(mapping);
  510. }
  511. return err;
  512. }
  513. EXPORT_SYMBOL(filemap_write_and_wait_range);
  514. void __filemap_set_wb_err(struct address_space *mapping, int err)
  515. {
  516. errseq_t eseq = errseq_set(&mapping->wb_err, err);
  517. trace_filemap_set_wb_err(mapping, eseq);
  518. }
  519. EXPORT_SYMBOL(__filemap_set_wb_err);
  520. /**
  521. * file_check_and_advance_wb_err - report wb error (if any) that was previously
  522. * and advance wb_err to current one
  523. * @file: struct file on which the error is being reported
  524. *
  525. * When userland calls fsync (or something like nfsd does the equivalent), we
  526. * want to report any writeback errors that occurred since the last fsync (or
  527. * since the file was opened if there haven't been any).
  528. *
  529. * Grab the wb_err from the mapping. If it matches what we have in the file,
  530. * then just quickly return 0. The file is all caught up.
  531. *
  532. * If it doesn't match, then take the mapping value, set the "seen" flag in
  533. * it and try to swap it into place. If it works, or another task beat us
  534. * to it with the new value, then update the f_wb_err and return the error
  535. * portion. The error at this point must be reported via proper channels
  536. * (a'la fsync, or NFS COMMIT operation, etc.).
  537. *
  538. * While we handle mapping->wb_err with atomic operations, the f_wb_err
  539. * value is protected by the f_lock since we must ensure that it reflects
  540. * the latest value swapped in for this file descriptor.
  541. */
  542. int file_check_and_advance_wb_err(struct file *file)
  543. {
  544. int err = 0;
  545. errseq_t old = READ_ONCE(file->f_wb_err);
  546. struct address_space *mapping = file->f_mapping;
  547. /* Locklessly handle the common case where nothing has changed */
  548. if (errseq_check(&mapping->wb_err, old)) {
  549. /* Something changed, must use slow path */
  550. spin_lock(&file->f_lock);
  551. old = file->f_wb_err;
  552. err = errseq_check_and_advance(&mapping->wb_err,
  553. &file->f_wb_err);
  554. trace_file_check_and_advance_wb_err(file, old);
  555. spin_unlock(&file->f_lock);
  556. }
  557. return err;
  558. }
  559. EXPORT_SYMBOL(file_check_and_advance_wb_err);
  560. /**
  561. * file_write_and_wait_range - write out & wait on a file range
  562. * @file: file pointing to address_space with pages
  563. * @lstart: offset in bytes where the range starts
  564. * @lend: offset in bytes where the range ends (inclusive)
  565. *
  566. * Write out and wait upon file offsets lstart->lend, inclusive.
  567. *
  568. * Note that @lend is inclusive (describes the last byte to be written) so
  569. * that this function can be used to write to the very end-of-file (end = -1).
  570. *
  571. * After writing out and waiting on the data, we check and advance the
  572. * f_wb_err cursor to the latest value, and return any errors detected there.
  573. */
  574. int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
  575. {
  576. int err = 0, err2;
  577. struct address_space *mapping = file->f_mapping;
  578. if (mapping_needs_writeback(mapping)) {
  579. err = __filemap_fdatawrite_range(mapping, lstart, lend,
  580. WB_SYNC_ALL);
  581. /* See comment of filemap_write_and_wait() */
  582. if (err != -EIO)
  583. __filemap_fdatawait_range(mapping, lstart, lend);
  584. }
  585. err2 = file_check_and_advance_wb_err(file);
  586. if (!err)
  587. err = err2;
  588. return err;
  589. }
  590. EXPORT_SYMBOL(file_write_and_wait_range);
  591. /**
  592. * replace_page_cache_page - replace a pagecache page with a new one
  593. * @old: page to be replaced
  594. * @new: page to replace with
  595. * @gfp_mask: allocation mode
  596. *
  597. * This function replaces a page in the pagecache with a new one. On
  598. * success it acquires the pagecache reference for the new page and
  599. * drops it for the old page. Both the old and new pages must be
  600. * locked. This function does not add the new page to the LRU, the
  601. * caller must do that.
  602. *
  603. * The remove + add is atomic. The only way this function can fail is
  604. * memory allocation failure.
  605. */
  606. int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask)
  607. {
  608. int error;
  609. VM_BUG_ON_PAGE(!PageLocked(old), old);
  610. VM_BUG_ON_PAGE(!PageLocked(new), new);
  611. VM_BUG_ON_PAGE(new->mapping, new);
  612. error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);
  613. if (!error) {
  614. struct address_space *mapping = old->mapping;
  615. void (*freepage)(struct page *);
  616. unsigned long flags;
  617. pgoff_t offset = old->index;
  618. freepage = mapping->a_ops->freepage;
  619. get_page(new);
  620. new->mapping = mapping;
  621. new->index = offset;
  622. spin_lock_irqsave(&mapping->tree_lock, flags);
  623. __delete_from_page_cache(old, NULL);
  624. error = page_cache_tree_insert(mapping, new, NULL);
  625. BUG_ON(error);
  626. /*
  627. * hugetlb pages do not participate in page cache accounting.
  628. */
  629. if (!PageHuge(new))
  630. __inc_node_page_state(new, NR_FILE_PAGES);
  631. if (PageSwapBacked(new))
  632. __inc_node_page_state(new, NR_SHMEM);
  633. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  634. mem_cgroup_migrate(old, new);
  635. radix_tree_preload_end();
  636. if (freepage)
  637. freepage(old);
  638. put_page(old);
  639. }
  640. return error;
  641. }
  642. EXPORT_SYMBOL_GPL(replace_page_cache_page);
  643. static int __add_to_page_cache_locked(struct page *page,
  644. struct address_space *mapping,
  645. pgoff_t offset, gfp_t gfp_mask,
  646. void **shadowp)
  647. {
  648. int huge = PageHuge(page);
  649. struct mem_cgroup *memcg;
  650. int error;
  651. VM_BUG_ON_PAGE(!PageLocked(page), page);
  652. VM_BUG_ON_PAGE(PageSwapBacked(page), page);
  653. if (!huge) {
  654. error = mem_cgroup_try_charge(page, current->mm,
  655. gfp_mask, &memcg, false);
  656. if (error)
  657. return error;
  658. }
  659. error = radix_tree_maybe_preload(gfp_mask & ~__GFP_HIGHMEM);
  660. if (error) {
  661. if (!huge)
  662. mem_cgroup_cancel_charge(page, memcg, false);
  663. return error;
  664. }
  665. get_page(page);
  666. page->mapping = mapping;
  667. page->index = offset;
  668. spin_lock_irq(&mapping->tree_lock);
  669. error = page_cache_tree_insert(mapping, page, shadowp);
  670. radix_tree_preload_end();
  671. if (unlikely(error))
  672. goto err_insert;
  673. /* hugetlb pages do not participate in page cache accounting. */
  674. if (!huge)
  675. __inc_node_page_state(page, NR_FILE_PAGES);
  676. spin_unlock_irq(&mapping->tree_lock);
  677. if (!huge)
  678. mem_cgroup_commit_charge(page, memcg, false, false);
  679. trace_mm_filemap_add_to_page_cache(page);
  680. return 0;
  681. err_insert:
  682. page->mapping = NULL;
  683. /* Leave page->index set: truncation relies upon it */
  684. spin_unlock_irq(&mapping->tree_lock);
  685. if (!huge)
  686. mem_cgroup_cancel_charge(page, memcg, false);
  687. put_page(page);
  688. return error;
  689. }
  690. /**
  691. * add_to_page_cache_locked - add a locked page to the pagecache
  692. * @page: page to add
  693. * @mapping: the page's address_space
  694. * @offset: page index
  695. * @gfp_mask: page allocation mode
  696. *
  697. * This function is used to add a page to the pagecache. It must be locked.
  698. * This function does not add the page to the LRU. The caller must do that.
  699. */
  700. int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
  701. pgoff_t offset, gfp_t gfp_mask)
  702. {
  703. return __add_to_page_cache_locked(page, mapping, offset,
  704. gfp_mask, NULL);
  705. }
  706. EXPORT_SYMBOL(add_to_page_cache_locked);
  707. int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
  708. pgoff_t offset, gfp_t gfp_mask)
  709. {
  710. void *shadow = NULL;
  711. int ret;
  712. __SetPageLocked(page);
  713. ret = __add_to_page_cache_locked(page, mapping, offset,
  714. gfp_mask, &shadow);
  715. if (unlikely(ret))
  716. __ClearPageLocked(page);
  717. else {
  718. /*
  719. * The page might have been evicted from cache only
  720. * recently, in which case it should be activated like
  721. * any other repeatedly accessed page.
  722. * The exception is pages getting rewritten; evicting other
  723. * data from the working set, only to cache data that will
  724. * get overwritten with something else, is a waste of memory.
  725. */
  726. if (!(gfp_mask & __GFP_WRITE) &&
  727. shadow && workingset_refault(shadow)) {
  728. SetPageActive(page);
  729. workingset_activation(page);
  730. } else
  731. ClearPageActive(page);
  732. lru_cache_add(page);
  733. }
  734. return ret;
  735. }
  736. EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
  737. #ifdef CONFIG_NUMA
  738. struct page *__page_cache_alloc(gfp_t gfp)
  739. {
  740. int n;
  741. struct page *page;
  742. if (cpuset_do_page_mem_spread()) {
  743. unsigned int cpuset_mems_cookie;
  744. do {
  745. cpuset_mems_cookie = read_mems_allowed_begin();
  746. n = cpuset_mem_spread_node();
  747. page = __alloc_pages_node(n, gfp, 0);
  748. } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
  749. return page;
  750. }
  751. return alloc_pages(gfp, 0);
  752. }
  753. EXPORT_SYMBOL(__page_cache_alloc);
  754. #endif
  755. /*
  756. * In order to wait for pages to become available there must be
  757. * waitqueues associated with pages. By using a hash table of
  758. * waitqueues where the bucket discipline is to maintain all
  759. * waiters on the same queue and wake all when any of the pages
  760. * become available, and for the woken contexts to check to be
  761. * sure the appropriate page became available, this saves space
  762. * at a cost of "thundering herd" phenomena during rare hash
  763. * collisions.
  764. */
  765. #define PAGE_WAIT_TABLE_BITS 8
  766. #define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
  767. static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
  768. static wait_queue_head_t *page_waitqueue(struct page *page)
  769. {
  770. return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
  771. }
  772. void __init pagecache_init(void)
  773. {
  774. int i;
  775. for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
  776. init_waitqueue_head(&page_wait_table[i]);
  777. page_writeback_init();
  778. }
  779. /* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
  780. struct wait_page_key {
  781. struct page *page;
  782. int bit_nr;
  783. int page_match;
  784. };
  785. struct wait_page_queue {
  786. struct page *page;
  787. int bit_nr;
  788. wait_queue_entry_t wait;
  789. };
  790. static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
  791. {
  792. struct wait_page_key *key = arg;
  793. struct wait_page_queue *wait_page
  794. = container_of(wait, struct wait_page_queue, wait);
  795. if (wait_page->page != key->page)
  796. return 0;
  797. key->page_match = 1;
  798. if (wait_page->bit_nr != key->bit_nr)
  799. return 0;
  800. /* Stop walking if it's locked */
  801. if (test_bit(key->bit_nr, &key->page->flags))
  802. return -1;
  803. return autoremove_wake_function(wait, mode, sync, key);
  804. }
  805. static void wake_up_page_bit(struct page *page, int bit_nr)
  806. {
  807. wait_queue_head_t *q = page_waitqueue(page);
  808. struct wait_page_key key;
  809. unsigned long flags;
  810. wait_queue_entry_t bookmark;
  811. key.page = page;
  812. key.bit_nr = bit_nr;
  813. key.page_match = 0;
  814. bookmark.flags = 0;
  815. bookmark.private = NULL;
  816. bookmark.func = NULL;
  817. INIT_LIST_HEAD(&bookmark.entry);
  818. spin_lock_irqsave(&q->lock, flags);
  819. __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
  820. while (bookmark.flags & WQ_FLAG_BOOKMARK) {
  821. /*
  822. * Take a breather from holding the lock,
  823. * allow pages that finish wake up asynchronously
  824. * to acquire the lock and remove themselves
  825. * from wait queue
  826. */
  827. spin_unlock_irqrestore(&q->lock, flags);
  828. cpu_relax();
  829. spin_lock_irqsave(&q->lock, flags);
  830. __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
  831. }
  832. /*
  833. * It is possible for other pages to have collided on the waitqueue
  834. * hash, so in that case check for a page match. That prevents a long-
  835. * term waiter
  836. *
  837. * It is still possible to miss a case here, when we woke page waiters
  838. * and removed them from the waitqueue, but there are still other
  839. * page waiters.
  840. */
  841. if (!waitqueue_active(q) || !key.page_match) {
  842. ClearPageWaiters(page);
  843. /*
  844. * It's possible to miss clearing Waiters here, when we woke
  845. * our page waiters, but the hashed waitqueue has waiters for
  846. * other pages on it.
  847. *
  848. * That's okay, it's a rare case. The next waker will clear it.
  849. */
  850. }
  851. spin_unlock_irqrestore(&q->lock, flags);
  852. }
  853. static void wake_up_page(struct page *page, int bit)
  854. {
  855. if (!PageWaiters(page))
  856. return;
  857. wake_up_page_bit(page, bit);
  858. }
  859. static inline int wait_on_page_bit_common(wait_queue_head_t *q,
  860. struct page *page, int bit_nr, int state, bool lock)
  861. {
  862. struct wait_page_queue wait_page;
  863. wait_queue_entry_t *wait = &wait_page.wait;
  864. int ret = 0;
  865. init_wait(wait);
  866. wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0;
  867. wait->func = wake_page_function;
  868. wait_page.page = page;
  869. wait_page.bit_nr = bit_nr;
  870. for (;;) {
  871. spin_lock_irq(&q->lock);
  872. if (likely(list_empty(&wait->entry))) {
  873. __add_wait_queue_entry_tail(q, wait);
  874. SetPageWaiters(page);
  875. }
  876. set_current_state(state);
  877. spin_unlock_irq(&q->lock);
  878. if (likely(test_bit(bit_nr, &page->flags))) {
  879. io_schedule();
  880. }
  881. if (lock) {
  882. if (!test_and_set_bit_lock(bit_nr, &page->flags))
  883. break;
  884. } else {
  885. if (!test_bit(bit_nr, &page->flags))
  886. break;
  887. }
  888. if (unlikely(signal_pending_state(state, current))) {
  889. ret = -EINTR;
  890. break;
  891. }
  892. }
  893. finish_wait(q, wait);
  894. /*
  895. * A signal could leave PageWaiters set. Clearing it here if
  896. * !waitqueue_active would be possible (by open-coding finish_wait),
  897. * but still fail to catch it in the case of wait hash collision. We
  898. * already can fail to clear wait hash collision cases, so don't
  899. * bother with signals either.
  900. */
  901. return ret;
  902. }
  903. void wait_on_page_bit(struct page *page, int bit_nr)
  904. {
  905. wait_queue_head_t *q = page_waitqueue(page);
  906. wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, false);
  907. }
  908. EXPORT_SYMBOL(wait_on_page_bit);
  909. int wait_on_page_bit_killable(struct page *page, int bit_nr)
  910. {
  911. wait_queue_head_t *q = page_waitqueue(page);
  912. return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, false);
  913. }
  914. /**
  915. * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
  916. * @page: Page defining the wait queue of interest
  917. * @waiter: Waiter to add to the queue
  918. *
  919. * Add an arbitrary @waiter to the wait queue for the nominated @page.
  920. */
  921. void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
  922. {
  923. wait_queue_head_t *q = page_waitqueue(page);
  924. unsigned long flags;
  925. spin_lock_irqsave(&q->lock, flags);
  926. __add_wait_queue_entry_tail(q, waiter);
  927. SetPageWaiters(page);
  928. spin_unlock_irqrestore(&q->lock, flags);
  929. }
  930. EXPORT_SYMBOL_GPL(add_page_wait_queue);
  931. #ifndef clear_bit_unlock_is_negative_byte
  932. /*
  933. * PG_waiters is the high bit in the same byte as PG_lock.
  934. *
  935. * On x86 (and on many other architectures), we can clear PG_lock and
  936. * test the sign bit at the same time. But if the architecture does
  937. * not support that special operation, we just do this all by hand
  938. * instead.
  939. *
  940. * The read of PG_waiters has to be after (or concurrently with) PG_locked
  941. * being cleared, but a memory barrier should be unneccssary since it is
  942. * in the same byte as PG_locked.
  943. */
  944. static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
  945. {
  946. clear_bit_unlock(nr, mem);
  947. /* smp_mb__after_atomic(); */
  948. return test_bit(PG_waiters, mem);
  949. }
  950. #endif
  951. /**
  952. * unlock_page - unlock a locked page
  953. * @page: the page
  954. *
  955. * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
  956. * Also wakes sleepers in wait_on_page_writeback() because the wakeup
  957. * mechanism between PageLocked pages and PageWriteback pages is shared.
  958. * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
  959. *
  960. * Note that this depends on PG_waiters being the sign bit in the byte
  961. * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
  962. * clear the PG_locked bit and test PG_waiters at the same time fairly
  963. * portably (architectures that do LL/SC can test any bit, while x86 can
  964. * test the sign bit).
  965. */
  966. void unlock_page(struct page *page)
  967. {
  968. BUILD_BUG_ON(PG_waiters != 7);
  969. page = compound_head(page);
  970. VM_BUG_ON_PAGE(!PageLocked(page), page);
  971. if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
  972. wake_up_page_bit(page, PG_locked);
  973. }
  974. EXPORT_SYMBOL(unlock_page);
  975. /**
  976. * end_page_writeback - end writeback against a page
  977. * @page: the page
  978. */
  979. void end_page_writeback(struct page *page)
  980. {
  981. /*
  982. * TestClearPageReclaim could be used here but it is an atomic
  983. * operation and overkill in this particular case. Failing to
  984. * shuffle a page marked for immediate reclaim is too mild to
  985. * justify taking an atomic operation penalty at the end of
  986. * ever page writeback.
  987. */
  988. if (PageReclaim(page)) {
  989. ClearPageReclaim(page);
  990. rotate_reclaimable_page(page);
  991. }
  992. if (!test_clear_page_writeback(page))
  993. BUG();
  994. smp_mb__after_atomic();
  995. wake_up_page(page, PG_writeback);
  996. }
  997. EXPORT_SYMBOL(end_page_writeback);
  998. /*
  999. * After completing I/O on a page, call this routine to update the page
  1000. * flags appropriately
  1001. */
  1002. void page_endio(struct page *page, bool is_write, int err)
  1003. {
  1004. if (!is_write) {
  1005. if (!err) {
  1006. SetPageUptodate(page);
  1007. } else {
  1008. ClearPageUptodate(page);
  1009. SetPageError(page);
  1010. }
  1011. unlock_page(page);
  1012. } else {
  1013. if (err) {
  1014. struct address_space *mapping;
  1015. SetPageError(page);
  1016. mapping = page_mapping(page);
  1017. if (mapping)
  1018. mapping_set_error(mapping, err);
  1019. }
  1020. end_page_writeback(page);
  1021. }
  1022. }
  1023. EXPORT_SYMBOL_GPL(page_endio);
  1024. /**
  1025. * __lock_page - get a lock on the page, assuming we need to sleep to get it
  1026. * @__page: the page to lock
  1027. */
  1028. void __lock_page(struct page *__page)
  1029. {
  1030. struct page *page = compound_head(__page);
  1031. wait_queue_head_t *q = page_waitqueue(page);
  1032. wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, true);
  1033. }
  1034. EXPORT_SYMBOL(__lock_page);
  1035. int __lock_page_killable(struct page *__page)
  1036. {
  1037. struct page *page = compound_head(__page);
  1038. wait_queue_head_t *q = page_waitqueue(page);
  1039. return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, true);
  1040. }
  1041. EXPORT_SYMBOL_GPL(__lock_page_killable);
  1042. /*
  1043. * Return values:
  1044. * 1 - page is locked; mmap_sem is still held.
  1045. * 0 - page is not locked.
  1046. * mmap_sem has been released (up_read()), unless flags had both
  1047. * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
  1048. * which case mmap_sem is still held.
  1049. *
  1050. * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
  1051. * with the page locked and the mmap_sem unperturbed.
  1052. */
  1053. int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
  1054. unsigned int flags)
  1055. {
  1056. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  1057. /*
  1058. * CAUTION! In this case, mmap_sem is not released
  1059. * even though return 0.
  1060. */
  1061. if (flags & FAULT_FLAG_RETRY_NOWAIT)
  1062. return 0;
  1063. up_read(&mm->mmap_sem);
  1064. if (flags & FAULT_FLAG_KILLABLE)
  1065. wait_on_page_locked_killable(page);
  1066. else
  1067. wait_on_page_locked(page);
  1068. return 0;
  1069. } else {
  1070. if (flags & FAULT_FLAG_KILLABLE) {
  1071. int ret;
  1072. ret = __lock_page_killable(page);
  1073. if (ret) {
  1074. up_read(&mm->mmap_sem);
  1075. return 0;
  1076. }
  1077. } else
  1078. __lock_page(page);
  1079. return 1;
  1080. }
  1081. }
  1082. /**
  1083. * page_cache_next_hole - find the next hole (not-present entry)
  1084. * @mapping: mapping
  1085. * @index: index
  1086. * @max_scan: maximum range to search
  1087. *
  1088. * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
  1089. * lowest indexed hole.
  1090. *
  1091. * Returns: the index of the hole if found, otherwise returns an index
  1092. * outside of the set specified (in which case 'return - index >=
  1093. * max_scan' will be true). In rare cases of index wrap-around, 0 will
  1094. * be returned.
  1095. *
  1096. * page_cache_next_hole may be called under rcu_read_lock. However,
  1097. * like radix_tree_gang_lookup, this will not atomically search a
  1098. * snapshot of the tree at a single point in time. For example, if a
  1099. * hole is created at index 5, then subsequently a hole is created at
  1100. * index 10, page_cache_next_hole covering both indexes may return 10
  1101. * if called under rcu_read_lock.
  1102. */
  1103. pgoff_t page_cache_next_hole(struct address_space *mapping,
  1104. pgoff_t index, unsigned long max_scan)
  1105. {
  1106. unsigned long i;
  1107. for (i = 0; i < max_scan; i++) {
  1108. struct page *page;
  1109. page = radix_tree_lookup(&mapping->page_tree, index);
  1110. if (!page || radix_tree_exceptional_entry(page))
  1111. break;
  1112. index++;
  1113. if (index == 0)
  1114. break;
  1115. }
  1116. return index;
  1117. }
  1118. EXPORT_SYMBOL(page_cache_next_hole);
  1119. /**
  1120. * page_cache_prev_hole - find the prev hole (not-present entry)
  1121. * @mapping: mapping
  1122. * @index: index
  1123. * @max_scan: maximum range to search
  1124. *
  1125. * Search backwards in the range [max(index-max_scan+1, 0), index] for
  1126. * the first hole.
  1127. *
  1128. * Returns: the index of the hole if found, otherwise returns an index
  1129. * outside of the set specified (in which case 'index - return >=
  1130. * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
  1131. * will be returned.
  1132. *
  1133. * page_cache_prev_hole may be called under rcu_read_lock. However,
  1134. * like radix_tree_gang_lookup, this will not atomically search a
  1135. * snapshot of the tree at a single point in time. For example, if a
  1136. * hole is created at index 10, then subsequently a hole is created at
  1137. * index 5, page_cache_prev_hole covering both indexes may return 5 if
  1138. * called under rcu_read_lock.
  1139. */
  1140. pgoff_t page_cache_prev_hole(struct address_space *mapping,
  1141. pgoff_t index, unsigned long max_scan)
  1142. {
  1143. unsigned long i;
  1144. for (i = 0; i < max_scan; i++) {
  1145. struct page *page;
  1146. page = radix_tree_lookup(&mapping->page_tree, index);
  1147. if (!page || radix_tree_exceptional_entry(page))
  1148. break;
  1149. index--;
  1150. if (index == ULONG_MAX)
  1151. break;
  1152. }
  1153. return index;
  1154. }
  1155. EXPORT_SYMBOL(page_cache_prev_hole);
  1156. /**
  1157. * find_get_entry - find and get a page cache entry
  1158. * @mapping: the address_space to search
  1159. * @offset: the page cache index
  1160. *
  1161. * Looks up the page cache slot at @mapping & @offset. If there is a
  1162. * page cache page, it is returned with an increased refcount.
  1163. *
  1164. * If the slot holds a shadow entry of a previously evicted page, or a
  1165. * swap entry from shmem/tmpfs, it is returned.
  1166. *
  1167. * Otherwise, %NULL is returned.
  1168. */
  1169. struct page *find_get_entry(struct address_space *mapping, pgoff_t offset)
  1170. {
  1171. void **pagep;
  1172. struct page *head, *page;
  1173. rcu_read_lock();
  1174. repeat:
  1175. page = NULL;
  1176. pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
  1177. if (pagep) {
  1178. page = radix_tree_deref_slot(pagep);
  1179. if (unlikely(!page))
  1180. goto out;
  1181. if (radix_tree_exception(page)) {
  1182. if (radix_tree_deref_retry(page))
  1183. goto repeat;
  1184. /*
  1185. * A shadow entry of a recently evicted page,
  1186. * or a swap entry from shmem/tmpfs. Return
  1187. * it without attempting to raise page count.
  1188. */
  1189. goto out;
  1190. }
  1191. head = compound_head(page);
  1192. if (!page_cache_get_speculative(head))
  1193. goto repeat;
  1194. /* The page was split under us? */
  1195. if (compound_head(page) != head) {
  1196. put_page(head);
  1197. goto repeat;
  1198. }
  1199. /*
  1200. * Has the page moved?
  1201. * This is part of the lockless pagecache protocol. See
  1202. * include/linux/pagemap.h for details.
  1203. */
  1204. if (unlikely(page != *pagep)) {
  1205. put_page(head);
  1206. goto repeat;
  1207. }
  1208. }
  1209. out:
  1210. rcu_read_unlock();
  1211. return page;
  1212. }
  1213. EXPORT_SYMBOL(find_get_entry);
  1214. /**
  1215. * find_lock_entry - locate, pin and lock a page cache entry
  1216. * @mapping: the address_space to search
  1217. * @offset: the page cache index
  1218. *
  1219. * Looks up the page cache slot at @mapping & @offset. If there is a
  1220. * page cache page, it is returned locked and with an increased
  1221. * refcount.
  1222. *
  1223. * If the slot holds a shadow entry of a previously evicted page, or a
  1224. * swap entry from shmem/tmpfs, it is returned.
  1225. *
  1226. * Otherwise, %NULL is returned.
  1227. *
  1228. * find_lock_entry() may sleep.
  1229. */
  1230. struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset)
  1231. {
  1232. struct page *page;
  1233. repeat:
  1234. page = find_get_entry(mapping, offset);
  1235. if (page && !radix_tree_exception(page)) {
  1236. lock_page(page);
  1237. /* Has the page been truncated? */
  1238. if (unlikely(page_mapping(page) != mapping)) {
  1239. unlock_page(page);
  1240. put_page(page);
  1241. goto repeat;
  1242. }
  1243. VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
  1244. }
  1245. return page;
  1246. }
  1247. EXPORT_SYMBOL(find_lock_entry);
  1248. /**
  1249. * pagecache_get_page - find and get a page reference
  1250. * @mapping: the address_space to search
  1251. * @offset: the page index
  1252. * @fgp_flags: PCG flags
  1253. * @gfp_mask: gfp mask to use for the page cache data page allocation
  1254. *
  1255. * Looks up the page cache slot at @mapping & @offset.
  1256. *
  1257. * PCG flags modify how the page is returned.
  1258. *
  1259. * @fgp_flags can be:
  1260. *
  1261. * - FGP_ACCESSED: the page will be marked accessed
  1262. * - FGP_LOCK: Page is return locked
  1263. * - FGP_CREAT: If page is not present then a new page is allocated using
  1264. * @gfp_mask and added to the page cache and the VM's LRU
  1265. * list. The page is returned locked and with an increased
  1266. * refcount. Otherwise, NULL is returned.
  1267. *
  1268. * If FGP_LOCK or FGP_CREAT are specified then the function may sleep even
  1269. * if the GFP flags specified for FGP_CREAT are atomic.
  1270. *
  1271. * If there is a page cache page, it is returned with an increased refcount.
  1272. */
  1273. struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
  1274. int fgp_flags, gfp_t gfp_mask)
  1275. {
  1276. struct page *page;
  1277. repeat:
  1278. page = find_get_entry(mapping, offset);
  1279. if (radix_tree_exceptional_entry(page))
  1280. page = NULL;
  1281. if (!page)
  1282. goto no_page;
  1283. if (fgp_flags & FGP_LOCK) {
  1284. if (fgp_flags & FGP_NOWAIT) {
  1285. if (!trylock_page(page)) {
  1286. put_page(page);
  1287. return NULL;
  1288. }
  1289. } else {
  1290. lock_page(page);
  1291. }
  1292. /* Has the page been truncated? */
  1293. if (unlikely(page->mapping != mapping)) {
  1294. unlock_page(page);
  1295. put_page(page);
  1296. goto repeat;
  1297. }
  1298. VM_BUG_ON_PAGE(page->index != offset, page);
  1299. }
  1300. if (page && (fgp_flags & FGP_ACCESSED))
  1301. mark_page_accessed(page);
  1302. no_page:
  1303. if (!page && (fgp_flags & FGP_CREAT)) {
  1304. int err;
  1305. if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping))
  1306. gfp_mask |= __GFP_WRITE;
  1307. if (fgp_flags & FGP_NOFS)
  1308. gfp_mask &= ~__GFP_FS;
  1309. page = __page_cache_alloc(gfp_mask);
  1310. if (!page)
  1311. return NULL;
  1312. if (WARN_ON_ONCE(!(fgp_flags & FGP_LOCK)))
  1313. fgp_flags |= FGP_LOCK;
  1314. /* Init accessed so avoid atomic mark_page_accessed later */
  1315. if (fgp_flags & FGP_ACCESSED)
  1316. __SetPageReferenced(page);
  1317. err = add_to_page_cache_lru(page, mapping, offset,
  1318. gfp_mask & GFP_RECLAIM_MASK);
  1319. if (unlikely(err)) {
  1320. put_page(page);
  1321. page = NULL;
  1322. if (err == -EEXIST)
  1323. goto repeat;
  1324. }
  1325. }
  1326. return page;
  1327. }
  1328. EXPORT_SYMBOL(pagecache_get_page);
  1329. /**
  1330. * find_get_entries - gang pagecache lookup
  1331. * @mapping: The address_space to search
  1332. * @start: The starting page cache index
  1333. * @nr_entries: The maximum number of entries
  1334. * @entries: Where the resulting entries are placed
  1335. * @indices: The cache indices corresponding to the entries in @entries
  1336. *
  1337. * find_get_entries() will search for and return a group of up to
  1338. * @nr_entries entries in the mapping. The entries are placed at
  1339. * @entries. find_get_entries() takes a reference against any actual
  1340. * pages it returns.
  1341. *
  1342. * The search returns a group of mapping-contiguous page cache entries
  1343. * with ascending indexes. There may be holes in the indices due to
  1344. * not-present pages.
  1345. *
  1346. * Any shadow entries of evicted pages, or swap entries from
  1347. * shmem/tmpfs, are included in the returned array.
  1348. *
  1349. * find_get_entries() returns the number of pages and shadow entries
  1350. * which were found.
  1351. */
  1352. unsigned find_get_entries(struct address_space *mapping,
  1353. pgoff_t start, unsigned int nr_entries,
  1354. struct page **entries, pgoff_t *indices)
  1355. {
  1356. void **slot;
  1357. unsigned int ret = 0;
  1358. struct radix_tree_iter iter;
  1359. if (!nr_entries)
  1360. return 0;
  1361. rcu_read_lock();
  1362. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1363. struct page *head, *page;
  1364. repeat:
  1365. page = radix_tree_deref_slot(slot);
  1366. if (unlikely(!page))
  1367. continue;
  1368. if (radix_tree_exception(page)) {
  1369. if (radix_tree_deref_retry(page)) {
  1370. slot = radix_tree_iter_retry(&iter);
  1371. continue;
  1372. }
  1373. /*
  1374. * A shadow entry of a recently evicted page, a swap
  1375. * entry from shmem/tmpfs or a DAX entry. Return it
  1376. * without attempting to raise page count.
  1377. */
  1378. goto export;
  1379. }
  1380. head = compound_head(page);
  1381. if (!page_cache_get_speculative(head))
  1382. goto repeat;
  1383. /* The page was split under us? */
  1384. if (compound_head(page) != head) {
  1385. put_page(head);
  1386. goto repeat;
  1387. }
  1388. /* Has the page moved? */
  1389. if (unlikely(page != *slot)) {
  1390. put_page(head);
  1391. goto repeat;
  1392. }
  1393. export:
  1394. indices[ret] = iter.index;
  1395. entries[ret] = page;
  1396. if (++ret == nr_entries)
  1397. break;
  1398. }
  1399. rcu_read_unlock();
  1400. return ret;
  1401. }
  1402. /**
  1403. * find_get_pages_range - gang pagecache lookup
  1404. * @mapping: The address_space to search
  1405. * @start: The starting page index
  1406. * @end: The final page index (inclusive)
  1407. * @nr_pages: The maximum number of pages
  1408. * @pages: Where the resulting pages are placed
  1409. *
  1410. * find_get_pages_range() will search for and return a group of up to @nr_pages
  1411. * pages in the mapping starting at index @start and up to index @end
  1412. * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
  1413. * a reference against the returned pages.
  1414. *
  1415. * The search returns a group of mapping-contiguous pages with ascending
  1416. * indexes. There may be holes in the indices due to not-present pages.
  1417. * We also update @start to index the next page for the traversal.
  1418. *
  1419. * find_get_pages_range() returns the number of pages which were found. If this
  1420. * number is smaller than @nr_pages, the end of specified range has been
  1421. * reached.
  1422. */
  1423. unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
  1424. pgoff_t end, unsigned int nr_pages,
  1425. struct page **pages)
  1426. {
  1427. struct radix_tree_iter iter;
  1428. void **slot;
  1429. unsigned ret = 0;
  1430. if (unlikely(!nr_pages))
  1431. return 0;
  1432. rcu_read_lock();
  1433. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, *start) {
  1434. struct page *head, *page;
  1435. if (iter.index > end)
  1436. break;
  1437. repeat:
  1438. page = radix_tree_deref_slot(slot);
  1439. if (unlikely(!page))
  1440. continue;
  1441. if (radix_tree_exception(page)) {
  1442. if (radix_tree_deref_retry(page)) {
  1443. slot = radix_tree_iter_retry(&iter);
  1444. continue;
  1445. }
  1446. /*
  1447. * A shadow entry of a recently evicted page,
  1448. * or a swap entry from shmem/tmpfs. Skip
  1449. * over it.
  1450. */
  1451. continue;
  1452. }
  1453. head = compound_head(page);
  1454. if (!page_cache_get_speculative(head))
  1455. goto repeat;
  1456. /* The page was split under us? */
  1457. if (compound_head(page) != head) {
  1458. put_page(head);
  1459. goto repeat;
  1460. }
  1461. /* Has the page moved? */
  1462. if (unlikely(page != *slot)) {
  1463. put_page(head);
  1464. goto repeat;
  1465. }
  1466. pages[ret] = page;
  1467. if (++ret == nr_pages) {
  1468. *start = pages[ret - 1]->index + 1;
  1469. goto out;
  1470. }
  1471. }
  1472. /*
  1473. * We come here when there is no page beyond @end. We take care to not
  1474. * overflow the index @start as it confuses some of the callers. This
  1475. * breaks the iteration when there is page at index -1 but that is
  1476. * already broken anyway.
  1477. */
  1478. if (end == (pgoff_t)-1)
  1479. *start = (pgoff_t)-1;
  1480. else
  1481. *start = end + 1;
  1482. out:
  1483. rcu_read_unlock();
  1484. return ret;
  1485. }
  1486. /**
  1487. * find_get_pages_contig - gang contiguous pagecache lookup
  1488. * @mapping: The address_space to search
  1489. * @index: The starting page index
  1490. * @nr_pages: The maximum number of pages
  1491. * @pages: Where the resulting pages are placed
  1492. *
  1493. * find_get_pages_contig() works exactly like find_get_pages(), except
  1494. * that the returned number of pages are guaranteed to be contiguous.
  1495. *
  1496. * find_get_pages_contig() returns the number of pages which were found.
  1497. */
  1498. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
  1499. unsigned int nr_pages, struct page **pages)
  1500. {
  1501. struct radix_tree_iter iter;
  1502. void **slot;
  1503. unsigned int ret = 0;
  1504. if (unlikely(!nr_pages))
  1505. return 0;
  1506. rcu_read_lock();
  1507. radix_tree_for_each_contig(slot, &mapping->page_tree, &iter, index) {
  1508. struct page *head, *page;
  1509. repeat:
  1510. page = radix_tree_deref_slot(slot);
  1511. /* The hole, there no reason to continue */
  1512. if (unlikely(!page))
  1513. break;
  1514. if (radix_tree_exception(page)) {
  1515. if (radix_tree_deref_retry(page)) {
  1516. slot = radix_tree_iter_retry(&iter);
  1517. continue;
  1518. }
  1519. /*
  1520. * A shadow entry of a recently evicted page,
  1521. * or a swap entry from shmem/tmpfs. Stop
  1522. * looking for contiguous pages.
  1523. */
  1524. break;
  1525. }
  1526. head = compound_head(page);
  1527. if (!page_cache_get_speculative(head))
  1528. goto repeat;
  1529. /* The page was split under us? */
  1530. if (compound_head(page) != head) {
  1531. put_page(head);
  1532. goto repeat;
  1533. }
  1534. /* Has the page moved? */
  1535. if (unlikely(page != *slot)) {
  1536. put_page(head);
  1537. goto repeat;
  1538. }
  1539. /*
  1540. * must check mapping and index after taking the ref.
  1541. * otherwise we can get both false positives and false
  1542. * negatives, which is just confusing to the caller.
  1543. */
  1544. if (page->mapping == NULL || page_to_pgoff(page) != iter.index) {
  1545. put_page(page);
  1546. break;
  1547. }
  1548. pages[ret] = page;
  1549. if (++ret == nr_pages)
  1550. break;
  1551. }
  1552. rcu_read_unlock();
  1553. return ret;
  1554. }
  1555. EXPORT_SYMBOL(find_get_pages_contig);
  1556. /**
  1557. * find_get_pages_tag - find and return pages that match @tag
  1558. * @mapping: the address_space to search
  1559. * @index: the starting page index
  1560. * @tag: the tag index
  1561. * @nr_pages: the maximum number of pages
  1562. * @pages: where the resulting pages are placed
  1563. *
  1564. * Like find_get_pages, except we only return pages which are tagged with
  1565. * @tag. We update @index to index the next page for the traversal.
  1566. */
  1567. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  1568. int tag, unsigned int nr_pages, struct page **pages)
  1569. {
  1570. struct radix_tree_iter iter;
  1571. void **slot;
  1572. unsigned ret = 0;
  1573. if (unlikely(!nr_pages))
  1574. return 0;
  1575. rcu_read_lock();
  1576. radix_tree_for_each_tagged(slot, &mapping->page_tree,
  1577. &iter, *index, tag) {
  1578. struct page *head, *page;
  1579. repeat:
  1580. page = radix_tree_deref_slot(slot);
  1581. if (unlikely(!page))
  1582. continue;
  1583. if (radix_tree_exception(page)) {
  1584. if (radix_tree_deref_retry(page)) {
  1585. slot = radix_tree_iter_retry(&iter);
  1586. continue;
  1587. }
  1588. /*
  1589. * A shadow entry of a recently evicted page.
  1590. *
  1591. * Those entries should never be tagged, but
  1592. * this tree walk is lockless and the tags are
  1593. * looked up in bulk, one radix tree node at a
  1594. * time, so there is a sizable window for page
  1595. * reclaim to evict a page we saw tagged.
  1596. *
  1597. * Skip over it.
  1598. */
  1599. continue;
  1600. }
  1601. head = compound_head(page);
  1602. if (!page_cache_get_speculative(head))
  1603. goto repeat;
  1604. /* The page was split under us? */
  1605. if (compound_head(page) != head) {
  1606. put_page(head);
  1607. goto repeat;
  1608. }
  1609. /* Has the page moved? */
  1610. if (unlikely(page != *slot)) {
  1611. put_page(head);
  1612. goto repeat;
  1613. }
  1614. pages[ret] = page;
  1615. if (++ret == nr_pages)
  1616. break;
  1617. }
  1618. rcu_read_unlock();
  1619. if (ret)
  1620. *index = pages[ret - 1]->index + 1;
  1621. return ret;
  1622. }
  1623. EXPORT_SYMBOL(find_get_pages_tag);
  1624. /**
  1625. * find_get_entries_tag - find and return entries that match @tag
  1626. * @mapping: the address_space to search
  1627. * @start: the starting page cache index
  1628. * @tag: the tag index
  1629. * @nr_entries: the maximum number of entries
  1630. * @entries: where the resulting entries are placed
  1631. * @indices: the cache indices corresponding to the entries in @entries
  1632. *
  1633. * Like find_get_entries, except we only return entries which are tagged with
  1634. * @tag.
  1635. */
  1636. unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
  1637. int tag, unsigned int nr_entries,
  1638. struct page **entries, pgoff_t *indices)
  1639. {
  1640. void **slot;
  1641. unsigned int ret = 0;
  1642. struct radix_tree_iter iter;
  1643. if (!nr_entries)
  1644. return 0;
  1645. rcu_read_lock();
  1646. radix_tree_for_each_tagged(slot, &mapping->page_tree,
  1647. &iter, start, tag) {
  1648. struct page *head, *page;
  1649. repeat:
  1650. page = radix_tree_deref_slot(slot);
  1651. if (unlikely(!page))
  1652. continue;
  1653. if (radix_tree_exception(page)) {
  1654. if (radix_tree_deref_retry(page)) {
  1655. slot = radix_tree_iter_retry(&iter);
  1656. continue;
  1657. }
  1658. /*
  1659. * A shadow entry of a recently evicted page, a swap
  1660. * entry from shmem/tmpfs or a DAX entry. Return it
  1661. * without attempting to raise page count.
  1662. */
  1663. goto export;
  1664. }
  1665. head = compound_head(page);
  1666. if (!page_cache_get_speculative(head))
  1667. goto repeat;
  1668. /* The page was split under us? */
  1669. if (compound_head(page) != head) {
  1670. put_page(head);
  1671. goto repeat;
  1672. }
  1673. /* Has the page moved? */
  1674. if (unlikely(page != *slot)) {
  1675. put_page(head);
  1676. goto repeat;
  1677. }
  1678. export:
  1679. indices[ret] = iter.index;
  1680. entries[ret] = page;
  1681. if (++ret == nr_entries)
  1682. break;
  1683. }
  1684. rcu_read_unlock();
  1685. return ret;
  1686. }
  1687. EXPORT_SYMBOL(find_get_entries_tag);
  1688. /*
  1689. * CD/DVDs are error prone. When a medium error occurs, the driver may fail
  1690. * a _large_ part of the i/o request. Imagine the worst scenario:
  1691. *
  1692. * ---R__________________________________________B__________
  1693. * ^ reading here ^ bad block(assume 4k)
  1694. *
  1695. * read(R) => miss => readahead(R...B) => media error => frustrating retries
  1696. * => failing the whole request => read(R) => read(R+1) =>
  1697. * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
  1698. * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
  1699. * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
  1700. *
  1701. * It is going insane. Fix it by quickly scaling down the readahead size.
  1702. */
  1703. static void shrink_readahead_size_eio(struct file *filp,
  1704. struct file_ra_state *ra)
  1705. {
  1706. ra->ra_pages /= 4;
  1707. }
  1708. /**
  1709. * generic_file_buffered_read - generic file read routine
  1710. * @iocb: the iocb to read
  1711. * @iter: data destination
  1712. * @written: already copied
  1713. *
  1714. * This is a generic file read routine, and uses the
  1715. * mapping->a_ops->readpage() function for the actual low-level stuff.
  1716. *
  1717. * This is really ugly. But the goto's actually try to clarify some
  1718. * of the logic when it comes to error handling etc.
  1719. */
  1720. static ssize_t generic_file_buffered_read(struct kiocb *iocb,
  1721. struct iov_iter *iter, ssize_t written)
  1722. {
  1723. struct file *filp = iocb->ki_filp;
  1724. struct address_space *mapping = filp->f_mapping;
  1725. struct inode *inode = mapping->host;
  1726. struct file_ra_state *ra = &filp->f_ra;
  1727. loff_t *ppos = &iocb->ki_pos;
  1728. pgoff_t index;
  1729. pgoff_t last_index;
  1730. pgoff_t prev_index;
  1731. unsigned long offset; /* offset into pagecache page */
  1732. unsigned int prev_offset;
  1733. int error = 0;
  1734. if (unlikely(*ppos >= inode->i_sb->s_maxbytes))
  1735. return 0;
  1736. iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
  1737. index = *ppos >> PAGE_SHIFT;
  1738. prev_index = ra->prev_pos >> PAGE_SHIFT;
  1739. prev_offset = ra->prev_pos & (PAGE_SIZE-1);
  1740. last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT;
  1741. offset = *ppos & ~PAGE_MASK;
  1742. for (;;) {
  1743. struct page *page;
  1744. pgoff_t end_index;
  1745. loff_t isize;
  1746. unsigned long nr, ret;
  1747. cond_resched();
  1748. find_page:
  1749. if (fatal_signal_pending(current)) {
  1750. error = -EINTR;
  1751. goto out;
  1752. }
  1753. page = find_get_page(mapping, index);
  1754. if (!page) {
  1755. if (iocb->ki_flags & IOCB_NOWAIT)
  1756. goto would_block;
  1757. page_cache_sync_readahead(mapping,
  1758. ra, filp,
  1759. index, last_index - index);
  1760. page = find_get_page(mapping, index);
  1761. if (unlikely(page == NULL))
  1762. goto no_cached_page;
  1763. }
  1764. if (PageReadahead(page)) {
  1765. page_cache_async_readahead(mapping,
  1766. ra, filp, page,
  1767. index, last_index - index);
  1768. }
  1769. if (!PageUptodate(page)) {
  1770. if (iocb->ki_flags & IOCB_NOWAIT) {
  1771. put_page(page);
  1772. goto would_block;
  1773. }
  1774. /*
  1775. * See comment in do_read_cache_page on why
  1776. * wait_on_page_locked is used to avoid unnecessarily
  1777. * serialisations and why it's safe.
  1778. */
  1779. error = wait_on_page_locked_killable(page);
  1780. if (unlikely(error))
  1781. goto readpage_error;
  1782. if (PageUptodate(page))
  1783. goto page_ok;
  1784. if (inode->i_blkbits == PAGE_SHIFT ||
  1785. !mapping->a_ops->is_partially_uptodate)
  1786. goto page_not_up_to_date;
  1787. /* pipes can't handle partially uptodate pages */
  1788. if (unlikely(iter->type & ITER_PIPE))
  1789. goto page_not_up_to_date;
  1790. if (!trylock_page(page))
  1791. goto page_not_up_to_date;
  1792. /* Did it get truncated before we got the lock? */
  1793. if (!page->mapping)
  1794. goto page_not_up_to_date_locked;
  1795. if (!mapping->a_ops->is_partially_uptodate(page,
  1796. offset, iter->count))
  1797. goto page_not_up_to_date_locked;
  1798. unlock_page(page);
  1799. }
  1800. page_ok:
  1801. /*
  1802. * i_size must be checked after we know the page is Uptodate.
  1803. *
  1804. * Checking i_size after the check allows us to calculate
  1805. * the correct value for "nr", which means the zero-filled
  1806. * part of the page is not copied back to userspace (unless
  1807. * another truncate extends the file - this is desired though).
  1808. */
  1809. isize = i_size_read(inode);
  1810. end_index = (isize - 1) >> PAGE_SHIFT;
  1811. if (unlikely(!isize || index > end_index)) {
  1812. put_page(page);
  1813. goto out;
  1814. }
  1815. /* nr is the maximum number of bytes to copy from this page */
  1816. nr = PAGE_SIZE;
  1817. if (index == end_index) {
  1818. nr = ((isize - 1) & ~PAGE_MASK) + 1;
  1819. if (nr <= offset) {
  1820. put_page(page);
  1821. goto out;
  1822. }
  1823. }
  1824. nr = nr - offset;
  1825. /* If users can be writing to this page using arbitrary
  1826. * virtual addresses, take care about potential aliasing
  1827. * before reading the page on the kernel side.
  1828. */
  1829. if (mapping_writably_mapped(mapping))
  1830. flush_dcache_page(page);
  1831. /*
  1832. * When a sequential read accesses a page several times,
  1833. * only mark it as accessed the first time.
  1834. */
  1835. if (prev_index != index || offset != prev_offset)
  1836. mark_page_accessed(page);
  1837. prev_index = index;
  1838. /*
  1839. * Ok, we have the page, and it's up-to-date, so
  1840. * now we can copy it to user space...
  1841. */
  1842. ret = copy_page_to_iter(page, offset, nr, iter);
  1843. offset += ret;
  1844. index += offset >> PAGE_SHIFT;
  1845. offset &= ~PAGE_MASK;
  1846. prev_offset = offset;
  1847. put_page(page);
  1848. written += ret;
  1849. if (!iov_iter_count(iter))
  1850. goto out;
  1851. if (ret < nr) {
  1852. error = -EFAULT;
  1853. goto out;
  1854. }
  1855. continue;
  1856. page_not_up_to_date:
  1857. /* Get exclusive access to the page ... */
  1858. error = lock_page_killable(page);
  1859. if (unlikely(error))
  1860. goto readpage_error;
  1861. page_not_up_to_date_locked:
  1862. /* Did it get truncated before we got the lock? */
  1863. if (!page->mapping) {
  1864. unlock_page(page);
  1865. put_page(page);
  1866. continue;
  1867. }
  1868. /* Did somebody else fill it already? */
  1869. if (PageUptodate(page)) {
  1870. unlock_page(page);
  1871. goto page_ok;
  1872. }
  1873. readpage:
  1874. /*
  1875. * A previous I/O error may have been due to temporary
  1876. * failures, eg. multipath errors.
  1877. * PG_error will be set again if readpage fails.
  1878. */
  1879. ClearPageError(page);
  1880. /* Start the actual read. The read will unlock the page. */
  1881. error = mapping->a_ops->readpage(filp, page);
  1882. if (unlikely(error)) {
  1883. if (error == AOP_TRUNCATED_PAGE) {
  1884. put_page(page);
  1885. error = 0;
  1886. goto find_page;
  1887. }
  1888. goto readpage_error;
  1889. }
  1890. if (!PageUptodate(page)) {
  1891. error = lock_page_killable(page);
  1892. if (unlikely(error))
  1893. goto readpage_error;
  1894. if (!PageUptodate(page)) {
  1895. if (page->mapping == NULL) {
  1896. /*
  1897. * invalidate_mapping_pages got it
  1898. */
  1899. unlock_page(page);
  1900. put_page(page);
  1901. goto find_page;
  1902. }
  1903. unlock_page(page);
  1904. shrink_readahead_size_eio(filp, ra);
  1905. error = -EIO;
  1906. goto readpage_error;
  1907. }
  1908. unlock_page(page);
  1909. }
  1910. goto page_ok;
  1911. readpage_error:
  1912. /* UHHUH! A synchronous read error occurred. Report it */
  1913. put_page(page);
  1914. goto out;
  1915. no_cached_page:
  1916. /*
  1917. * Ok, it wasn't cached, so we need to create a new
  1918. * page..
  1919. */
  1920. page = page_cache_alloc_cold(mapping);
  1921. if (!page) {
  1922. error = -ENOMEM;
  1923. goto out;
  1924. }
  1925. error = add_to_page_cache_lru(page, mapping, index,
  1926. mapping_gfp_constraint(mapping, GFP_KERNEL));
  1927. if (error) {
  1928. put_page(page);
  1929. if (error == -EEXIST) {
  1930. error = 0;
  1931. goto find_page;
  1932. }
  1933. goto out;
  1934. }
  1935. goto readpage;
  1936. }
  1937. would_block:
  1938. error = -EAGAIN;
  1939. out:
  1940. ra->prev_pos = prev_index;
  1941. ra->prev_pos <<= PAGE_SHIFT;
  1942. ra->prev_pos |= prev_offset;
  1943. *ppos = ((loff_t)index << PAGE_SHIFT) + offset;
  1944. file_accessed(filp);
  1945. return written ? written : error;
  1946. }
  1947. /**
  1948. * generic_file_read_iter - generic filesystem read routine
  1949. * @iocb: kernel I/O control block
  1950. * @iter: destination for the data read
  1951. *
  1952. * This is the "read_iter()" routine for all filesystems
  1953. * that can use the page cache directly.
  1954. */
  1955. ssize_t
  1956. generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  1957. {
  1958. size_t count = iov_iter_count(iter);
  1959. ssize_t retval = 0;
  1960. if (!count)
  1961. goto out; /* skip atime */
  1962. if (iocb->ki_flags & IOCB_DIRECT) {
  1963. struct file *file = iocb->ki_filp;
  1964. struct address_space *mapping = file->f_mapping;
  1965. struct inode *inode = mapping->host;
  1966. loff_t size;
  1967. size = i_size_read(inode);
  1968. if (iocb->ki_flags & IOCB_NOWAIT) {
  1969. if (filemap_range_has_page(mapping, iocb->ki_pos,
  1970. iocb->ki_pos + count - 1))
  1971. return -EAGAIN;
  1972. } else {
  1973. retval = filemap_write_and_wait_range(mapping,
  1974. iocb->ki_pos,
  1975. iocb->ki_pos + count - 1);
  1976. if (retval < 0)
  1977. goto out;
  1978. }
  1979. file_accessed(file);
  1980. retval = mapping->a_ops->direct_IO(iocb, iter);
  1981. if (retval >= 0) {
  1982. iocb->ki_pos += retval;
  1983. count -= retval;
  1984. }
  1985. iov_iter_revert(iter, count - iov_iter_count(iter));
  1986. /*
  1987. * Btrfs can have a short DIO read if we encounter
  1988. * compressed extents, so if there was an error, or if
  1989. * we've already read everything we wanted to, or if
  1990. * there was a short read because we hit EOF, go ahead
  1991. * and return. Otherwise fallthrough to buffered io for
  1992. * the rest of the read. Buffered reads will not work for
  1993. * DAX files, so don't bother trying.
  1994. */
  1995. if (retval < 0 || !count || iocb->ki_pos >= size ||
  1996. IS_DAX(inode))
  1997. goto out;
  1998. }
  1999. retval = generic_file_buffered_read(iocb, iter, retval);
  2000. out:
  2001. return retval;
  2002. }
  2003. EXPORT_SYMBOL(generic_file_read_iter);
  2004. #ifdef CONFIG_MMU
  2005. /**
  2006. * page_cache_read - adds requested page to the page cache if not already there
  2007. * @file: file to read
  2008. * @offset: page index
  2009. * @gfp_mask: memory allocation flags
  2010. *
  2011. * This adds the requested page to the page cache if it isn't already there,
  2012. * and schedules an I/O to read in its contents from disk.
  2013. */
  2014. static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)
  2015. {
  2016. struct address_space *mapping = file->f_mapping;
  2017. struct page *page;
  2018. int ret;
  2019. do {
  2020. page = __page_cache_alloc(gfp_mask|__GFP_COLD);
  2021. if (!page)
  2022. return -ENOMEM;
  2023. ret = add_to_page_cache_lru(page, mapping, offset, gfp_mask & GFP_KERNEL);
  2024. if (ret == 0)
  2025. ret = mapping->a_ops->readpage(file, page);
  2026. else if (ret == -EEXIST)
  2027. ret = 0; /* losing race to add is OK */
  2028. put_page(page);
  2029. } while (ret == AOP_TRUNCATED_PAGE);
  2030. return ret;
  2031. }
  2032. #define MMAP_LOTSAMISS (100)
  2033. /*
  2034. * Synchronous readahead happens when we don't even find
  2035. * a page in the page cache at all.
  2036. */
  2037. static void do_sync_mmap_readahead(struct vm_area_struct *vma,
  2038. struct file_ra_state *ra,
  2039. struct file *file,
  2040. pgoff_t offset)
  2041. {
  2042. struct address_space *mapping = file->f_mapping;
  2043. /* If we don't want any read-ahead, don't bother */
  2044. if (vma->vm_flags & VM_RAND_READ)
  2045. return;
  2046. if (!ra->ra_pages)
  2047. return;
  2048. if (vma->vm_flags & VM_SEQ_READ) {
  2049. page_cache_sync_readahead(mapping, ra, file, offset,
  2050. ra->ra_pages);
  2051. return;
  2052. }
  2053. /* Avoid banging the cache line if not needed */
  2054. if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
  2055. ra->mmap_miss++;
  2056. /*
  2057. * Do we miss much more than hit in this file? If so,
  2058. * stop bothering with read-ahead. It will only hurt.
  2059. */
  2060. if (ra->mmap_miss > MMAP_LOTSAMISS)
  2061. return;
  2062. /*
  2063. * mmap read-around
  2064. */
  2065. ra->start = max_t(long, 0, offset - ra->ra_pages / 2);
  2066. ra->size = ra->ra_pages;
  2067. ra->async_size = ra->ra_pages / 4;
  2068. ra_submit(ra, mapping, file);
  2069. }
  2070. /*
  2071. * Asynchronous readahead happens when we find the page and PG_readahead,
  2072. * so we want to possibly extend the readahead further..
  2073. */
  2074. static void do_async_mmap_readahead(struct vm_area_struct *vma,
  2075. struct file_ra_state *ra,
  2076. struct file *file,
  2077. struct page *page,
  2078. pgoff_t offset)
  2079. {
  2080. struct address_space *mapping = file->f_mapping;
  2081. /* If we don't want any read-ahead, don't bother */
  2082. if (vma->vm_flags & VM_RAND_READ)
  2083. return;
  2084. if (ra->mmap_miss > 0)
  2085. ra->mmap_miss--;
  2086. if (PageReadahead(page))
  2087. page_cache_async_readahead(mapping, ra, file,
  2088. page, offset, ra->ra_pages);
  2089. }
  2090. /**
  2091. * filemap_fault - read in file data for page fault handling
  2092. * @vmf: struct vm_fault containing details of the fault
  2093. *
  2094. * filemap_fault() is invoked via the vma operations vector for a
  2095. * mapped memory region to read in file data during a page fault.
  2096. *
  2097. * The goto's are kind of ugly, but this streamlines the normal case of having
  2098. * it in the page cache, and handles the special cases reasonably without
  2099. * having a lot of duplicated code.
  2100. *
  2101. * vma->vm_mm->mmap_sem must be held on entry.
  2102. *
  2103. * If our return value has VM_FAULT_RETRY set, it's because
  2104. * lock_page_or_retry() returned 0.
  2105. * The mmap_sem has usually been released in this case.
  2106. * See __lock_page_or_retry() for the exception.
  2107. *
  2108. * If our return value does not have VM_FAULT_RETRY set, the mmap_sem
  2109. * has not been released.
  2110. *
  2111. * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
  2112. */
  2113. int filemap_fault(struct vm_fault *vmf)
  2114. {
  2115. int error;
  2116. struct file *file = vmf->vma->vm_file;
  2117. struct address_space *mapping = file->f_mapping;
  2118. struct file_ra_state *ra = &file->f_ra;
  2119. struct inode *inode = mapping->host;
  2120. pgoff_t offset = vmf->pgoff;
  2121. pgoff_t max_off;
  2122. struct page *page;
  2123. int ret = 0;
  2124. max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  2125. if (unlikely(offset >= max_off))
  2126. return VM_FAULT_SIGBUS;
  2127. /*
  2128. * Do we have something in the page cache already?
  2129. */
  2130. page = find_get_page(mapping, offset);
  2131. if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) {
  2132. /*
  2133. * We found the page, so try async readahead before
  2134. * waiting for the lock.
  2135. */
  2136. do_async_mmap_readahead(vmf->vma, ra, file, page, offset);
  2137. } else if (!page) {
  2138. /* No page in the page cache at all */
  2139. do_sync_mmap_readahead(vmf->vma, ra, file, offset);
  2140. count_vm_event(PGMAJFAULT);
  2141. count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
  2142. ret = VM_FAULT_MAJOR;
  2143. retry_find:
  2144. page = find_get_page(mapping, offset);
  2145. if (!page)
  2146. goto no_cached_page;
  2147. }
  2148. if (!lock_page_or_retry(page, vmf->vma->vm_mm, vmf->flags)) {
  2149. put_page(page);
  2150. return ret | VM_FAULT_RETRY;
  2151. }
  2152. /* Did it get truncated? */
  2153. if (unlikely(page->mapping != mapping)) {
  2154. unlock_page(page);
  2155. put_page(page);
  2156. goto retry_find;
  2157. }
  2158. VM_BUG_ON_PAGE(page->index != offset, page);
  2159. /*
  2160. * We have a locked page in the page cache, now we need to check
  2161. * that it's up-to-date. If not, it is going to be due to an error.
  2162. */
  2163. if (unlikely(!PageUptodate(page)))
  2164. goto page_not_uptodate;
  2165. /*
  2166. * Found the page and have a reference on it.
  2167. * We must recheck i_size under page lock.
  2168. */
  2169. max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  2170. if (unlikely(offset >= max_off)) {
  2171. unlock_page(page);
  2172. put_page(page);
  2173. return VM_FAULT_SIGBUS;
  2174. }
  2175. vmf->page = page;
  2176. return ret | VM_FAULT_LOCKED;
  2177. no_cached_page:
  2178. /*
  2179. * We're only likely to ever get here if MADV_RANDOM is in
  2180. * effect.
  2181. */
  2182. error = page_cache_read(file, offset, vmf->gfp_mask);
  2183. /*
  2184. * The page we want has now been added to the page cache.
  2185. * In the unlikely event that someone removed it in the
  2186. * meantime, we'll just come back here and read it again.
  2187. */
  2188. if (error >= 0)
  2189. goto retry_find;
  2190. /*
  2191. * An error return from page_cache_read can result if the
  2192. * system is low on memory, or a problem occurs while trying
  2193. * to schedule I/O.
  2194. */
  2195. if (error == -ENOMEM)
  2196. return VM_FAULT_OOM;
  2197. return VM_FAULT_SIGBUS;
  2198. page_not_uptodate:
  2199. /*
  2200. * Umm, take care of errors if the page isn't up-to-date.
  2201. * Try to re-read it _once_. We do this synchronously,
  2202. * because there really aren't any performance issues here
  2203. * and we need to check for errors.
  2204. */
  2205. ClearPageError(page);
  2206. error = mapping->a_ops->readpage(file, page);
  2207. if (!error) {
  2208. wait_on_page_locked(page);
  2209. if (!PageUptodate(page))
  2210. error = -EIO;
  2211. }
  2212. put_page(page);
  2213. if (!error || error == AOP_TRUNCATED_PAGE)
  2214. goto retry_find;
  2215. /* Things didn't work out. Return zero to tell the mm layer so. */
  2216. shrink_readahead_size_eio(file, ra);
  2217. return VM_FAULT_SIGBUS;
  2218. }
  2219. EXPORT_SYMBOL(filemap_fault);
  2220. void filemap_map_pages(struct vm_fault *vmf,
  2221. pgoff_t start_pgoff, pgoff_t end_pgoff)
  2222. {
  2223. struct radix_tree_iter iter;
  2224. void **slot;
  2225. struct file *file = vmf->vma->vm_file;
  2226. struct address_space *mapping = file->f_mapping;
  2227. pgoff_t last_pgoff = start_pgoff;
  2228. unsigned long max_idx;
  2229. struct page *head, *page;
  2230. rcu_read_lock();
  2231. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
  2232. start_pgoff) {
  2233. if (iter.index > end_pgoff)
  2234. break;
  2235. repeat:
  2236. page = radix_tree_deref_slot(slot);
  2237. if (unlikely(!page))
  2238. goto next;
  2239. if (radix_tree_exception(page)) {
  2240. if (radix_tree_deref_retry(page)) {
  2241. slot = radix_tree_iter_retry(&iter);
  2242. continue;
  2243. }
  2244. goto next;
  2245. }
  2246. head = compound_head(page);
  2247. if (!page_cache_get_speculative(head))
  2248. goto repeat;
  2249. /* The page was split under us? */
  2250. if (compound_head(page) != head) {
  2251. put_page(head);
  2252. goto repeat;
  2253. }
  2254. /* Has the page moved? */
  2255. if (unlikely(page != *slot)) {
  2256. put_page(head);
  2257. goto repeat;
  2258. }
  2259. if (!PageUptodate(page) ||
  2260. PageReadahead(page) ||
  2261. PageHWPoison(page))
  2262. goto skip;
  2263. if (!trylock_page(page))
  2264. goto skip;
  2265. if (page->mapping != mapping || !PageUptodate(page))
  2266. goto unlock;
  2267. max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
  2268. if (page->index >= max_idx)
  2269. goto unlock;
  2270. if (file->f_ra.mmap_miss > 0)
  2271. file->f_ra.mmap_miss--;
  2272. vmf->address += (iter.index - last_pgoff) << PAGE_SHIFT;
  2273. if (vmf->pte)
  2274. vmf->pte += iter.index - last_pgoff;
  2275. last_pgoff = iter.index;
  2276. if (alloc_set_pte(vmf, NULL, page))
  2277. goto unlock;
  2278. unlock_page(page);
  2279. goto next;
  2280. unlock:
  2281. unlock_page(page);
  2282. skip:
  2283. put_page(page);
  2284. next:
  2285. /* Huge page is mapped? No need to proceed. */
  2286. if (pmd_trans_huge(*vmf->pmd))
  2287. break;
  2288. if (iter.index == end_pgoff)
  2289. break;
  2290. }
  2291. rcu_read_unlock();
  2292. }
  2293. EXPORT_SYMBOL(filemap_map_pages);
  2294. int filemap_page_mkwrite(struct vm_fault *vmf)
  2295. {
  2296. struct page *page = vmf->page;
  2297. struct inode *inode = file_inode(vmf->vma->vm_file);
  2298. int ret = VM_FAULT_LOCKED;
  2299. sb_start_pagefault(inode->i_sb);
  2300. file_update_time(vmf->vma->vm_file);
  2301. lock_page(page);
  2302. if (page->mapping != inode->i_mapping) {
  2303. unlock_page(page);
  2304. ret = VM_FAULT_NOPAGE;
  2305. goto out;
  2306. }
  2307. /*
  2308. * We mark the page dirty already here so that when freeze is in
  2309. * progress, we are guaranteed that writeback during freezing will
  2310. * see the dirty page and writeprotect it again.
  2311. */
  2312. set_page_dirty(page);
  2313. wait_for_stable_page(page);
  2314. out:
  2315. sb_end_pagefault(inode->i_sb);
  2316. return ret;
  2317. }
  2318. EXPORT_SYMBOL(filemap_page_mkwrite);
  2319. const struct vm_operations_struct generic_file_vm_ops = {
  2320. .fault = filemap_fault,
  2321. .map_pages = filemap_map_pages,
  2322. .page_mkwrite = filemap_page_mkwrite,
  2323. };
  2324. /* This is used for a general mmap of a disk file */
  2325. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  2326. {
  2327. struct address_space *mapping = file->f_mapping;
  2328. if (!mapping->a_ops->readpage)
  2329. return -ENOEXEC;
  2330. file_accessed(file);
  2331. vma->vm_ops = &generic_file_vm_ops;
  2332. return 0;
  2333. }
  2334. /*
  2335. * This is for filesystems which do not implement ->writepage.
  2336. */
  2337. int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
  2338. {
  2339. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  2340. return -EINVAL;
  2341. return generic_file_mmap(file, vma);
  2342. }
  2343. #else
  2344. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  2345. {
  2346. return -ENOSYS;
  2347. }
  2348. int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
  2349. {
  2350. return -ENOSYS;
  2351. }
  2352. #endif /* CONFIG_MMU */
  2353. EXPORT_SYMBOL(generic_file_mmap);
  2354. EXPORT_SYMBOL(generic_file_readonly_mmap);
  2355. static struct page *wait_on_page_read(struct page *page)
  2356. {
  2357. if (!IS_ERR(page)) {
  2358. wait_on_page_locked(page);
  2359. if (!PageUptodate(page)) {
  2360. put_page(page);
  2361. page = ERR_PTR(-EIO);
  2362. }
  2363. }
  2364. return page;
  2365. }
  2366. static struct page *do_read_cache_page(struct address_space *mapping,
  2367. pgoff_t index,
  2368. int (*filler)(void *, struct page *),
  2369. void *data,
  2370. gfp_t gfp)
  2371. {
  2372. struct page *page;
  2373. int err;
  2374. repeat:
  2375. page = find_get_page(mapping, index);
  2376. if (!page) {
  2377. page = __page_cache_alloc(gfp | __GFP_COLD);
  2378. if (!page)
  2379. return ERR_PTR(-ENOMEM);
  2380. err = add_to_page_cache_lru(page, mapping, index, gfp);
  2381. if (unlikely(err)) {
  2382. put_page(page);
  2383. if (err == -EEXIST)
  2384. goto repeat;
  2385. /* Presumably ENOMEM for radix tree node */
  2386. return ERR_PTR(err);
  2387. }
  2388. filler:
  2389. err = filler(data, page);
  2390. if (err < 0) {
  2391. put_page(page);
  2392. return ERR_PTR(err);
  2393. }
  2394. page = wait_on_page_read(page);
  2395. if (IS_ERR(page))
  2396. return page;
  2397. goto out;
  2398. }
  2399. if (PageUptodate(page))
  2400. goto out;
  2401. /*
  2402. * Page is not up to date and may be locked due one of the following
  2403. * case a: Page is being filled and the page lock is held
  2404. * case b: Read/write error clearing the page uptodate status
  2405. * case c: Truncation in progress (page locked)
  2406. * case d: Reclaim in progress
  2407. *
  2408. * Case a, the page will be up to date when the page is unlocked.
  2409. * There is no need to serialise on the page lock here as the page
  2410. * is pinned so the lock gives no additional protection. Even if the
  2411. * the page is truncated, the data is still valid if PageUptodate as
  2412. * it's a race vs truncate race.
  2413. * Case b, the page will not be up to date
  2414. * Case c, the page may be truncated but in itself, the data may still
  2415. * be valid after IO completes as it's a read vs truncate race. The
  2416. * operation must restart if the page is not uptodate on unlock but
  2417. * otherwise serialising on page lock to stabilise the mapping gives
  2418. * no additional guarantees to the caller as the page lock is
  2419. * released before return.
  2420. * Case d, similar to truncation. If reclaim holds the page lock, it
  2421. * will be a race with remove_mapping that determines if the mapping
  2422. * is valid on unlock but otherwise the data is valid and there is
  2423. * no need to serialise with page lock.
  2424. *
  2425. * As the page lock gives no additional guarantee, we optimistically
  2426. * wait on the page to be unlocked and check if it's up to date and
  2427. * use the page if it is. Otherwise, the page lock is required to
  2428. * distinguish between the different cases. The motivation is that we
  2429. * avoid spurious serialisations and wakeups when multiple processes
  2430. * wait on the same page for IO to complete.
  2431. */
  2432. wait_on_page_locked(page);
  2433. if (PageUptodate(page))
  2434. goto out;
  2435. /* Distinguish between all the cases under the safety of the lock */
  2436. lock_page(page);
  2437. /* Case c or d, restart the operation */
  2438. if (!page->mapping) {
  2439. unlock_page(page);
  2440. put_page(page);
  2441. goto repeat;
  2442. }
  2443. /* Someone else locked and filled the page in a very small window */
  2444. if (PageUptodate(page)) {
  2445. unlock_page(page);
  2446. goto out;
  2447. }
  2448. goto filler;
  2449. out:
  2450. mark_page_accessed(page);
  2451. return page;
  2452. }
  2453. /**
  2454. * read_cache_page - read into page cache, fill it if needed
  2455. * @mapping: the page's address_space
  2456. * @index: the page index
  2457. * @filler: function to perform the read
  2458. * @data: first arg to filler(data, page) function, often left as NULL
  2459. *
  2460. * Read into the page cache. If a page already exists, and PageUptodate() is
  2461. * not set, try to fill the page and wait for it to become unlocked.
  2462. *
  2463. * If the page does not get brought uptodate, return -EIO.
  2464. */
  2465. struct page *read_cache_page(struct address_space *mapping,
  2466. pgoff_t index,
  2467. int (*filler)(void *, struct page *),
  2468. void *data)
  2469. {
  2470. return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
  2471. }
  2472. EXPORT_SYMBOL(read_cache_page);
  2473. /**
  2474. * read_cache_page_gfp - read into page cache, using specified page allocation flags.
  2475. * @mapping: the page's address_space
  2476. * @index: the page index
  2477. * @gfp: the page allocator flags to use if allocating
  2478. *
  2479. * This is the same as "read_mapping_page(mapping, index, NULL)", but with
  2480. * any new page allocations done using the specified allocation flags.
  2481. *
  2482. * If the page does not get brought uptodate, return -EIO.
  2483. */
  2484. struct page *read_cache_page_gfp(struct address_space *mapping,
  2485. pgoff_t index,
  2486. gfp_t gfp)
  2487. {
  2488. filler_t *filler = (filler_t *)mapping->a_ops->readpage;
  2489. return do_read_cache_page(mapping, index, filler, NULL, gfp);
  2490. }
  2491. EXPORT_SYMBOL(read_cache_page_gfp);
  2492. /*
  2493. * Performs necessary checks before doing a write
  2494. *
  2495. * Can adjust writing position or amount of bytes to write.
  2496. * Returns appropriate error code that caller should return or
  2497. * zero in case that write should be allowed.
  2498. */
  2499. inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
  2500. {
  2501. struct file *file = iocb->ki_filp;
  2502. struct inode *inode = file->f_mapping->host;
  2503. unsigned long limit = rlimit(RLIMIT_FSIZE);
  2504. loff_t pos;
  2505. if (!iov_iter_count(from))
  2506. return 0;
  2507. /* FIXME: this is for backwards compatibility with 2.4 */
  2508. if (iocb->ki_flags & IOCB_APPEND)
  2509. iocb->ki_pos = i_size_read(inode);
  2510. pos = iocb->ki_pos;
  2511. if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
  2512. return -EINVAL;
  2513. if (limit != RLIM_INFINITY) {
  2514. if (iocb->ki_pos >= limit) {
  2515. send_sig(SIGXFSZ, current, 0);
  2516. return -EFBIG;
  2517. }
  2518. iov_iter_truncate(from, limit - (unsigned long)pos);
  2519. }
  2520. /*
  2521. * LFS rule
  2522. */
  2523. if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
  2524. !(file->f_flags & O_LARGEFILE))) {
  2525. if (pos >= MAX_NON_LFS)
  2526. return -EFBIG;
  2527. iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
  2528. }
  2529. /*
  2530. * Are we about to exceed the fs block limit ?
  2531. *
  2532. * If we have written data it becomes a short write. If we have
  2533. * exceeded without writing data we send a signal and return EFBIG.
  2534. * Linus frestrict idea will clean these up nicely..
  2535. */
  2536. if (unlikely(pos >= inode->i_sb->s_maxbytes))
  2537. return -EFBIG;
  2538. iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
  2539. return iov_iter_count(from);
  2540. }
  2541. EXPORT_SYMBOL(generic_write_checks);
  2542. int pagecache_write_begin(struct file *file, struct address_space *mapping,
  2543. loff_t pos, unsigned len, unsigned flags,
  2544. struct page **pagep, void **fsdata)
  2545. {
  2546. const struct address_space_operations *aops = mapping->a_ops;
  2547. return aops->write_begin(file, mapping, pos, len, flags,
  2548. pagep, fsdata);
  2549. }
  2550. EXPORT_SYMBOL(pagecache_write_begin);
  2551. int pagecache_write_end(struct file *file, struct address_space *mapping,
  2552. loff_t pos, unsigned len, unsigned copied,
  2553. struct page *page, void *fsdata)
  2554. {
  2555. const struct address_space_operations *aops = mapping->a_ops;
  2556. return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
  2557. }
  2558. EXPORT_SYMBOL(pagecache_write_end);
  2559. ssize_t
  2560. generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
  2561. {
  2562. struct file *file = iocb->ki_filp;
  2563. struct address_space *mapping = file->f_mapping;
  2564. struct inode *inode = mapping->host;
  2565. loff_t pos = iocb->ki_pos;
  2566. ssize_t written;
  2567. size_t write_len;
  2568. pgoff_t end;
  2569. write_len = iov_iter_count(from);
  2570. end = (pos + write_len - 1) >> PAGE_SHIFT;
  2571. if (iocb->ki_flags & IOCB_NOWAIT) {
  2572. /* If there are pages to writeback, return */
  2573. if (filemap_range_has_page(inode->i_mapping, pos,
  2574. pos + iov_iter_count(from)))
  2575. return -EAGAIN;
  2576. } else {
  2577. written = filemap_write_and_wait_range(mapping, pos,
  2578. pos + write_len - 1);
  2579. if (written)
  2580. goto out;
  2581. }
  2582. /*
  2583. * After a write we want buffered reads to be sure to go to disk to get
  2584. * the new data. We invalidate clean cached page from the region we're
  2585. * about to write. We do this *before* the write so that we can return
  2586. * without clobbering -EIOCBQUEUED from ->direct_IO().
  2587. */
  2588. written = invalidate_inode_pages2_range(mapping,
  2589. pos >> PAGE_SHIFT, end);
  2590. /*
  2591. * If a page can not be invalidated, return 0 to fall back
  2592. * to buffered write.
  2593. */
  2594. if (written) {
  2595. if (written == -EBUSY)
  2596. return 0;
  2597. goto out;
  2598. }
  2599. written = mapping->a_ops->direct_IO(iocb, from);
  2600. /*
  2601. * Finally, try again to invalidate clean pages which might have been
  2602. * cached by non-direct readahead, or faulted in by get_user_pages()
  2603. * if the source of the write was an mmap'ed region of the file
  2604. * we're writing. Either one is a pretty crazy thing to do,
  2605. * so we don't support it 100%. If this invalidation
  2606. * fails, tough, the write still worked...
  2607. */
  2608. invalidate_inode_pages2_range(mapping,
  2609. pos >> PAGE_SHIFT, end);
  2610. if (written > 0) {
  2611. pos += written;
  2612. write_len -= written;
  2613. if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
  2614. i_size_write(inode, pos);
  2615. mark_inode_dirty(inode);
  2616. }
  2617. iocb->ki_pos = pos;
  2618. }
  2619. iov_iter_revert(from, write_len - iov_iter_count(from));
  2620. out:
  2621. return written;
  2622. }
  2623. EXPORT_SYMBOL(generic_file_direct_write);
  2624. /*
  2625. * Find or create a page at the given pagecache position. Return the locked
  2626. * page. This function is specifically for buffered writes.
  2627. */
  2628. struct page *grab_cache_page_write_begin(struct address_space *mapping,
  2629. pgoff_t index, unsigned flags)
  2630. {
  2631. struct page *page;
  2632. int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
  2633. if (flags & AOP_FLAG_NOFS)
  2634. fgp_flags |= FGP_NOFS;
  2635. page = pagecache_get_page(mapping, index, fgp_flags,
  2636. mapping_gfp_mask(mapping));
  2637. if (page)
  2638. wait_for_stable_page(page);
  2639. return page;
  2640. }
  2641. EXPORT_SYMBOL(grab_cache_page_write_begin);
  2642. ssize_t generic_perform_write(struct file *file,
  2643. struct iov_iter *i, loff_t pos)
  2644. {
  2645. struct address_space *mapping = file->f_mapping;
  2646. const struct address_space_operations *a_ops = mapping->a_ops;
  2647. long status = 0;
  2648. ssize_t written = 0;
  2649. unsigned int flags = 0;
  2650. do {
  2651. struct page *page;
  2652. unsigned long offset; /* Offset into pagecache page */
  2653. unsigned long bytes; /* Bytes to write to page */
  2654. size_t copied; /* Bytes copied from user */
  2655. void *fsdata;
  2656. offset = (pos & (PAGE_SIZE - 1));
  2657. bytes = min_t(unsigned long, PAGE_SIZE - offset,
  2658. iov_iter_count(i));
  2659. again:
  2660. /*
  2661. * Bring in the user page that we will copy from _first_.
  2662. * Otherwise there's a nasty deadlock on copying from the
  2663. * same page as we're writing to, without it being marked
  2664. * up-to-date.
  2665. *
  2666. * Not only is this an optimisation, but it is also required
  2667. * to check that the address is actually valid, when atomic
  2668. * usercopies are used, below.
  2669. */
  2670. if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
  2671. status = -EFAULT;
  2672. break;
  2673. }
  2674. if (fatal_signal_pending(current)) {
  2675. status = -EINTR;
  2676. break;
  2677. }
  2678. status = a_ops->write_begin(file, mapping, pos, bytes, flags,
  2679. &page, &fsdata);
  2680. if (unlikely(status < 0))
  2681. break;
  2682. if (mapping_writably_mapped(mapping))
  2683. flush_dcache_page(page);
  2684. copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
  2685. flush_dcache_page(page);
  2686. status = a_ops->write_end(file, mapping, pos, bytes, copied,
  2687. page, fsdata);
  2688. if (unlikely(status < 0))
  2689. break;
  2690. copied = status;
  2691. cond_resched();
  2692. iov_iter_advance(i, copied);
  2693. if (unlikely(copied == 0)) {
  2694. /*
  2695. * If we were unable to copy any data at all, we must
  2696. * fall back to a single segment length write.
  2697. *
  2698. * If we didn't fallback here, we could livelock
  2699. * because not all segments in the iov can be copied at
  2700. * once without a pagefault.
  2701. */
  2702. bytes = min_t(unsigned long, PAGE_SIZE - offset,
  2703. iov_iter_single_seg_count(i));
  2704. goto again;
  2705. }
  2706. pos += copied;
  2707. written += copied;
  2708. balance_dirty_pages_ratelimited(mapping);
  2709. } while (iov_iter_count(i));
  2710. return written ? written : status;
  2711. }
  2712. EXPORT_SYMBOL(generic_perform_write);
  2713. /**
  2714. * __generic_file_write_iter - write data to a file
  2715. * @iocb: IO state structure (file, offset, etc.)
  2716. * @from: iov_iter with data to write
  2717. *
  2718. * This function does all the work needed for actually writing data to a
  2719. * file. It does all basic checks, removes SUID from the file, updates
  2720. * modification times and calls proper subroutines depending on whether we
  2721. * do direct IO or a standard buffered write.
  2722. *
  2723. * It expects i_mutex to be grabbed unless we work on a block device or similar
  2724. * object which does not need locking at all.
  2725. *
  2726. * This function does *not* take care of syncing data in case of O_SYNC write.
  2727. * A caller has to handle it. This is mainly due to the fact that we want to
  2728. * avoid syncing under i_mutex.
  2729. */
  2730. ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  2731. {
  2732. struct file *file = iocb->ki_filp;
  2733. struct address_space * mapping = file->f_mapping;
  2734. struct inode *inode = mapping->host;
  2735. ssize_t written = 0;
  2736. ssize_t err;
  2737. ssize_t status;
  2738. /* We can write back this queue in page reclaim */
  2739. current->backing_dev_info = inode_to_bdi(inode);
  2740. err = file_remove_privs(file);
  2741. if (err)
  2742. goto out;
  2743. err = file_update_time(file);
  2744. if (err)
  2745. goto out;
  2746. if (iocb->ki_flags & IOCB_DIRECT) {
  2747. loff_t pos, endbyte;
  2748. written = generic_file_direct_write(iocb, from);
  2749. /*
  2750. * If the write stopped short of completing, fall back to
  2751. * buffered writes. Some filesystems do this for writes to
  2752. * holes, for example. For DAX files, a buffered write will
  2753. * not succeed (even if it did, DAX does not handle dirty
  2754. * page-cache pages correctly).
  2755. */
  2756. if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
  2757. goto out;
  2758. status = generic_perform_write(file, from, pos = iocb->ki_pos);
  2759. /*
  2760. * If generic_perform_write() returned a synchronous error
  2761. * then we want to return the number of bytes which were
  2762. * direct-written, or the error code if that was zero. Note
  2763. * that this differs from normal direct-io semantics, which
  2764. * will return -EFOO even if some bytes were written.
  2765. */
  2766. if (unlikely(status < 0)) {
  2767. err = status;
  2768. goto out;
  2769. }
  2770. /*
  2771. * We need to ensure that the page cache pages are written to
  2772. * disk and invalidated to preserve the expected O_DIRECT
  2773. * semantics.
  2774. */
  2775. endbyte = pos + status - 1;
  2776. err = filemap_write_and_wait_range(mapping, pos, endbyte);
  2777. if (err == 0) {
  2778. iocb->ki_pos = endbyte + 1;
  2779. written += status;
  2780. invalidate_mapping_pages(mapping,
  2781. pos >> PAGE_SHIFT,
  2782. endbyte >> PAGE_SHIFT);
  2783. } else {
  2784. /*
  2785. * We don't know how much we wrote, so just return
  2786. * the number of bytes which were direct-written
  2787. */
  2788. }
  2789. } else {
  2790. written = generic_perform_write(file, from, iocb->ki_pos);
  2791. if (likely(written > 0))
  2792. iocb->ki_pos += written;
  2793. }
  2794. out:
  2795. current->backing_dev_info = NULL;
  2796. return written ? written : err;
  2797. }
  2798. EXPORT_SYMBOL(__generic_file_write_iter);
  2799. /**
  2800. * generic_file_write_iter - write data to a file
  2801. * @iocb: IO state structure
  2802. * @from: iov_iter with data to write
  2803. *
  2804. * This is a wrapper around __generic_file_write_iter() to be used by most
  2805. * filesystems. It takes care of syncing the file in case of O_SYNC file
  2806. * and acquires i_mutex as needed.
  2807. */
  2808. ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  2809. {
  2810. struct file *file = iocb->ki_filp;
  2811. struct inode *inode = file->f_mapping->host;
  2812. ssize_t ret;
  2813. inode_lock(inode);
  2814. ret = generic_write_checks(iocb, from);
  2815. if (ret > 0)
  2816. ret = __generic_file_write_iter(iocb, from);
  2817. inode_unlock(inode);
  2818. if (ret > 0)
  2819. ret = generic_write_sync(iocb, ret);
  2820. return ret;
  2821. }
  2822. EXPORT_SYMBOL(generic_file_write_iter);
  2823. /**
  2824. * try_to_release_page() - release old fs-specific metadata on a page
  2825. *
  2826. * @page: the page which the kernel is trying to free
  2827. * @gfp_mask: memory allocation flags (and I/O mode)
  2828. *
  2829. * The address_space is to try to release any data against the page
  2830. * (presumably at page->private). If the release was successful, return '1'.
  2831. * Otherwise return zero.
  2832. *
  2833. * This may also be called if PG_fscache is set on a page, indicating that the
  2834. * page is known to the local caching routines.
  2835. *
  2836. * The @gfp_mask argument specifies whether I/O may be performed to release
  2837. * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
  2838. *
  2839. */
  2840. int try_to_release_page(struct page *page, gfp_t gfp_mask)
  2841. {
  2842. struct address_space * const mapping = page->mapping;
  2843. BUG_ON(!PageLocked(page));
  2844. if (PageWriteback(page))
  2845. return 0;
  2846. if (mapping && mapping->a_ops->releasepage)
  2847. return mapping->a_ops->releasepage(page, gfp_mask);
  2848. return try_to_free_buffers(page);
  2849. }
  2850. EXPORT_SYMBOL(try_to_release_page);