filemap.c 58 KB

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