filemap.c 87 KB

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