filemap.c 70 KB

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