filemap.c 70 KB

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