filemap.c 69 KB

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