filemap.c 68 KB

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