filemap.c 69 KB

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