filemap.c 58 KB

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