filemap.c 70 KB

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