filemap.c 70 KB

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