filemap.c 69 KB

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