filemap.c 86 KB

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