filemap.c 69 KB

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