filemap.c 68 KB

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