filemap.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  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. unsigned int i;
  734. unsigned int ret;
  735. unsigned int nr_found, nr_skip;
  736. rcu_read_lock();
  737. restart:
  738. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  739. (void ***)pages, NULL, start, nr_pages);
  740. ret = 0;
  741. nr_skip = 0;
  742. for (i = 0; i < nr_found; i++) {
  743. struct page *page;
  744. repeat:
  745. page = radix_tree_deref_slot((void **)pages[i]);
  746. if (unlikely(!page))
  747. continue;
  748. if (radix_tree_exception(page)) {
  749. if (radix_tree_deref_retry(page)) {
  750. /*
  751. * Transient condition which can only trigger
  752. * when entry at index 0 moves out of or back
  753. * to root: none yet gotten, safe to restart.
  754. */
  755. WARN_ON(start | i);
  756. goto restart;
  757. }
  758. /*
  759. * Otherwise, shmem/tmpfs must be storing a swap entry
  760. * here as an exceptional entry: so skip over it -
  761. * we only reach this from invalidate_mapping_pages().
  762. */
  763. nr_skip++;
  764. continue;
  765. }
  766. if (!page_cache_get_speculative(page))
  767. goto repeat;
  768. /* Has the page moved? */
  769. if (unlikely(page != *((void **)pages[i]))) {
  770. page_cache_release(page);
  771. goto repeat;
  772. }
  773. pages[ret] = page;
  774. ret++;
  775. }
  776. /*
  777. * If all entries were removed before we could secure them,
  778. * try again, because callers stop trying once 0 is returned.
  779. */
  780. if (unlikely(!ret && nr_found > nr_skip))
  781. goto restart;
  782. rcu_read_unlock();
  783. return ret;
  784. }
  785. /**
  786. * find_get_pages_contig - gang contiguous pagecache lookup
  787. * @mapping: The address_space to search
  788. * @index: The starting page index
  789. * @nr_pages: The maximum number of pages
  790. * @pages: Where the resulting pages are placed
  791. *
  792. * find_get_pages_contig() works exactly like find_get_pages(), except
  793. * that the returned number of pages are guaranteed to be contiguous.
  794. *
  795. * find_get_pages_contig() returns the number of pages which were found.
  796. */
  797. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
  798. unsigned int nr_pages, struct page **pages)
  799. {
  800. unsigned int i;
  801. unsigned int ret;
  802. unsigned int nr_found;
  803. rcu_read_lock();
  804. restart:
  805. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  806. (void ***)pages, NULL, index, nr_pages);
  807. ret = 0;
  808. for (i = 0; i < nr_found; i++) {
  809. struct page *page;
  810. repeat:
  811. page = radix_tree_deref_slot((void **)pages[i]);
  812. if (unlikely(!page))
  813. continue;
  814. if (radix_tree_exception(page)) {
  815. if (radix_tree_deref_retry(page)) {
  816. /*
  817. * Transient condition which can only trigger
  818. * when entry at index 0 moves out of or back
  819. * to root: none yet gotten, safe to restart.
  820. */
  821. goto restart;
  822. }
  823. /*
  824. * Otherwise, shmem/tmpfs must be storing a swap entry
  825. * here as an exceptional entry: so stop looking for
  826. * contiguous pages.
  827. */
  828. break;
  829. }
  830. if (!page_cache_get_speculative(page))
  831. goto repeat;
  832. /* Has the page moved? */
  833. if (unlikely(page != *((void **)pages[i]))) {
  834. page_cache_release(page);
  835. goto repeat;
  836. }
  837. /*
  838. * must check mapping and index after taking the ref.
  839. * otherwise we can get both false positives and false
  840. * negatives, which is just confusing to the caller.
  841. */
  842. if (page->mapping == NULL || page->index != index) {
  843. page_cache_release(page);
  844. break;
  845. }
  846. pages[ret] = page;
  847. ret++;
  848. index++;
  849. }
  850. rcu_read_unlock();
  851. return ret;
  852. }
  853. EXPORT_SYMBOL(find_get_pages_contig);
  854. /**
  855. * find_get_pages_tag - find and return pages that match @tag
  856. * @mapping: the address_space to search
  857. * @index: the starting page index
  858. * @tag: the tag index
  859. * @nr_pages: the maximum number of pages
  860. * @pages: where the resulting pages are placed
  861. *
  862. * Like find_get_pages, except we only return pages which are tagged with
  863. * @tag. We update @index to index the next page for the traversal.
  864. */
  865. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  866. int tag, unsigned int nr_pages, struct page **pages)
  867. {
  868. unsigned int i;
  869. unsigned int ret;
  870. unsigned int nr_found;
  871. rcu_read_lock();
  872. restart:
  873. nr_found = radix_tree_gang_lookup_tag_slot(&mapping->page_tree,
  874. (void ***)pages, *index, nr_pages, tag);
  875. ret = 0;
  876. for (i = 0; i < nr_found; i++) {
  877. struct page *page;
  878. repeat:
  879. page = radix_tree_deref_slot((void **)pages[i]);
  880. if (unlikely(!page))
  881. continue;
  882. if (radix_tree_exception(page)) {
  883. if (radix_tree_deref_retry(page)) {
  884. /*
  885. * Transient condition which can only trigger
  886. * when entry at index 0 moves out of or back
  887. * to root: none yet gotten, safe to restart.
  888. */
  889. goto restart;
  890. }
  891. /*
  892. * This function is never used on a shmem/tmpfs
  893. * mapping, so a swap entry won't be found here.
  894. */
  895. BUG();
  896. }
  897. if (!page_cache_get_speculative(page))
  898. goto repeat;
  899. /* Has the page moved? */
  900. if (unlikely(page != *((void **)pages[i]))) {
  901. page_cache_release(page);
  902. goto repeat;
  903. }
  904. pages[ret] = page;
  905. ret++;
  906. }
  907. /*
  908. * If all entries were removed before we could secure them,
  909. * try again, because callers stop trying once 0 is returned.
  910. */
  911. if (unlikely(!ret && nr_found))
  912. goto restart;
  913. rcu_read_unlock();
  914. if (ret)
  915. *index = pages[ret - 1]->index + 1;
  916. return ret;
  917. }
  918. EXPORT_SYMBOL(find_get_pages_tag);
  919. /**
  920. * grab_cache_page_nowait - returns locked page at given index in given cache
  921. * @mapping: target address_space
  922. * @index: the page index
  923. *
  924. * Same as grab_cache_page(), but do not wait if the page is unavailable.
  925. * This is intended for speculative data generators, where the data can
  926. * be regenerated if the page couldn't be grabbed. This routine should
  927. * be safe to call while holding the lock for another page.
  928. *
  929. * Clear __GFP_FS when allocating the page to avoid recursion into the fs
  930. * and deadlock against the caller's locked page.
  931. */
  932. struct page *
  933. grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
  934. {
  935. struct page *page = find_get_page(mapping, index);
  936. if (page) {
  937. if (trylock_page(page))
  938. return page;
  939. page_cache_release(page);
  940. return NULL;
  941. }
  942. page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
  943. if (page && add_to_page_cache_lru(page, mapping, index, GFP_NOFS)) {
  944. page_cache_release(page);
  945. page = NULL;
  946. }
  947. return page;
  948. }
  949. EXPORT_SYMBOL(grab_cache_page_nowait);
  950. /*
  951. * CD/DVDs are error prone. When a medium error occurs, the driver may fail
  952. * a _large_ part of the i/o request. Imagine the worst scenario:
  953. *
  954. * ---R__________________________________________B__________
  955. * ^ reading here ^ bad block(assume 4k)
  956. *
  957. * read(R) => miss => readahead(R...B) => media error => frustrating retries
  958. * => failing the whole request => read(R) => read(R+1) =>
  959. * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
  960. * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
  961. * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
  962. *
  963. * It is going insane. Fix it by quickly scaling down the readahead size.
  964. */
  965. static void shrink_readahead_size_eio(struct file *filp,
  966. struct file_ra_state *ra)
  967. {
  968. ra->ra_pages /= 4;
  969. }
  970. /**
  971. * do_generic_file_read - generic file read routine
  972. * @filp: the file to read
  973. * @ppos: current file position
  974. * @desc: read_descriptor
  975. * @actor: read method
  976. *
  977. * This is a generic file read routine, and uses the
  978. * mapping->a_ops->readpage() function for the actual low-level stuff.
  979. *
  980. * This is really ugly. But the goto's actually try to clarify some
  981. * of the logic when it comes to error handling etc.
  982. */
  983. static void do_generic_file_read(struct file *filp, loff_t *ppos,
  984. read_descriptor_t *desc, read_actor_t actor)
  985. {
  986. struct address_space *mapping = filp->f_mapping;
  987. struct inode *inode = mapping->host;
  988. struct file_ra_state *ra = &filp->f_ra;
  989. pgoff_t index;
  990. pgoff_t last_index;
  991. pgoff_t prev_index;
  992. unsigned long offset; /* offset into pagecache page */
  993. unsigned int prev_offset;
  994. int error;
  995. index = *ppos >> PAGE_CACHE_SHIFT;
  996. prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
  997. prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
  998. last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  999. offset = *ppos & ~PAGE_CACHE_MASK;
  1000. for (;;) {
  1001. struct page *page;
  1002. pgoff_t end_index;
  1003. loff_t isize;
  1004. unsigned long nr, ret;
  1005. cond_resched();
  1006. find_page:
  1007. page = find_get_page(mapping, index);
  1008. if (!page) {
  1009. page_cache_sync_readahead(mapping,
  1010. ra, filp,
  1011. index, last_index - index);
  1012. page = find_get_page(mapping, index);
  1013. if (unlikely(page == NULL))
  1014. goto no_cached_page;
  1015. }
  1016. if (PageReadahead(page)) {
  1017. page_cache_async_readahead(mapping,
  1018. ra, filp, page,
  1019. index, last_index - index);
  1020. }
  1021. if (!PageUptodate(page)) {
  1022. if (inode->i_blkbits == PAGE_CACHE_SHIFT ||
  1023. !mapping->a_ops->is_partially_uptodate)
  1024. goto page_not_up_to_date;
  1025. if (!trylock_page(page))
  1026. goto page_not_up_to_date;
  1027. /* Did it get truncated before we got the lock? */
  1028. if (!page->mapping)
  1029. goto page_not_up_to_date_locked;
  1030. if (!mapping->a_ops->is_partially_uptodate(page,
  1031. desc, offset))
  1032. goto page_not_up_to_date_locked;
  1033. unlock_page(page);
  1034. }
  1035. page_ok:
  1036. /*
  1037. * i_size must be checked after we know the page is Uptodate.
  1038. *
  1039. * Checking i_size after the check allows us to calculate
  1040. * the correct value for "nr", which means the zero-filled
  1041. * part of the page is not copied back to userspace (unless
  1042. * another truncate extends the file - this is desired though).
  1043. */
  1044. isize = i_size_read(inode);
  1045. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1046. if (unlikely(!isize || index > end_index)) {
  1047. page_cache_release(page);
  1048. goto out;
  1049. }
  1050. /* nr is the maximum number of bytes to copy from this page */
  1051. nr = PAGE_CACHE_SIZE;
  1052. if (index == end_index) {
  1053. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1054. if (nr <= offset) {
  1055. page_cache_release(page);
  1056. goto out;
  1057. }
  1058. }
  1059. nr = nr - offset;
  1060. /* If users can be writing to this page using arbitrary
  1061. * virtual addresses, take care about potential aliasing
  1062. * before reading the page on the kernel side.
  1063. */
  1064. if (mapping_writably_mapped(mapping))
  1065. flush_dcache_page(page);
  1066. /*
  1067. * When a sequential read accesses a page several times,
  1068. * only mark it as accessed the first time.
  1069. */
  1070. if (prev_index != index || offset != prev_offset)
  1071. mark_page_accessed(page);
  1072. prev_index = index;
  1073. /*
  1074. * Ok, we have the page, and it's up-to-date, so
  1075. * now we can copy it to user space...
  1076. *
  1077. * The actor routine returns how many bytes were actually used..
  1078. * NOTE! This may not be the same as how much of a user buffer
  1079. * we filled up (we may be padding etc), so we can only update
  1080. * "pos" here (the actor routine has to update the user buffer
  1081. * pointers and the remaining count).
  1082. */
  1083. ret = actor(desc, page, offset, nr);
  1084. offset += ret;
  1085. index += offset >> PAGE_CACHE_SHIFT;
  1086. offset &= ~PAGE_CACHE_MASK;
  1087. prev_offset = offset;
  1088. page_cache_release(page);
  1089. if (ret == nr && desc->count)
  1090. continue;
  1091. goto out;
  1092. page_not_up_to_date:
  1093. /* Get exclusive access to the page ... */
  1094. error = lock_page_killable(page);
  1095. if (unlikely(error))
  1096. goto readpage_error;
  1097. page_not_up_to_date_locked:
  1098. /* Did it get truncated before we got the lock? */
  1099. if (!page->mapping) {
  1100. unlock_page(page);
  1101. page_cache_release(page);
  1102. continue;
  1103. }
  1104. /* Did somebody else fill it already? */
  1105. if (PageUptodate(page)) {
  1106. unlock_page(page);
  1107. goto page_ok;
  1108. }
  1109. readpage:
  1110. /*
  1111. * A previous I/O error may have been due to temporary
  1112. * failures, eg. multipath errors.
  1113. * PG_error will be set again if readpage fails.
  1114. */
  1115. ClearPageError(page);
  1116. /* Start the actual read. The read will unlock the page. */
  1117. error = mapping->a_ops->readpage(filp, page);
  1118. if (unlikely(error)) {
  1119. if (error == AOP_TRUNCATED_PAGE) {
  1120. page_cache_release(page);
  1121. goto find_page;
  1122. }
  1123. goto readpage_error;
  1124. }
  1125. if (!PageUptodate(page)) {
  1126. error = lock_page_killable(page);
  1127. if (unlikely(error))
  1128. goto readpage_error;
  1129. if (!PageUptodate(page)) {
  1130. if (page->mapping == NULL) {
  1131. /*
  1132. * invalidate_mapping_pages got it
  1133. */
  1134. unlock_page(page);
  1135. page_cache_release(page);
  1136. goto find_page;
  1137. }
  1138. unlock_page(page);
  1139. shrink_readahead_size_eio(filp, ra);
  1140. error = -EIO;
  1141. goto readpage_error;
  1142. }
  1143. unlock_page(page);
  1144. }
  1145. goto page_ok;
  1146. readpage_error:
  1147. /* UHHUH! A synchronous read error occurred. Report it */
  1148. desc->error = error;
  1149. page_cache_release(page);
  1150. goto out;
  1151. no_cached_page:
  1152. /*
  1153. * Ok, it wasn't cached, so we need to create a new
  1154. * page..
  1155. */
  1156. page = page_cache_alloc_cold(mapping);
  1157. if (!page) {
  1158. desc->error = -ENOMEM;
  1159. goto out;
  1160. }
  1161. error = add_to_page_cache_lru(page, mapping,
  1162. index, GFP_KERNEL);
  1163. if (error) {
  1164. page_cache_release(page);
  1165. if (error == -EEXIST)
  1166. goto find_page;
  1167. desc->error = error;
  1168. goto out;
  1169. }
  1170. goto readpage;
  1171. }
  1172. out:
  1173. ra->prev_pos = prev_index;
  1174. ra->prev_pos <<= PAGE_CACHE_SHIFT;
  1175. ra->prev_pos |= prev_offset;
  1176. *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
  1177. file_accessed(filp);
  1178. }
  1179. int file_read_actor(read_descriptor_t *desc, struct page *page,
  1180. unsigned long offset, unsigned long size)
  1181. {
  1182. char *kaddr;
  1183. unsigned long left, count = desc->count;
  1184. if (size > count)
  1185. size = count;
  1186. /*
  1187. * Faults on the destination of a read are common, so do it before
  1188. * taking the kmap.
  1189. */
  1190. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1191. kaddr = kmap_atomic(page);
  1192. left = __copy_to_user_inatomic(desc->arg.buf,
  1193. kaddr + offset, size);
  1194. kunmap_atomic(kaddr);
  1195. if (left == 0)
  1196. goto success;
  1197. }
  1198. /* Do it the slow way */
  1199. kaddr = kmap(page);
  1200. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1201. kunmap(page);
  1202. if (left) {
  1203. size -= left;
  1204. desc->error = -EFAULT;
  1205. }
  1206. success:
  1207. desc->count = count - size;
  1208. desc->written += size;
  1209. desc->arg.buf += size;
  1210. return size;
  1211. }
  1212. /*
  1213. * Performs necessary checks before doing a write
  1214. * @iov: io vector request
  1215. * @nr_segs: number of segments in the iovec
  1216. * @count: number of bytes to write
  1217. * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
  1218. *
  1219. * Adjust number of segments and amount of bytes to write (nr_segs should be
  1220. * properly initialized first). Returns appropriate error code that caller
  1221. * should return or zero in case that write should be allowed.
  1222. */
  1223. int generic_segment_checks(const struct iovec *iov,
  1224. unsigned long *nr_segs, size_t *count, int access_flags)
  1225. {
  1226. unsigned long seg;
  1227. size_t cnt = 0;
  1228. for (seg = 0; seg < *nr_segs; seg++) {
  1229. const struct iovec *iv = &iov[seg];
  1230. /*
  1231. * If any segment has a negative length, or the cumulative
  1232. * length ever wraps negative then return -EINVAL.
  1233. */
  1234. cnt += iv->iov_len;
  1235. if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
  1236. return -EINVAL;
  1237. if (access_ok(access_flags, iv->iov_base, iv->iov_len))
  1238. continue;
  1239. if (seg == 0)
  1240. return -EFAULT;
  1241. *nr_segs = seg;
  1242. cnt -= iv->iov_len; /* This segment is no good */
  1243. break;
  1244. }
  1245. *count = cnt;
  1246. return 0;
  1247. }
  1248. EXPORT_SYMBOL(generic_segment_checks);
  1249. /**
  1250. * generic_file_aio_read - generic filesystem read routine
  1251. * @iocb: kernel I/O control block
  1252. * @iov: io vector request
  1253. * @nr_segs: number of segments in the iovec
  1254. * @pos: current file position
  1255. *
  1256. * This is the "read()" routine for all filesystems
  1257. * that can use the page cache directly.
  1258. */
  1259. ssize_t
  1260. generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1261. unsigned long nr_segs, loff_t pos)
  1262. {
  1263. struct file *filp = iocb->ki_filp;
  1264. ssize_t retval;
  1265. unsigned long seg = 0;
  1266. size_t count;
  1267. loff_t *ppos = &iocb->ki_pos;
  1268. count = 0;
  1269. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1270. if (retval)
  1271. return retval;
  1272. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1273. if (filp->f_flags & O_DIRECT) {
  1274. loff_t size;
  1275. struct address_space *mapping;
  1276. struct inode *inode;
  1277. mapping = filp->f_mapping;
  1278. inode = mapping->host;
  1279. if (!count)
  1280. goto out; /* skip atime */
  1281. size = i_size_read(inode);
  1282. if (pos < size) {
  1283. retval = filemap_write_and_wait_range(mapping, pos,
  1284. pos + iov_length(iov, nr_segs) - 1);
  1285. if (!retval) {
  1286. struct blk_plug plug;
  1287. blk_start_plug(&plug);
  1288. retval = mapping->a_ops->direct_IO(READ, iocb,
  1289. iov, pos, nr_segs);
  1290. blk_finish_plug(&plug);
  1291. }
  1292. if (retval > 0) {
  1293. *ppos = pos + retval;
  1294. count -= retval;
  1295. }
  1296. /*
  1297. * Btrfs can have a short DIO read if we encounter
  1298. * compressed extents, so if there was an error, or if
  1299. * we've already read everything we wanted to, or if
  1300. * there was a short read because we hit EOF, go ahead
  1301. * and return. Otherwise fallthrough to buffered io for
  1302. * the rest of the read.
  1303. */
  1304. if (retval < 0 || !count || *ppos >= size) {
  1305. file_accessed(filp);
  1306. goto out;
  1307. }
  1308. }
  1309. }
  1310. count = retval;
  1311. for (seg = 0; seg < nr_segs; seg++) {
  1312. read_descriptor_t desc;
  1313. loff_t offset = 0;
  1314. /*
  1315. * If we did a short DIO read we need to skip the section of the
  1316. * iov that we've already read data into.
  1317. */
  1318. if (count) {
  1319. if (count > iov[seg].iov_len) {
  1320. count -= iov[seg].iov_len;
  1321. continue;
  1322. }
  1323. offset = count;
  1324. count = 0;
  1325. }
  1326. desc.written = 0;
  1327. desc.arg.buf = iov[seg].iov_base + offset;
  1328. desc.count = iov[seg].iov_len - offset;
  1329. if (desc.count == 0)
  1330. continue;
  1331. desc.error = 0;
  1332. do_generic_file_read(filp, ppos, &desc, file_read_actor);
  1333. retval += desc.written;
  1334. if (desc.error) {
  1335. retval = retval ?: desc.error;
  1336. break;
  1337. }
  1338. if (desc.count > 0)
  1339. break;
  1340. }
  1341. out:
  1342. return retval;
  1343. }
  1344. EXPORT_SYMBOL(generic_file_aio_read);
  1345. static ssize_t
  1346. do_readahead(struct address_space *mapping, struct file *filp,
  1347. pgoff_t index, unsigned long nr)
  1348. {
  1349. if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
  1350. return -EINVAL;
  1351. force_page_cache_readahead(mapping, filp, index, nr);
  1352. return 0;
  1353. }
  1354. SYSCALL_DEFINE(readahead)(int fd, loff_t offset, size_t count)
  1355. {
  1356. ssize_t ret;
  1357. struct file *file;
  1358. ret = -EBADF;
  1359. file = fget(fd);
  1360. if (file) {
  1361. if (file->f_mode & FMODE_READ) {
  1362. struct address_space *mapping = file->f_mapping;
  1363. pgoff_t start = offset >> PAGE_CACHE_SHIFT;
  1364. pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  1365. unsigned long len = end - start + 1;
  1366. ret = do_readahead(mapping, file, start, len);
  1367. }
  1368. fput(file);
  1369. }
  1370. return ret;
  1371. }
  1372. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  1373. asmlinkage long SyS_readahead(long fd, loff_t offset, long count)
  1374. {
  1375. return SYSC_readahead((int) fd, offset, (size_t) count);
  1376. }
  1377. SYSCALL_ALIAS(sys_readahead, SyS_readahead);
  1378. #endif
  1379. #ifdef CONFIG_MMU
  1380. /**
  1381. * page_cache_read - adds requested page to the page cache if not already there
  1382. * @file: file to read
  1383. * @offset: page index
  1384. *
  1385. * This adds the requested page to the page cache if it isn't already there,
  1386. * and schedules an I/O to read in its contents from disk.
  1387. */
  1388. static int page_cache_read(struct file *file, pgoff_t offset)
  1389. {
  1390. struct address_space *mapping = file->f_mapping;
  1391. struct page *page;
  1392. int ret;
  1393. do {
  1394. page = page_cache_alloc_cold(mapping);
  1395. if (!page)
  1396. return -ENOMEM;
  1397. ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
  1398. if (ret == 0)
  1399. ret = mapping->a_ops->readpage(file, page);
  1400. else if (ret == -EEXIST)
  1401. ret = 0; /* losing race to add is OK */
  1402. page_cache_release(page);
  1403. } while (ret == AOP_TRUNCATED_PAGE);
  1404. return ret;
  1405. }
  1406. #define MMAP_LOTSAMISS (100)
  1407. /*
  1408. * Synchronous readahead happens when we don't even find
  1409. * a page in the page cache at all.
  1410. */
  1411. static void do_sync_mmap_readahead(struct vm_area_struct *vma,
  1412. struct file_ra_state *ra,
  1413. struct file *file,
  1414. pgoff_t offset)
  1415. {
  1416. unsigned long ra_pages;
  1417. struct address_space *mapping = file->f_mapping;
  1418. /* If we don't want any read-ahead, don't bother */
  1419. if (VM_RandomReadHint(vma))
  1420. return;
  1421. if (!ra->ra_pages)
  1422. return;
  1423. if (VM_SequentialReadHint(vma)) {
  1424. page_cache_sync_readahead(mapping, ra, file, offset,
  1425. ra->ra_pages);
  1426. return;
  1427. }
  1428. /* Avoid banging the cache line if not needed */
  1429. if (ra->mmap_miss < MMAP_LOTSAMISS * 10)
  1430. ra->mmap_miss++;
  1431. /*
  1432. * Do we miss much more than hit in this file? If so,
  1433. * stop bothering with read-ahead. It will only hurt.
  1434. */
  1435. if (ra->mmap_miss > MMAP_LOTSAMISS)
  1436. return;
  1437. /*
  1438. * mmap read-around
  1439. */
  1440. ra_pages = max_sane_readahead(ra->ra_pages);
  1441. ra->start = max_t(long, 0, offset - ra_pages / 2);
  1442. ra->size = ra_pages;
  1443. ra->async_size = ra_pages / 4;
  1444. ra_submit(ra, mapping, file);
  1445. }
  1446. /*
  1447. * Asynchronous readahead happens when we find the page and PG_readahead,
  1448. * so we want to possibly extend the readahead further..
  1449. */
  1450. static void do_async_mmap_readahead(struct vm_area_struct *vma,
  1451. struct file_ra_state *ra,
  1452. struct file *file,
  1453. struct page *page,
  1454. pgoff_t offset)
  1455. {
  1456. struct address_space *mapping = file->f_mapping;
  1457. /* If we don't want any read-ahead, don't bother */
  1458. if (VM_RandomReadHint(vma))
  1459. return;
  1460. if (ra->mmap_miss > 0)
  1461. ra->mmap_miss--;
  1462. if (PageReadahead(page))
  1463. page_cache_async_readahead(mapping, ra, file,
  1464. page, offset, ra->ra_pages);
  1465. }
  1466. /**
  1467. * filemap_fault - read in file data for page fault handling
  1468. * @vma: vma in which the fault was taken
  1469. * @vmf: struct vm_fault containing details of the fault
  1470. *
  1471. * filemap_fault() is invoked via the vma operations vector for a
  1472. * mapped memory region to read in file data during a page fault.
  1473. *
  1474. * The goto's are kind of ugly, but this streamlines the normal case of having
  1475. * it in the page cache, and handles the special cases reasonably without
  1476. * having a lot of duplicated code.
  1477. */
  1478. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1479. {
  1480. int error;
  1481. struct file *file = vma->vm_file;
  1482. struct address_space *mapping = file->f_mapping;
  1483. struct file_ra_state *ra = &file->f_ra;
  1484. struct inode *inode = mapping->host;
  1485. pgoff_t offset = vmf->pgoff;
  1486. struct page *page;
  1487. pgoff_t size;
  1488. int ret = 0;
  1489. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1490. if (offset >= size)
  1491. return VM_FAULT_SIGBUS;
  1492. /*
  1493. * Do we have something in the page cache already?
  1494. */
  1495. page = find_get_page(mapping, offset);
  1496. if (likely(page)) {
  1497. /*
  1498. * We found the page, so try async readahead before
  1499. * waiting for the lock.
  1500. */
  1501. do_async_mmap_readahead(vma, ra, file, page, offset);
  1502. } else {
  1503. /* No page in the page cache at all */
  1504. do_sync_mmap_readahead(vma, ra, file, offset);
  1505. count_vm_event(PGMAJFAULT);
  1506. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1507. ret = VM_FAULT_MAJOR;
  1508. retry_find:
  1509. page = find_get_page(mapping, offset);
  1510. if (!page)
  1511. goto no_cached_page;
  1512. }
  1513. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  1514. page_cache_release(page);
  1515. return ret | VM_FAULT_RETRY;
  1516. }
  1517. /* Did it get truncated? */
  1518. if (unlikely(page->mapping != mapping)) {
  1519. unlock_page(page);
  1520. put_page(page);
  1521. goto retry_find;
  1522. }
  1523. VM_BUG_ON(page->index != offset);
  1524. /*
  1525. * We have a locked page in the page cache, now we need to check
  1526. * that it's up-to-date. If not, it is going to be due to an error.
  1527. */
  1528. if (unlikely(!PageUptodate(page)))
  1529. goto page_not_uptodate;
  1530. /*
  1531. * Found the page and have a reference on it.
  1532. * We must recheck i_size under page lock.
  1533. */
  1534. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1535. if (unlikely(offset >= size)) {
  1536. unlock_page(page);
  1537. page_cache_release(page);
  1538. return VM_FAULT_SIGBUS;
  1539. }
  1540. vmf->page = page;
  1541. return ret | VM_FAULT_LOCKED;
  1542. no_cached_page:
  1543. /*
  1544. * We're only likely to ever get here if MADV_RANDOM is in
  1545. * effect.
  1546. */
  1547. error = page_cache_read(file, offset);
  1548. /*
  1549. * The page we want has now been added to the page cache.
  1550. * In the unlikely event that someone removed it in the
  1551. * meantime, we'll just come back here and read it again.
  1552. */
  1553. if (error >= 0)
  1554. goto retry_find;
  1555. /*
  1556. * An error return from page_cache_read can result if the
  1557. * system is low on memory, or a problem occurs while trying
  1558. * to schedule I/O.
  1559. */
  1560. if (error == -ENOMEM)
  1561. return VM_FAULT_OOM;
  1562. return VM_FAULT_SIGBUS;
  1563. page_not_uptodate:
  1564. /*
  1565. * Umm, take care of errors if the page isn't up-to-date.
  1566. * Try to re-read it _once_. We do this synchronously,
  1567. * because there really aren't any performance issues here
  1568. * and we need to check for errors.
  1569. */
  1570. ClearPageError(page);
  1571. error = mapping->a_ops->readpage(file, page);
  1572. if (!error) {
  1573. wait_on_page_locked(page);
  1574. if (!PageUptodate(page))
  1575. error = -EIO;
  1576. }
  1577. page_cache_release(page);
  1578. if (!error || error == AOP_TRUNCATED_PAGE)
  1579. goto retry_find;
  1580. /* Things didn't work out. Return zero to tell the mm layer so. */
  1581. shrink_readahead_size_eio(file, ra);
  1582. return VM_FAULT_SIGBUS;
  1583. }
  1584. EXPORT_SYMBOL(filemap_fault);
  1585. const struct vm_operations_struct generic_file_vm_ops = {
  1586. .fault = filemap_fault,
  1587. };
  1588. /* This is used for a general mmap of a disk file */
  1589. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1590. {
  1591. struct address_space *mapping = file->f_mapping;
  1592. if (!mapping->a_ops->readpage)
  1593. return -ENOEXEC;
  1594. file_accessed(file);
  1595. vma->vm_ops = &generic_file_vm_ops;
  1596. vma->vm_flags |= VM_CAN_NONLINEAR;
  1597. return 0;
  1598. }
  1599. /*
  1600. * This is for filesystems which do not implement ->writepage.
  1601. */
  1602. int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
  1603. {
  1604. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  1605. return -EINVAL;
  1606. return generic_file_mmap(file, vma);
  1607. }
  1608. #else
  1609. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1610. {
  1611. return -ENOSYS;
  1612. }
  1613. int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
  1614. {
  1615. return -ENOSYS;
  1616. }
  1617. #endif /* CONFIG_MMU */
  1618. EXPORT_SYMBOL(generic_file_mmap);
  1619. EXPORT_SYMBOL(generic_file_readonly_mmap);
  1620. static struct page *__read_cache_page(struct address_space *mapping,
  1621. pgoff_t index,
  1622. int (*filler)(void *, struct page *),
  1623. void *data,
  1624. gfp_t gfp)
  1625. {
  1626. struct page *page;
  1627. int err;
  1628. repeat:
  1629. page = find_get_page(mapping, index);
  1630. if (!page) {
  1631. page = __page_cache_alloc(gfp | __GFP_COLD);
  1632. if (!page)
  1633. return ERR_PTR(-ENOMEM);
  1634. err = add_to_page_cache_lru(page, mapping, index, gfp);
  1635. if (unlikely(err)) {
  1636. page_cache_release(page);
  1637. if (err == -EEXIST)
  1638. goto repeat;
  1639. /* Presumably ENOMEM for radix tree node */
  1640. return ERR_PTR(err);
  1641. }
  1642. err = filler(data, page);
  1643. if (err < 0) {
  1644. page_cache_release(page);
  1645. page = ERR_PTR(err);
  1646. }
  1647. }
  1648. return page;
  1649. }
  1650. static struct page *do_read_cache_page(struct address_space *mapping,
  1651. pgoff_t index,
  1652. int (*filler)(void *, struct page *),
  1653. void *data,
  1654. gfp_t gfp)
  1655. {
  1656. struct page *page;
  1657. int err;
  1658. retry:
  1659. page = __read_cache_page(mapping, index, filler, data, gfp);
  1660. if (IS_ERR(page))
  1661. return page;
  1662. if (PageUptodate(page))
  1663. goto out;
  1664. lock_page(page);
  1665. if (!page->mapping) {
  1666. unlock_page(page);
  1667. page_cache_release(page);
  1668. goto retry;
  1669. }
  1670. if (PageUptodate(page)) {
  1671. unlock_page(page);
  1672. goto out;
  1673. }
  1674. err = filler(data, page);
  1675. if (err < 0) {
  1676. page_cache_release(page);
  1677. return ERR_PTR(err);
  1678. }
  1679. out:
  1680. mark_page_accessed(page);
  1681. return page;
  1682. }
  1683. /**
  1684. * read_cache_page_async - read into page cache, fill it if needed
  1685. * @mapping: the page's address_space
  1686. * @index: the page index
  1687. * @filler: function to perform the read
  1688. * @data: first arg to filler(data, page) function, often left as NULL
  1689. *
  1690. * Same as read_cache_page, but don't wait for page to become unlocked
  1691. * after submitting it to the filler.
  1692. *
  1693. * Read into the page cache. If a page already exists, and PageUptodate() is
  1694. * not set, try to fill the page but don't wait for it to become unlocked.
  1695. *
  1696. * If the page does not get brought uptodate, return -EIO.
  1697. */
  1698. struct page *read_cache_page_async(struct address_space *mapping,
  1699. pgoff_t index,
  1700. int (*filler)(void *, struct page *),
  1701. void *data)
  1702. {
  1703. return do_read_cache_page(mapping, index, filler, data, mapping_gfp_mask(mapping));
  1704. }
  1705. EXPORT_SYMBOL(read_cache_page_async);
  1706. static struct page *wait_on_page_read(struct page *page)
  1707. {
  1708. if (!IS_ERR(page)) {
  1709. wait_on_page_locked(page);
  1710. if (!PageUptodate(page)) {
  1711. page_cache_release(page);
  1712. page = ERR_PTR(-EIO);
  1713. }
  1714. }
  1715. return page;
  1716. }
  1717. /**
  1718. * read_cache_page_gfp - read into page cache, using specified page allocation flags.
  1719. * @mapping: the page's address_space
  1720. * @index: the page index
  1721. * @gfp: the page allocator flags to use if allocating
  1722. *
  1723. * This is the same as "read_mapping_page(mapping, index, NULL)", but with
  1724. * any new page allocations done using the specified allocation flags.
  1725. *
  1726. * If the page does not get brought uptodate, return -EIO.
  1727. */
  1728. struct page *read_cache_page_gfp(struct address_space *mapping,
  1729. pgoff_t index,
  1730. gfp_t gfp)
  1731. {
  1732. filler_t *filler = (filler_t *)mapping->a_ops->readpage;
  1733. return wait_on_page_read(do_read_cache_page(mapping, index, filler, NULL, gfp));
  1734. }
  1735. EXPORT_SYMBOL(read_cache_page_gfp);
  1736. /**
  1737. * read_cache_page - read into page cache, fill it if needed
  1738. * @mapping: the page's address_space
  1739. * @index: the page index
  1740. * @filler: function to perform the read
  1741. * @data: first arg to filler(data, page) function, often left as NULL
  1742. *
  1743. * Read into the page cache. If a page already exists, and PageUptodate() is
  1744. * not set, try to fill the page then wait for it to become unlocked.
  1745. *
  1746. * If the page does not get brought uptodate, return -EIO.
  1747. */
  1748. struct page *read_cache_page(struct address_space *mapping,
  1749. pgoff_t index,
  1750. int (*filler)(void *, struct page *),
  1751. void *data)
  1752. {
  1753. return wait_on_page_read(read_cache_page_async(mapping, index, filler, data));
  1754. }
  1755. EXPORT_SYMBOL(read_cache_page);
  1756. /*
  1757. * The logic we want is
  1758. *
  1759. * if suid or (sgid and xgrp)
  1760. * remove privs
  1761. */
  1762. int should_remove_suid(struct dentry *dentry)
  1763. {
  1764. umode_t mode = dentry->d_inode->i_mode;
  1765. int kill = 0;
  1766. /* suid always must be killed */
  1767. if (unlikely(mode & S_ISUID))
  1768. kill = ATTR_KILL_SUID;
  1769. /*
  1770. * sgid without any exec bits is just a mandatory locking mark; leave
  1771. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1772. */
  1773. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1774. kill |= ATTR_KILL_SGID;
  1775. if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
  1776. return kill;
  1777. return 0;
  1778. }
  1779. EXPORT_SYMBOL(should_remove_suid);
  1780. static int __remove_suid(struct dentry *dentry, int kill)
  1781. {
  1782. struct iattr newattrs;
  1783. newattrs.ia_valid = ATTR_FORCE | kill;
  1784. return notify_change(dentry, &newattrs);
  1785. }
  1786. int file_remove_suid(struct file *file)
  1787. {
  1788. struct dentry *dentry = file->f_path.dentry;
  1789. struct inode *inode = dentry->d_inode;
  1790. int killsuid;
  1791. int killpriv;
  1792. int error = 0;
  1793. /* Fast path for nothing security related */
  1794. if (IS_NOSEC(inode))
  1795. return 0;
  1796. killsuid = should_remove_suid(dentry);
  1797. killpriv = security_inode_need_killpriv(dentry);
  1798. if (killpriv < 0)
  1799. return killpriv;
  1800. if (killpriv)
  1801. error = security_inode_killpriv(dentry);
  1802. if (!error && killsuid)
  1803. error = __remove_suid(dentry, killsuid);
  1804. if (!error && (inode->i_sb->s_flags & MS_NOSEC))
  1805. inode->i_flags |= S_NOSEC;
  1806. return error;
  1807. }
  1808. EXPORT_SYMBOL(file_remove_suid);
  1809. static size_t __iovec_copy_from_user_inatomic(char *vaddr,
  1810. const struct iovec *iov, size_t base, size_t bytes)
  1811. {
  1812. size_t copied = 0, left = 0;
  1813. while (bytes) {
  1814. char __user *buf = iov->iov_base + base;
  1815. int copy = min(bytes, iov->iov_len - base);
  1816. base = 0;
  1817. left = __copy_from_user_inatomic(vaddr, buf, copy);
  1818. copied += copy;
  1819. bytes -= copy;
  1820. vaddr += copy;
  1821. iov++;
  1822. if (unlikely(left))
  1823. break;
  1824. }
  1825. return copied - left;
  1826. }
  1827. /*
  1828. * Copy as much as we can into the page and return the number of bytes which
  1829. * were successfully copied. If a fault is encountered then return the number of
  1830. * bytes which were copied.
  1831. */
  1832. size_t iov_iter_copy_from_user_atomic(struct page *page,
  1833. struct iov_iter *i, unsigned long offset, size_t bytes)
  1834. {
  1835. char *kaddr;
  1836. size_t copied;
  1837. BUG_ON(!in_atomic());
  1838. kaddr = kmap_atomic(page);
  1839. if (likely(i->nr_segs == 1)) {
  1840. int left;
  1841. char __user *buf = i->iov->iov_base + i->iov_offset;
  1842. left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
  1843. copied = bytes - left;
  1844. } else {
  1845. copied = __iovec_copy_from_user_inatomic(kaddr + offset,
  1846. i->iov, i->iov_offset, bytes);
  1847. }
  1848. kunmap_atomic(kaddr);
  1849. return copied;
  1850. }
  1851. EXPORT_SYMBOL(iov_iter_copy_from_user_atomic);
  1852. /*
  1853. * This has the same sideeffects and return value as
  1854. * iov_iter_copy_from_user_atomic().
  1855. * The difference is that it attempts to resolve faults.
  1856. * Page must not be locked.
  1857. */
  1858. size_t iov_iter_copy_from_user(struct page *page,
  1859. struct iov_iter *i, unsigned long offset, size_t bytes)
  1860. {
  1861. char *kaddr;
  1862. size_t copied;
  1863. kaddr = kmap(page);
  1864. if (likely(i->nr_segs == 1)) {
  1865. int left;
  1866. char __user *buf = i->iov->iov_base + i->iov_offset;
  1867. left = __copy_from_user(kaddr + offset, buf, bytes);
  1868. copied = bytes - left;
  1869. } else {
  1870. copied = __iovec_copy_from_user_inatomic(kaddr + offset,
  1871. i->iov, i->iov_offset, bytes);
  1872. }
  1873. kunmap(page);
  1874. return copied;
  1875. }
  1876. EXPORT_SYMBOL(iov_iter_copy_from_user);
  1877. void iov_iter_advance(struct iov_iter *i, size_t bytes)
  1878. {
  1879. BUG_ON(i->count < bytes);
  1880. if (likely(i->nr_segs == 1)) {
  1881. i->iov_offset += bytes;
  1882. i->count -= bytes;
  1883. } else {
  1884. const struct iovec *iov = i->iov;
  1885. size_t base = i->iov_offset;
  1886. unsigned long nr_segs = i->nr_segs;
  1887. /*
  1888. * The !iov->iov_len check ensures we skip over unlikely
  1889. * zero-length segments (without overruning the iovec).
  1890. */
  1891. while (bytes || unlikely(i->count && !iov->iov_len)) {
  1892. int copy;
  1893. copy = min(bytes, iov->iov_len - base);
  1894. BUG_ON(!i->count || i->count < copy);
  1895. i->count -= copy;
  1896. bytes -= copy;
  1897. base += copy;
  1898. if (iov->iov_len == base) {
  1899. iov++;
  1900. nr_segs--;
  1901. base = 0;
  1902. }
  1903. }
  1904. i->iov = iov;
  1905. i->iov_offset = base;
  1906. i->nr_segs = nr_segs;
  1907. }
  1908. }
  1909. EXPORT_SYMBOL(iov_iter_advance);
  1910. /*
  1911. * Fault in the first iovec of the given iov_iter, to a maximum length
  1912. * of bytes. Returns 0 on success, or non-zero if the memory could not be
  1913. * accessed (ie. because it is an invalid address).
  1914. *
  1915. * writev-intensive code may want this to prefault several iovecs -- that
  1916. * would be possible (callers must not rely on the fact that _only_ the
  1917. * first iovec will be faulted with the current implementation).
  1918. */
  1919. int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)
  1920. {
  1921. char __user *buf = i->iov->iov_base + i->iov_offset;
  1922. bytes = min(bytes, i->iov->iov_len - i->iov_offset);
  1923. return fault_in_pages_readable(buf, bytes);
  1924. }
  1925. EXPORT_SYMBOL(iov_iter_fault_in_readable);
  1926. /*
  1927. * Return the count of just the current iov_iter segment.
  1928. */
  1929. size_t iov_iter_single_seg_count(struct iov_iter *i)
  1930. {
  1931. const struct iovec *iov = i->iov;
  1932. if (i->nr_segs == 1)
  1933. return i->count;
  1934. else
  1935. return min(i->count, iov->iov_len - i->iov_offset);
  1936. }
  1937. EXPORT_SYMBOL(iov_iter_single_seg_count);
  1938. /*
  1939. * Performs necessary checks before doing a write
  1940. *
  1941. * Can adjust writing position or amount of bytes to write.
  1942. * Returns appropriate error code that caller should return or
  1943. * zero in case that write should be allowed.
  1944. */
  1945. inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
  1946. {
  1947. struct inode *inode = file->f_mapping->host;
  1948. unsigned long limit = rlimit(RLIMIT_FSIZE);
  1949. if (unlikely(*pos < 0))
  1950. return -EINVAL;
  1951. if (!isblk) {
  1952. /* FIXME: this is for backwards compatibility with 2.4 */
  1953. if (file->f_flags & O_APPEND)
  1954. *pos = i_size_read(inode);
  1955. if (limit != RLIM_INFINITY) {
  1956. if (*pos >= limit) {
  1957. send_sig(SIGXFSZ, current, 0);
  1958. return -EFBIG;
  1959. }
  1960. if (*count > limit - (typeof(limit))*pos) {
  1961. *count = limit - (typeof(limit))*pos;
  1962. }
  1963. }
  1964. }
  1965. /*
  1966. * LFS rule
  1967. */
  1968. if (unlikely(*pos + *count > MAX_NON_LFS &&
  1969. !(file->f_flags & O_LARGEFILE))) {
  1970. if (*pos >= MAX_NON_LFS) {
  1971. return -EFBIG;
  1972. }
  1973. if (*count > MAX_NON_LFS - (unsigned long)*pos) {
  1974. *count = MAX_NON_LFS - (unsigned long)*pos;
  1975. }
  1976. }
  1977. /*
  1978. * Are we about to exceed the fs block limit ?
  1979. *
  1980. * If we have written data it becomes a short write. If we have
  1981. * exceeded without writing data we send a signal and return EFBIG.
  1982. * Linus frestrict idea will clean these up nicely..
  1983. */
  1984. if (likely(!isblk)) {
  1985. if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
  1986. if (*count || *pos > inode->i_sb->s_maxbytes) {
  1987. return -EFBIG;
  1988. }
  1989. /* zero-length writes at ->s_maxbytes are OK */
  1990. }
  1991. if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
  1992. *count = inode->i_sb->s_maxbytes - *pos;
  1993. } else {
  1994. #ifdef CONFIG_BLOCK
  1995. loff_t isize;
  1996. if (bdev_read_only(I_BDEV(inode)))
  1997. return -EPERM;
  1998. isize = i_size_read(inode);
  1999. if (*pos >= isize) {
  2000. if (*count || *pos > isize)
  2001. return -ENOSPC;
  2002. }
  2003. if (*pos + *count > isize)
  2004. *count = isize - *pos;
  2005. #else
  2006. return -EPERM;
  2007. #endif
  2008. }
  2009. return 0;
  2010. }
  2011. EXPORT_SYMBOL(generic_write_checks);
  2012. int pagecache_write_begin(struct file *file, struct address_space *mapping,
  2013. loff_t pos, unsigned len, unsigned flags,
  2014. struct page **pagep, void **fsdata)
  2015. {
  2016. const struct address_space_operations *aops = mapping->a_ops;
  2017. return aops->write_begin(file, mapping, pos, len, flags,
  2018. pagep, fsdata);
  2019. }
  2020. EXPORT_SYMBOL(pagecache_write_begin);
  2021. int pagecache_write_end(struct file *file, struct address_space *mapping,
  2022. loff_t pos, unsigned len, unsigned copied,
  2023. struct page *page, void *fsdata)
  2024. {
  2025. const struct address_space_operations *aops = mapping->a_ops;
  2026. mark_page_accessed(page);
  2027. return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
  2028. }
  2029. EXPORT_SYMBOL(pagecache_write_end);
  2030. ssize_t
  2031. generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  2032. unsigned long *nr_segs, loff_t pos, loff_t *ppos,
  2033. size_t count, size_t ocount)
  2034. {
  2035. struct file *file = iocb->ki_filp;
  2036. struct address_space *mapping = file->f_mapping;
  2037. struct inode *inode = mapping->host;
  2038. ssize_t written;
  2039. size_t write_len;
  2040. pgoff_t end;
  2041. if (count != ocount)
  2042. *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
  2043. write_len = iov_length(iov, *nr_segs);
  2044. end = (pos + write_len - 1) >> PAGE_CACHE_SHIFT;
  2045. written = filemap_write_and_wait_range(mapping, pos, pos + write_len - 1);
  2046. if (written)
  2047. goto out;
  2048. /*
  2049. * After a write we want buffered reads to be sure to go to disk to get
  2050. * the new data. We invalidate clean cached page from the region we're
  2051. * about to write. We do this *before* the write so that we can return
  2052. * without clobbering -EIOCBQUEUED from ->direct_IO().
  2053. */
  2054. if (mapping->nrpages) {
  2055. written = invalidate_inode_pages2_range(mapping,
  2056. pos >> PAGE_CACHE_SHIFT, end);
  2057. /*
  2058. * If a page can not be invalidated, return 0 to fall back
  2059. * to buffered write.
  2060. */
  2061. if (written) {
  2062. if (written == -EBUSY)
  2063. return 0;
  2064. goto out;
  2065. }
  2066. }
  2067. written = mapping->a_ops->direct_IO(WRITE, iocb, iov, pos, *nr_segs);
  2068. /*
  2069. * Finally, try again to invalidate clean pages which might have been
  2070. * cached by non-direct readahead, or faulted in by get_user_pages()
  2071. * if the source of the write was an mmap'ed region of the file
  2072. * we're writing. Either one is a pretty crazy thing to do,
  2073. * so we don't support it 100%. If this invalidation
  2074. * fails, tough, the write still worked...
  2075. */
  2076. if (mapping->nrpages) {
  2077. invalidate_inode_pages2_range(mapping,
  2078. pos >> PAGE_CACHE_SHIFT, end);
  2079. }
  2080. if (written > 0) {
  2081. pos += written;
  2082. if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
  2083. i_size_write(inode, pos);
  2084. mark_inode_dirty(inode);
  2085. }
  2086. *ppos = pos;
  2087. }
  2088. out:
  2089. return written;
  2090. }
  2091. EXPORT_SYMBOL(generic_file_direct_write);
  2092. /*
  2093. * Find or create a page at the given pagecache position. Return the locked
  2094. * page. This function is specifically for buffered writes.
  2095. */
  2096. struct page *grab_cache_page_write_begin(struct address_space *mapping,
  2097. pgoff_t index, unsigned flags)
  2098. {
  2099. int status;
  2100. gfp_t gfp_mask;
  2101. struct page *page;
  2102. gfp_t gfp_notmask = 0;
  2103. gfp_mask = mapping_gfp_mask(mapping);
  2104. if (mapping_cap_account_dirty(mapping))
  2105. gfp_mask |= __GFP_WRITE;
  2106. if (flags & AOP_FLAG_NOFS)
  2107. gfp_notmask = __GFP_FS;
  2108. repeat:
  2109. page = find_lock_page(mapping, index);
  2110. if (page)
  2111. goto found;
  2112. page = __page_cache_alloc(gfp_mask & ~gfp_notmask);
  2113. if (!page)
  2114. return NULL;
  2115. status = add_to_page_cache_lru(page, mapping, index,
  2116. GFP_KERNEL & ~gfp_notmask);
  2117. if (unlikely(status)) {
  2118. page_cache_release(page);
  2119. if (status == -EEXIST)
  2120. goto repeat;
  2121. return NULL;
  2122. }
  2123. found:
  2124. wait_on_page_writeback(page);
  2125. return page;
  2126. }
  2127. EXPORT_SYMBOL(grab_cache_page_write_begin);
  2128. static ssize_t generic_perform_write(struct file *file,
  2129. struct iov_iter *i, loff_t pos)
  2130. {
  2131. struct address_space *mapping = file->f_mapping;
  2132. const struct address_space_operations *a_ops = mapping->a_ops;
  2133. long status = 0;
  2134. ssize_t written = 0;
  2135. unsigned int flags = 0;
  2136. /*
  2137. * Copies from kernel address space cannot fail (NFSD is a big user).
  2138. */
  2139. if (segment_eq(get_fs(), KERNEL_DS))
  2140. flags |= AOP_FLAG_UNINTERRUPTIBLE;
  2141. do {
  2142. struct page *page;
  2143. unsigned long offset; /* Offset into pagecache page */
  2144. unsigned long bytes; /* Bytes to write to page */
  2145. size_t copied; /* Bytes copied from user */
  2146. void *fsdata;
  2147. offset = (pos & (PAGE_CACHE_SIZE - 1));
  2148. bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
  2149. iov_iter_count(i));
  2150. again:
  2151. /*
  2152. * Bring in the user page that we will copy from _first_.
  2153. * Otherwise there's a nasty deadlock on copying from the
  2154. * same page as we're writing to, without it being marked
  2155. * up-to-date.
  2156. *
  2157. * Not only is this an optimisation, but it is also required
  2158. * to check that the address is actually valid, when atomic
  2159. * usercopies are used, below.
  2160. */
  2161. if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
  2162. status = -EFAULT;
  2163. break;
  2164. }
  2165. status = a_ops->write_begin(file, mapping, pos, bytes, flags,
  2166. &page, &fsdata);
  2167. if (unlikely(status))
  2168. break;
  2169. if (mapping_writably_mapped(mapping))
  2170. flush_dcache_page(page);
  2171. pagefault_disable();
  2172. copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
  2173. pagefault_enable();
  2174. flush_dcache_page(page);
  2175. mark_page_accessed(page);
  2176. status = a_ops->write_end(file, mapping, pos, bytes, copied,
  2177. page, fsdata);
  2178. if (unlikely(status < 0))
  2179. break;
  2180. copied = status;
  2181. cond_resched();
  2182. iov_iter_advance(i, copied);
  2183. if (unlikely(copied == 0)) {
  2184. /*
  2185. * If we were unable to copy any data at all, we must
  2186. * fall back to a single segment length write.
  2187. *
  2188. * If we didn't fallback here, we could livelock
  2189. * because not all segments in the iov can be copied at
  2190. * once without a pagefault.
  2191. */
  2192. bytes = min_t(unsigned long, PAGE_CACHE_SIZE - offset,
  2193. iov_iter_single_seg_count(i));
  2194. goto again;
  2195. }
  2196. pos += copied;
  2197. written += copied;
  2198. balance_dirty_pages_ratelimited(mapping);
  2199. if (fatal_signal_pending(current)) {
  2200. status = -EINTR;
  2201. break;
  2202. }
  2203. } while (iov_iter_count(i));
  2204. return written ? written : status;
  2205. }
  2206. ssize_t
  2207. generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
  2208. unsigned long nr_segs, loff_t pos, loff_t *ppos,
  2209. size_t count, ssize_t written)
  2210. {
  2211. struct file *file = iocb->ki_filp;
  2212. ssize_t status;
  2213. struct iov_iter i;
  2214. iov_iter_init(&i, iov, nr_segs, count, written);
  2215. status = generic_perform_write(file, &i, pos);
  2216. if (likely(status >= 0)) {
  2217. written += status;
  2218. *ppos = pos + status;
  2219. }
  2220. return written ? written : status;
  2221. }
  2222. EXPORT_SYMBOL(generic_file_buffered_write);
  2223. /**
  2224. * __generic_file_aio_write - write data to a file
  2225. * @iocb: IO state structure (file, offset, etc.)
  2226. * @iov: vector with data to write
  2227. * @nr_segs: number of segments in the vector
  2228. * @ppos: position where to write
  2229. *
  2230. * This function does all the work needed for actually writing data to a
  2231. * file. It does all basic checks, removes SUID from the file, updates
  2232. * modification times and calls proper subroutines depending on whether we
  2233. * do direct IO or a standard buffered write.
  2234. *
  2235. * It expects i_mutex to be grabbed unless we work on a block device or similar
  2236. * object which does not need locking at all.
  2237. *
  2238. * This function does *not* take care of syncing data in case of O_SYNC write.
  2239. * A caller has to handle it. This is mainly due to the fact that we want to
  2240. * avoid syncing under i_mutex.
  2241. */
  2242. ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  2243. unsigned long nr_segs, loff_t *ppos)
  2244. {
  2245. struct file *file = iocb->ki_filp;
  2246. struct address_space * mapping = file->f_mapping;
  2247. size_t ocount; /* original count */
  2248. size_t count; /* after file limit checks */
  2249. struct inode *inode = mapping->host;
  2250. loff_t pos;
  2251. ssize_t written;
  2252. ssize_t err;
  2253. ocount = 0;
  2254. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  2255. if (err)
  2256. return err;
  2257. count = ocount;
  2258. pos = *ppos;
  2259. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  2260. /* We can write back this queue in page reclaim */
  2261. current->backing_dev_info = mapping->backing_dev_info;
  2262. written = 0;
  2263. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  2264. if (err)
  2265. goto out;
  2266. if (count == 0)
  2267. goto out;
  2268. err = file_remove_suid(file);
  2269. if (err)
  2270. goto out;
  2271. file_update_time(file);
  2272. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  2273. if (unlikely(file->f_flags & O_DIRECT)) {
  2274. loff_t endbyte;
  2275. ssize_t written_buffered;
  2276. written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
  2277. ppos, count, ocount);
  2278. if (written < 0 || written == count)
  2279. goto out;
  2280. /*
  2281. * direct-io write to a hole: fall through to buffered I/O
  2282. * for completing the rest of the request.
  2283. */
  2284. pos += written;
  2285. count -= written;
  2286. written_buffered = generic_file_buffered_write(iocb, iov,
  2287. nr_segs, pos, ppos, count,
  2288. written);
  2289. /*
  2290. * If generic_file_buffered_write() retuned a synchronous error
  2291. * then we want to return the number of bytes which were
  2292. * direct-written, or the error code if that was zero. Note
  2293. * that this differs from normal direct-io semantics, which
  2294. * will return -EFOO even if some bytes were written.
  2295. */
  2296. if (written_buffered < 0) {
  2297. err = written_buffered;
  2298. goto out;
  2299. }
  2300. /*
  2301. * We need to ensure that the page cache pages are written to
  2302. * disk and invalidated to preserve the expected O_DIRECT
  2303. * semantics.
  2304. */
  2305. endbyte = pos + written_buffered - written - 1;
  2306. err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
  2307. if (err == 0) {
  2308. written = written_buffered;
  2309. invalidate_mapping_pages(mapping,
  2310. pos >> PAGE_CACHE_SHIFT,
  2311. endbyte >> PAGE_CACHE_SHIFT);
  2312. } else {
  2313. /*
  2314. * We don't know how much we wrote, so just return
  2315. * the number of bytes which were direct-written
  2316. */
  2317. }
  2318. } else {
  2319. written = generic_file_buffered_write(iocb, iov, nr_segs,
  2320. pos, ppos, count, written);
  2321. }
  2322. out:
  2323. current->backing_dev_info = NULL;
  2324. return written ? written : err;
  2325. }
  2326. EXPORT_SYMBOL(__generic_file_aio_write);
  2327. /**
  2328. * generic_file_aio_write - write data to a file
  2329. * @iocb: IO state structure
  2330. * @iov: vector with data to write
  2331. * @nr_segs: number of segments in the vector
  2332. * @pos: position in file where to write
  2333. *
  2334. * This is a wrapper around __generic_file_aio_write() to be used by most
  2335. * filesystems. It takes care of syncing the file in case of O_SYNC file
  2336. * and acquires i_mutex as needed.
  2337. */
  2338. ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  2339. unsigned long nr_segs, loff_t pos)
  2340. {
  2341. struct file *file = iocb->ki_filp;
  2342. struct inode *inode = file->f_mapping->host;
  2343. struct blk_plug plug;
  2344. ssize_t ret;
  2345. BUG_ON(iocb->ki_pos != pos);
  2346. mutex_lock(&inode->i_mutex);
  2347. blk_start_plug(&plug);
  2348. ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
  2349. mutex_unlock(&inode->i_mutex);
  2350. if (ret > 0 || ret == -EIOCBQUEUED) {
  2351. ssize_t err;
  2352. err = generic_write_sync(file, pos, ret);
  2353. if (err < 0 && ret > 0)
  2354. ret = err;
  2355. }
  2356. blk_finish_plug(&plug);
  2357. return ret;
  2358. }
  2359. EXPORT_SYMBOL(generic_file_aio_write);
  2360. /**
  2361. * try_to_release_page() - release old fs-specific metadata on a page
  2362. *
  2363. * @page: the page which the kernel is trying to free
  2364. * @gfp_mask: memory allocation flags (and I/O mode)
  2365. *
  2366. * The address_space is to try to release any data against the page
  2367. * (presumably at page->private). If the release was successful, return `1'.
  2368. * Otherwise return zero.
  2369. *
  2370. * This may also be called if PG_fscache is set on a page, indicating that the
  2371. * page is known to the local caching routines.
  2372. *
  2373. * The @gfp_mask argument specifies whether I/O may be performed to release
  2374. * this page (__GFP_IO), and whether the call may block (__GFP_WAIT & __GFP_FS).
  2375. *
  2376. */
  2377. int try_to_release_page(struct page *page, gfp_t gfp_mask)
  2378. {
  2379. struct address_space * const mapping = page->mapping;
  2380. BUG_ON(!PageLocked(page));
  2381. if (PageWriteback(page))
  2382. return 0;
  2383. if (mapping && mapping->a_ops->releasepage)
  2384. return mapping->a_ops->releasepage(page, gfp_mask);
  2385. return try_to_free_buffers(page);
  2386. }
  2387. EXPORT_SYMBOL(try_to_release_page);