filemap.c 71 KB

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