filemap.c 70 KB

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