filemap.c 70 KB

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