filemap.c 70 KB

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