filemap.c 70 KB

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