filemap.c 56 KB

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