filemap.c 68 KB

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