filemap.c 56 KB

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