filemap.c 70 KB

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