filemap.c 57 KB

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