filemap.c 68 KB

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