filemap.c 68 KB

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