filemap.c 68 KB

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