filemap.c 71 KB

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