filemap.c 69 KB

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