filemap.c 60 KB

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