filemap.c 68 KB

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