filemap.c 72 KB

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