filemap.c 67 KB

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