filemap.c 78 KB

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