filemap.c 72 KB

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