filemap.c 70 KB

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