filemap.c 70 KB

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