filemap.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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 (various places, mainly in mmap.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 (anon_vma_prepare and various)
  85. *
  86. * ->page_table_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((page_flags_t *)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 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, int 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, int 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, unsigned int 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. unsigned int 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. goto readpage_error;
  749. if (!PageUptodate(page)) {
  750. lock_page(page);
  751. if (!PageUptodate(page)) {
  752. if (page->mapping == NULL) {
  753. /*
  754. * invalidate_inode_pages got it
  755. */
  756. unlock_page(page);
  757. page_cache_release(page);
  758. goto find_page;
  759. }
  760. unlock_page(page);
  761. error = -EIO;
  762. goto readpage_error;
  763. }
  764. unlock_page(page);
  765. }
  766. /*
  767. * i_size must be checked after we have done ->readpage.
  768. *
  769. * Checking i_size after the readpage allows us to calculate
  770. * the correct value for "nr", which means the zero-filled
  771. * part of the page is not copied back to userspace (unless
  772. * another truncate extends the file - this is desired though).
  773. */
  774. isize = i_size_read(inode);
  775. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  776. if (unlikely(!isize || index > end_index)) {
  777. page_cache_release(page);
  778. goto out;
  779. }
  780. /* nr is the maximum number of bytes to copy from this page */
  781. nr = PAGE_CACHE_SIZE;
  782. if (index == end_index) {
  783. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  784. if (nr <= offset) {
  785. page_cache_release(page);
  786. goto out;
  787. }
  788. }
  789. nr = nr - offset;
  790. goto page_ok;
  791. readpage_error:
  792. /* UHHUH! A synchronous read error occurred. Report it */
  793. desc->error = error;
  794. page_cache_release(page);
  795. goto out;
  796. no_cached_page:
  797. /*
  798. * Ok, it wasn't cached, so we need to create a new
  799. * page..
  800. */
  801. if (!cached_page) {
  802. cached_page = page_cache_alloc_cold(mapping);
  803. if (!cached_page) {
  804. desc->error = -ENOMEM;
  805. goto out;
  806. }
  807. }
  808. error = add_to_page_cache_lru(cached_page, mapping,
  809. index, GFP_KERNEL);
  810. if (error) {
  811. if (error == -EEXIST)
  812. goto find_page;
  813. desc->error = error;
  814. goto out;
  815. }
  816. page = cached_page;
  817. cached_page = NULL;
  818. goto readpage;
  819. }
  820. out:
  821. *_ra = ra;
  822. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  823. if (cached_page)
  824. page_cache_release(cached_page);
  825. if (filp)
  826. file_accessed(filp);
  827. }
  828. EXPORT_SYMBOL(do_generic_mapping_read);
  829. int file_read_actor(read_descriptor_t *desc, struct page *page,
  830. unsigned long offset, unsigned long size)
  831. {
  832. char *kaddr;
  833. unsigned long left, count = desc->count;
  834. if (size > count)
  835. size = count;
  836. /*
  837. * Faults on the destination of a read are common, so do it before
  838. * taking the kmap.
  839. */
  840. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  841. kaddr = kmap_atomic(page, KM_USER0);
  842. left = __copy_to_user_inatomic(desc->arg.buf,
  843. kaddr + offset, size);
  844. kunmap_atomic(kaddr, KM_USER0);
  845. if (left == 0)
  846. goto success;
  847. }
  848. /* Do it the slow way */
  849. kaddr = kmap(page);
  850. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  851. kunmap(page);
  852. if (left) {
  853. size -= left;
  854. desc->error = -EFAULT;
  855. }
  856. success:
  857. desc->count = count - size;
  858. desc->written += size;
  859. desc->arg.buf += size;
  860. return size;
  861. }
  862. /*
  863. * This is the "read()" routine for all filesystems
  864. * that can use the page cache directly.
  865. */
  866. ssize_t
  867. __generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  868. unsigned long nr_segs, loff_t *ppos)
  869. {
  870. struct file *filp = iocb->ki_filp;
  871. ssize_t retval;
  872. unsigned long seg;
  873. size_t count;
  874. count = 0;
  875. for (seg = 0; seg < nr_segs; seg++) {
  876. const struct iovec *iv = &iov[seg];
  877. /*
  878. * If any segment has a negative length, or the cumulative
  879. * length ever wraps negative then return -EINVAL.
  880. */
  881. count += iv->iov_len;
  882. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  883. return -EINVAL;
  884. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  885. continue;
  886. if (seg == 0)
  887. return -EFAULT;
  888. nr_segs = seg;
  889. count -= iv->iov_len; /* This segment is no good */
  890. break;
  891. }
  892. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  893. if (filp->f_flags & O_DIRECT) {
  894. loff_t pos = *ppos, size;
  895. struct address_space *mapping;
  896. struct inode *inode;
  897. mapping = filp->f_mapping;
  898. inode = mapping->host;
  899. retval = 0;
  900. if (!count)
  901. goto out; /* skip atime */
  902. size = i_size_read(inode);
  903. if (pos < size) {
  904. retval = generic_file_direct_IO(READ, iocb,
  905. iov, pos, nr_segs);
  906. if (retval > 0 && !is_sync_kiocb(iocb))
  907. retval = -EIOCBQUEUED;
  908. if (retval > 0)
  909. *ppos = pos + retval;
  910. }
  911. file_accessed(filp);
  912. goto out;
  913. }
  914. retval = 0;
  915. if (count) {
  916. for (seg = 0; seg < nr_segs; seg++) {
  917. read_descriptor_t desc;
  918. desc.written = 0;
  919. desc.arg.buf = iov[seg].iov_base;
  920. desc.count = iov[seg].iov_len;
  921. if (desc.count == 0)
  922. continue;
  923. desc.error = 0;
  924. do_generic_file_read(filp,ppos,&desc,file_read_actor);
  925. retval += desc.written;
  926. if (!retval) {
  927. retval = desc.error;
  928. break;
  929. }
  930. }
  931. }
  932. out:
  933. return retval;
  934. }
  935. EXPORT_SYMBOL(__generic_file_aio_read);
  936. ssize_t
  937. generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
  938. {
  939. struct iovec local_iov = { .iov_base = buf, .iov_len = count };
  940. BUG_ON(iocb->ki_pos != pos);
  941. return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
  942. }
  943. EXPORT_SYMBOL(generic_file_aio_read);
  944. ssize_t
  945. generic_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  946. {
  947. struct iovec local_iov = { .iov_base = buf, .iov_len = count };
  948. struct kiocb kiocb;
  949. ssize_t ret;
  950. init_sync_kiocb(&kiocb, filp);
  951. ret = __generic_file_aio_read(&kiocb, &local_iov, 1, ppos);
  952. if (-EIOCBQUEUED == ret)
  953. ret = wait_on_sync_kiocb(&kiocb);
  954. return ret;
  955. }
  956. EXPORT_SYMBOL(generic_file_read);
  957. int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
  958. {
  959. ssize_t written;
  960. unsigned long count = desc->count;
  961. struct file *file = desc->arg.data;
  962. if (size > count)
  963. size = count;
  964. written = file->f_op->sendpage(file, page, offset,
  965. size, &file->f_pos, size<count);
  966. if (written < 0) {
  967. desc->error = written;
  968. written = 0;
  969. }
  970. desc->count = count - written;
  971. desc->written += written;
  972. return written;
  973. }
  974. ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
  975. size_t count, read_actor_t actor, void *target)
  976. {
  977. read_descriptor_t desc;
  978. if (!count)
  979. return 0;
  980. desc.written = 0;
  981. desc.count = count;
  982. desc.arg.data = target;
  983. desc.error = 0;
  984. do_generic_file_read(in_file, ppos, &desc, actor);
  985. if (desc.written)
  986. return desc.written;
  987. return desc.error;
  988. }
  989. EXPORT_SYMBOL(generic_file_sendfile);
  990. static ssize_t
  991. do_readahead(struct address_space *mapping, struct file *filp,
  992. unsigned long index, unsigned long nr)
  993. {
  994. if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
  995. return -EINVAL;
  996. force_page_cache_readahead(mapping, filp, index,
  997. max_sane_readahead(nr));
  998. return 0;
  999. }
  1000. asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
  1001. {
  1002. ssize_t ret;
  1003. struct file *file;
  1004. ret = -EBADF;
  1005. file = fget(fd);
  1006. if (file) {
  1007. if (file->f_mode & FMODE_READ) {
  1008. struct address_space *mapping = file->f_mapping;
  1009. unsigned long start = offset >> PAGE_CACHE_SHIFT;
  1010. unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  1011. unsigned long len = end - start + 1;
  1012. ret = do_readahead(mapping, file, start, len);
  1013. }
  1014. fput(file);
  1015. }
  1016. return ret;
  1017. }
  1018. #ifdef CONFIG_MMU
  1019. /*
  1020. * This adds the requested page to the page cache if it isn't already there,
  1021. * and schedules an I/O to read in its contents from disk.
  1022. */
  1023. static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
  1024. static int fastcall page_cache_read(struct file * file, unsigned long offset)
  1025. {
  1026. struct address_space *mapping = file->f_mapping;
  1027. struct page *page;
  1028. int error;
  1029. page = page_cache_alloc_cold(mapping);
  1030. if (!page)
  1031. return -ENOMEM;
  1032. error = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
  1033. if (!error) {
  1034. error = mapping->a_ops->readpage(file, page);
  1035. page_cache_release(page);
  1036. return error;
  1037. }
  1038. /*
  1039. * We arrive here in the unlikely event that someone
  1040. * raced with us and added our page to the cache first
  1041. * or we are out of memory for radix-tree nodes.
  1042. */
  1043. page_cache_release(page);
  1044. return error == -EEXIST ? 0 : error;
  1045. }
  1046. #define MMAP_LOTSAMISS (100)
  1047. /*
  1048. * filemap_nopage() is invoked via the vma operations vector for a
  1049. * mapped memory region to read in file data during a page fault.
  1050. *
  1051. * The goto's are kind of ugly, but this streamlines the normal case of having
  1052. * it in the page cache, and handles the special cases reasonably without
  1053. * having a lot of duplicated code.
  1054. */
  1055. struct page *filemap_nopage(struct vm_area_struct *area,
  1056. unsigned long address, int *type)
  1057. {
  1058. int error;
  1059. struct file *file = area->vm_file;
  1060. struct address_space *mapping = file->f_mapping;
  1061. struct file_ra_state *ra = &file->f_ra;
  1062. struct inode *inode = mapping->host;
  1063. struct page *page;
  1064. unsigned long size, pgoff;
  1065. int did_readaround = 0, majmin = VM_FAULT_MINOR;
  1066. pgoff = ((address-area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
  1067. retry_all:
  1068. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1069. if (pgoff >= size)
  1070. goto outside_data_content;
  1071. /* If we don't want any read-ahead, don't bother */
  1072. if (VM_RandomReadHint(area))
  1073. goto no_cached_page;
  1074. /*
  1075. * The readahead code wants to be told about each and every page
  1076. * so it can build and shrink its windows appropriately
  1077. *
  1078. * For sequential accesses, we use the generic readahead logic.
  1079. */
  1080. if (VM_SequentialReadHint(area))
  1081. page_cache_readahead(mapping, ra, file, pgoff, 1);
  1082. /*
  1083. * Do we have something in the page cache already?
  1084. */
  1085. retry_find:
  1086. page = find_get_page(mapping, pgoff);
  1087. if (!page) {
  1088. unsigned long ra_pages;
  1089. if (VM_SequentialReadHint(area)) {
  1090. handle_ra_miss(mapping, ra, pgoff);
  1091. goto no_cached_page;
  1092. }
  1093. ra->mmap_miss++;
  1094. /*
  1095. * Do we miss much more than hit in this file? If so,
  1096. * stop bothering with read-ahead. It will only hurt.
  1097. */
  1098. if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
  1099. goto no_cached_page;
  1100. /*
  1101. * To keep the pgmajfault counter straight, we need to
  1102. * check did_readaround, as this is an inner loop.
  1103. */
  1104. if (!did_readaround) {
  1105. majmin = VM_FAULT_MAJOR;
  1106. inc_page_state(pgmajfault);
  1107. }
  1108. did_readaround = 1;
  1109. ra_pages = max_sane_readahead(file->f_ra.ra_pages);
  1110. if (ra_pages) {
  1111. pgoff_t start = 0;
  1112. if (pgoff > ra_pages / 2)
  1113. start = pgoff - ra_pages / 2;
  1114. do_page_cache_readahead(mapping, file, start, ra_pages);
  1115. }
  1116. page = find_get_page(mapping, pgoff);
  1117. if (!page)
  1118. goto no_cached_page;
  1119. }
  1120. if (!did_readaround)
  1121. ra->mmap_hit++;
  1122. /*
  1123. * Ok, found a page in the page cache, now we need to check
  1124. * that it's up-to-date.
  1125. */
  1126. if (!PageUptodate(page))
  1127. goto page_not_uptodate;
  1128. success:
  1129. /*
  1130. * Found the page and have a reference on it.
  1131. */
  1132. mark_page_accessed(page);
  1133. if (type)
  1134. *type = majmin;
  1135. return page;
  1136. outside_data_content:
  1137. /*
  1138. * An external ptracer can access pages that normally aren't
  1139. * accessible..
  1140. */
  1141. if (area->vm_mm == current->mm)
  1142. return NULL;
  1143. /* Fall through to the non-read-ahead case */
  1144. no_cached_page:
  1145. /*
  1146. * We're only likely to ever get here if MADV_RANDOM is in
  1147. * effect.
  1148. */
  1149. error = page_cache_read(file, pgoff);
  1150. grab_swap_token();
  1151. /*
  1152. * The page we want has now been added to the page cache.
  1153. * In the unlikely event that someone removed it in the
  1154. * meantime, we'll just come back here and read it again.
  1155. */
  1156. if (error >= 0)
  1157. goto retry_find;
  1158. /*
  1159. * An error return from page_cache_read can result if the
  1160. * system is low on memory, or a problem occurs while trying
  1161. * to schedule I/O.
  1162. */
  1163. if (error == -ENOMEM)
  1164. return NOPAGE_OOM;
  1165. return NULL;
  1166. page_not_uptodate:
  1167. if (!did_readaround) {
  1168. majmin = VM_FAULT_MAJOR;
  1169. inc_page_state(pgmajfault);
  1170. }
  1171. lock_page(page);
  1172. /* Did it get unhashed while we waited for it? */
  1173. if (!page->mapping) {
  1174. unlock_page(page);
  1175. page_cache_release(page);
  1176. goto retry_all;
  1177. }
  1178. /* Did somebody else get it up-to-date? */
  1179. if (PageUptodate(page)) {
  1180. unlock_page(page);
  1181. goto success;
  1182. }
  1183. if (!mapping->a_ops->readpage(file, page)) {
  1184. wait_on_page_locked(page);
  1185. if (PageUptodate(page))
  1186. goto success;
  1187. }
  1188. /*
  1189. * Umm, take care of errors if the page isn't up-to-date.
  1190. * Try to re-read it _once_. We do this synchronously,
  1191. * because there really aren't any performance issues here
  1192. * and we need to check for errors.
  1193. */
  1194. lock_page(page);
  1195. /* Somebody truncated the page on us? */
  1196. if (!page->mapping) {
  1197. unlock_page(page);
  1198. page_cache_release(page);
  1199. goto retry_all;
  1200. }
  1201. /* Somebody else successfully read it in? */
  1202. if (PageUptodate(page)) {
  1203. unlock_page(page);
  1204. goto success;
  1205. }
  1206. ClearPageError(page);
  1207. if (!mapping->a_ops->readpage(file, page)) {
  1208. wait_on_page_locked(page);
  1209. if (PageUptodate(page))
  1210. goto success;
  1211. }
  1212. /*
  1213. * Things didn't work out. Return zero to tell the
  1214. * mm layer so, possibly freeing the page cache page first.
  1215. */
  1216. page_cache_release(page);
  1217. return NULL;
  1218. }
  1219. EXPORT_SYMBOL(filemap_nopage);
  1220. static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
  1221. int nonblock)
  1222. {
  1223. struct address_space *mapping = file->f_mapping;
  1224. struct page *page;
  1225. int error;
  1226. /*
  1227. * Do we have something in the page cache already?
  1228. */
  1229. retry_find:
  1230. page = find_get_page(mapping, pgoff);
  1231. if (!page) {
  1232. if (nonblock)
  1233. return NULL;
  1234. goto no_cached_page;
  1235. }
  1236. /*
  1237. * Ok, found a page in the page cache, now we need to check
  1238. * that it's up-to-date.
  1239. */
  1240. if (!PageUptodate(page)) {
  1241. if (nonblock) {
  1242. page_cache_release(page);
  1243. return NULL;
  1244. }
  1245. goto page_not_uptodate;
  1246. }
  1247. success:
  1248. /*
  1249. * Found the page and have a reference on it.
  1250. */
  1251. mark_page_accessed(page);
  1252. return page;
  1253. no_cached_page:
  1254. error = page_cache_read(file, pgoff);
  1255. /*
  1256. * The page we want has now been added to the page cache.
  1257. * In the unlikely event that someone removed it in the
  1258. * meantime, we'll just come back here and read it again.
  1259. */
  1260. if (error >= 0)
  1261. goto retry_find;
  1262. /*
  1263. * An error return from page_cache_read can result if the
  1264. * system is low on memory, or a problem occurs while trying
  1265. * to schedule I/O.
  1266. */
  1267. return NULL;
  1268. page_not_uptodate:
  1269. lock_page(page);
  1270. /* Did it get unhashed while we waited for it? */
  1271. if (!page->mapping) {
  1272. unlock_page(page);
  1273. goto err;
  1274. }
  1275. /* Did somebody else get it up-to-date? */
  1276. if (PageUptodate(page)) {
  1277. unlock_page(page);
  1278. goto success;
  1279. }
  1280. if (!mapping->a_ops->readpage(file, page)) {
  1281. wait_on_page_locked(page);
  1282. if (PageUptodate(page))
  1283. goto success;
  1284. }
  1285. /*
  1286. * Umm, take care of errors if the page isn't up-to-date.
  1287. * Try to re-read it _once_. We do this synchronously,
  1288. * because there really aren't any performance issues here
  1289. * and we need to check for errors.
  1290. */
  1291. lock_page(page);
  1292. /* Somebody truncated the page on us? */
  1293. if (!page->mapping) {
  1294. unlock_page(page);
  1295. goto err;
  1296. }
  1297. /* Somebody else successfully read it in? */
  1298. if (PageUptodate(page)) {
  1299. unlock_page(page);
  1300. goto success;
  1301. }
  1302. ClearPageError(page);
  1303. if (!mapping->a_ops->readpage(file, page)) {
  1304. wait_on_page_locked(page);
  1305. if (PageUptodate(page))
  1306. goto success;
  1307. }
  1308. /*
  1309. * Things didn't work out. Return zero to tell the
  1310. * mm layer so, possibly freeing the page cache page first.
  1311. */
  1312. err:
  1313. page_cache_release(page);
  1314. return NULL;
  1315. }
  1316. int filemap_populate(struct vm_area_struct *vma, unsigned long addr,
  1317. unsigned long len, pgprot_t prot, unsigned long pgoff,
  1318. int nonblock)
  1319. {
  1320. struct file *file = vma->vm_file;
  1321. struct address_space *mapping = file->f_mapping;
  1322. struct inode *inode = mapping->host;
  1323. unsigned long size;
  1324. struct mm_struct *mm = vma->vm_mm;
  1325. struct page *page;
  1326. int err;
  1327. if (!nonblock)
  1328. force_page_cache_readahead(mapping, vma->vm_file,
  1329. pgoff, len >> PAGE_CACHE_SHIFT);
  1330. repeat:
  1331. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1332. if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
  1333. return -EINVAL;
  1334. page = filemap_getpage(file, pgoff, nonblock);
  1335. /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
  1336. * done in shmem_populate calling shmem_getpage */
  1337. if (!page && !nonblock)
  1338. return -ENOMEM;
  1339. if (page) {
  1340. err = install_page(mm, vma, addr, page, prot);
  1341. if (err) {
  1342. page_cache_release(page);
  1343. return err;
  1344. }
  1345. } else {
  1346. /* No page was found just because we can't read it in now (being
  1347. * here implies nonblock != 0), but the page may exist, so set
  1348. * the PTE to fault it in later. */
  1349. err = install_file_pte(mm, vma, addr, pgoff, prot);
  1350. if (err)
  1351. return err;
  1352. }
  1353. len -= PAGE_SIZE;
  1354. addr += PAGE_SIZE;
  1355. pgoff++;
  1356. if (len)
  1357. goto repeat;
  1358. return 0;
  1359. }
  1360. struct vm_operations_struct generic_file_vm_ops = {
  1361. .nopage = filemap_nopage,
  1362. .populate = filemap_populate,
  1363. };
  1364. /* This is used for a general mmap of a disk file */
  1365. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1366. {
  1367. struct address_space *mapping = file->f_mapping;
  1368. if (!mapping->a_ops->readpage)
  1369. return -ENOEXEC;
  1370. file_accessed(file);
  1371. vma->vm_ops = &generic_file_vm_ops;
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL(filemap_populate);
  1375. /*
  1376. * This is for filesystems which do not implement ->writepage.
  1377. */
  1378. int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
  1379. {
  1380. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  1381. return -EINVAL;
  1382. return generic_file_mmap(file, vma);
  1383. }
  1384. #else
  1385. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1386. {
  1387. return -ENOSYS;
  1388. }
  1389. int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
  1390. {
  1391. return -ENOSYS;
  1392. }
  1393. #endif /* CONFIG_MMU */
  1394. EXPORT_SYMBOL(generic_file_mmap);
  1395. EXPORT_SYMBOL(generic_file_readonly_mmap);
  1396. static inline struct page *__read_cache_page(struct address_space *mapping,
  1397. unsigned long index,
  1398. int (*filler)(void *,struct page*),
  1399. void *data)
  1400. {
  1401. struct page *page, *cached_page = NULL;
  1402. int err;
  1403. repeat:
  1404. page = find_get_page(mapping, index);
  1405. if (!page) {
  1406. if (!cached_page) {
  1407. cached_page = page_cache_alloc_cold(mapping);
  1408. if (!cached_page)
  1409. return ERR_PTR(-ENOMEM);
  1410. }
  1411. err = add_to_page_cache_lru(cached_page, mapping,
  1412. index, GFP_KERNEL);
  1413. if (err == -EEXIST)
  1414. goto repeat;
  1415. if (err < 0) {
  1416. /* Presumably ENOMEM for radix tree node */
  1417. page_cache_release(cached_page);
  1418. return ERR_PTR(err);
  1419. }
  1420. page = cached_page;
  1421. cached_page = NULL;
  1422. err = filler(data, page);
  1423. if (err < 0) {
  1424. page_cache_release(page);
  1425. page = ERR_PTR(err);
  1426. }
  1427. }
  1428. if (cached_page)
  1429. page_cache_release(cached_page);
  1430. return page;
  1431. }
  1432. /*
  1433. * Read into the page cache. If a page already exists,
  1434. * and PageUptodate() is not set, try to fill the page.
  1435. */
  1436. struct page *read_cache_page(struct address_space *mapping,
  1437. unsigned long index,
  1438. int (*filler)(void *,struct page*),
  1439. void *data)
  1440. {
  1441. struct page *page;
  1442. int err;
  1443. retry:
  1444. page = __read_cache_page(mapping, index, filler, data);
  1445. if (IS_ERR(page))
  1446. goto out;
  1447. mark_page_accessed(page);
  1448. if (PageUptodate(page))
  1449. goto out;
  1450. lock_page(page);
  1451. if (!page->mapping) {
  1452. unlock_page(page);
  1453. page_cache_release(page);
  1454. goto retry;
  1455. }
  1456. if (PageUptodate(page)) {
  1457. unlock_page(page);
  1458. goto out;
  1459. }
  1460. err = filler(data, page);
  1461. if (err < 0) {
  1462. page_cache_release(page);
  1463. page = ERR_PTR(err);
  1464. }
  1465. out:
  1466. return page;
  1467. }
  1468. EXPORT_SYMBOL(read_cache_page);
  1469. /*
  1470. * If the page was newly created, increment its refcount and add it to the
  1471. * caller's lru-buffering pagevec. This function is specifically for
  1472. * generic_file_write().
  1473. */
  1474. static inline struct page *
  1475. __grab_cache_page(struct address_space *mapping, unsigned long index,
  1476. struct page **cached_page, struct pagevec *lru_pvec)
  1477. {
  1478. int err;
  1479. struct page *page;
  1480. repeat:
  1481. page = find_lock_page(mapping, index);
  1482. if (!page) {
  1483. if (!*cached_page) {
  1484. *cached_page = page_cache_alloc(mapping);
  1485. if (!*cached_page)
  1486. return NULL;
  1487. }
  1488. err = add_to_page_cache(*cached_page, mapping,
  1489. index, GFP_KERNEL);
  1490. if (err == -EEXIST)
  1491. goto repeat;
  1492. if (err == 0) {
  1493. page = *cached_page;
  1494. page_cache_get(page);
  1495. if (!pagevec_add(lru_pvec, page))
  1496. __pagevec_lru_add(lru_pvec);
  1497. *cached_page = NULL;
  1498. }
  1499. }
  1500. return page;
  1501. }
  1502. /*
  1503. * The logic we want is
  1504. *
  1505. * if suid or (sgid and xgrp)
  1506. * remove privs
  1507. */
  1508. int remove_suid(struct dentry *dentry)
  1509. {
  1510. mode_t mode = dentry->d_inode->i_mode;
  1511. int kill = 0;
  1512. int result = 0;
  1513. /* suid always must be killed */
  1514. if (unlikely(mode & S_ISUID))
  1515. kill = ATTR_KILL_SUID;
  1516. /*
  1517. * sgid without any exec bits is just a mandatory locking mark; leave
  1518. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1519. */
  1520. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1521. kill |= ATTR_KILL_SGID;
  1522. if (unlikely(kill && !capable(CAP_FSETID))) {
  1523. struct iattr newattrs;
  1524. newattrs.ia_valid = ATTR_FORCE | kill;
  1525. result = notify_change(dentry, &newattrs);
  1526. }
  1527. return result;
  1528. }
  1529. EXPORT_SYMBOL(remove_suid);
  1530. size_t
  1531. __filemap_copy_from_user_iovec(char *vaddr,
  1532. const struct iovec *iov, size_t base, size_t bytes)
  1533. {
  1534. size_t copied = 0, left = 0;
  1535. while (bytes) {
  1536. char __user *buf = iov->iov_base + base;
  1537. int copy = min(bytes, iov->iov_len - base);
  1538. base = 0;
  1539. left = __copy_from_user_inatomic(vaddr, buf, copy);
  1540. copied += copy;
  1541. bytes -= copy;
  1542. vaddr += copy;
  1543. iov++;
  1544. if (unlikely(left)) {
  1545. /* zero the rest of the target like __copy_from_user */
  1546. if (bytes)
  1547. memset(vaddr, 0, bytes);
  1548. break;
  1549. }
  1550. }
  1551. return copied - left;
  1552. }
  1553. /*
  1554. * Performs necessary checks before doing a write
  1555. *
  1556. * Can adjust writing position aor amount of bytes to write.
  1557. * Returns appropriate error code that caller should return or
  1558. * zero in case that write should be allowed.
  1559. */
  1560. inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
  1561. {
  1562. struct inode *inode = file->f_mapping->host;
  1563. unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1564. if (unlikely(*pos < 0))
  1565. return -EINVAL;
  1566. if (!isblk) {
  1567. /* FIXME: this is for backwards compatibility with 2.4 */
  1568. if (file->f_flags & O_APPEND)
  1569. *pos = i_size_read(inode);
  1570. if (limit != RLIM_INFINITY) {
  1571. if (*pos >= limit) {
  1572. send_sig(SIGXFSZ, current, 0);
  1573. return -EFBIG;
  1574. }
  1575. if (*count > limit - (typeof(limit))*pos) {
  1576. *count = limit - (typeof(limit))*pos;
  1577. }
  1578. }
  1579. }
  1580. /*
  1581. * LFS rule
  1582. */
  1583. if (unlikely(*pos + *count > MAX_NON_LFS &&
  1584. !(file->f_flags & O_LARGEFILE))) {
  1585. if (*pos >= MAX_NON_LFS) {
  1586. send_sig(SIGXFSZ, current, 0);
  1587. return -EFBIG;
  1588. }
  1589. if (*count > MAX_NON_LFS - (unsigned long)*pos) {
  1590. *count = MAX_NON_LFS - (unsigned long)*pos;
  1591. }
  1592. }
  1593. /*
  1594. * Are we about to exceed the fs block limit ?
  1595. *
  1596. * If we have written data it becomes a short write. If we have
  1597. * exceeded without writing data we send a signal and return EFBIG.
  1598. * Linus frestrict idea will clean these up nicely..
  1599. */
  1600. if (likely(!isblk)) {
  1601. if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
  1602. if (*count || *pos > inode->i_sb->s_maxbytes) {
  1603. send_sig(SIGXFSZ, current, 0);
  1604. return -EFBIG;
  1605. }
  1606. /* zero-length writes at ->s_maxbytes are OK */
  1607. }
  1608. if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
  1609. *count = inode->i_sb->s_maxbytes - *pos;
  1610. } else {
  1611. loff_t isize;
  1612. if (bdev_read_only(I_BDEV(inode)))
  1613. return -EPERM;
  1614. isize = i_size_read(inode);
  1615. if (*pos >= isize) {
  1616. if (*count || *pos > isize)
  1617. return -ENOSPC;
  1618. }
  1619. if (*pos + *count > isize)
  1620. *count = isize - *pos;
  1621. }
  1622. return 0;
  1623. }
  1624. EXPORT_SYMBOL(generic_write_checks);
  1625. ssize_t
  1626. generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  1627. unsigned long *nr_segs, loff_t pos, loff_t *ppos,
  1628. size_t count, size_t ocount)
  1629. {
  1630. struct file *file = iocb->ki_filp;
  1631. struct address_space *mapping = file->f_mapping;
  1632. struct inode *inode = mapping->host;
  1633. ssize_t written;
  1634. if (count != ocount)
  1635. *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
  1636. written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
  1637. if (written > 0) {
  1638. loff_t end = pos + written;
  1639. if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
  1640. i_size_write(inode, end);
  1641. mark_inode_dirty(inode);
  1642. }
  1643. *ppos = end;
  1644. }
  1645. /*
  1646. * Sync the fs metadata but not the minor inode changes and
  1647. * of course not the data as we did direct DMA for the IO.
  1648. * i_sem is held, which protects generic_osync_inode() from
  1649. * livelocking.
  1650. */
  1651. if (written >= 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1652. int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
  1653. if (err < 0)
  1654. written = err;
  1655. }
  1656. if (written == count && !is_sync_kiocb(iocb))
  1657. written = -EIOCBQUEUED;
  1658. return written;
  1659. }
  1660. EXPORT_SYMBOL(generic_file_direct_write);
  1661. ssize_t
  1662. generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
  1663. unsigned long nr_segs, loff_t pos, loff_t *ppos,
  1664. size_t count, ssize_t written)
  1665. {
  1666. struct file *file = iocb->ki_filp;
  1667. struct address_space * mapping = file->f_mapping;
  1668. struct address_space_operations *a_ops = mapping->a_ops;
  1669. struct inode *inode = mapping->host;
  1670. long status = 0;
  1671. struct page *page;
  1672. struct page *cached_page = NULL;
  1673. size_t bytes;
  1674. struct pagevec lru_pvec;
  1675. const struct iovec *cur_iov = iov; /* current iovec */
  1676. size_t iov_base = 0; /* offset in the current iovec */
  1677. char __user *buf;
  1678. pagevec_init(&lru_pvec, 0);
  1679. /*
  1680. * handle partial DIO write. Adjust cur_iov if needed.
  1681. */
  1682. if (likely(nr_segs == 1))
  1683. buf = iov->iov_base + written;
  1684. else {
  1685. filemap_set_next_iovec(&cur_iov, &iov_base, written);
  1686. buf = cur_iov->iov_base + iov_base;
  1687. }
  1688. do {
  1689. unsigned long index;
  1690. unsigned long offset;
  1691. unsigned long maxlen;
  1692. size_t copied;
  1693. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1694. index = pos >> PAGE_CACHE_SHIFT;
  1695. bytes = PAGE_CACHE_SIZE - offset;
  1696. if (bytes > count)
  1697. bytes = count;
  1698. /*
  1699. * Bring in the user page that we will copy from _first_.
  1700. * Otherwise there's a nasty deadlock on copying from the
  1701. * same page as we're writing to, without it being marked
  1702. * up-to-date.
  1703. */
  1704. maxlen = cur_iov->iov_len - iov_base;
  1705. if (maxlen > bytes)
  1706. maxlen = bytes;
  1707. fault_in_pages_readable(buf, maxlen);
  1708. page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
  1709. if (!page) {
  1710. status = -ENOMEM;
  1711. break;
  1712. }
  1713. status = a_ops->prepare_write(file, page, offset, offset+bytes);
  1714. if (unlikely(status)) {
  1715. loff_t isize = i_size_read(inode);
  1716. /*
  1717. * prepare_write() may have instantiated a few blocks
  1718. * outside i_size. Trim these off again.
  1719. */
  1720. unlock_page(page);
  1721. page_cache_release(page);
  1722. if (pos + bytes > isize)
  1723. vmtruncate(inode, isize);
  1724. break;
  1725. }
  1726. if (likely(nr_segs == 1))
  1727. copied = filemap_copy_from_user(page, offset,
  1728. buf, bytes);
  1729. else
  1730. copied = filemap_copy_from_user_iovec(page, offset,
  1731. cur_iov, iov_base, bytes);
  1732. flush_dcache_page(page);
  1733. status = a_ops->commit_write(file, page, offset, offset+bytes);
  1734. if (likely(copied > 0)) {
  1735. if (!status)
  1736. status = copied;
  1737. if (status >= 0) {
  1738. written += status;
  1739. count -= status;
  1740. pos += status;
  1741. buf += status;
  1742. if (unlikely(nr_segs > 1)) {
  1743. filemap_set_next_iovec(&cur_iov,
  1744. &iov_base, status);
  1745. if (count)
  1746. buf = cur_iov->iov_base +
  1747. iov_base;
  1748. } else {
  1749. iov_base += status;
  1750. }
  1751. }
  1752. }
  1753. if (unlikely(copied != bytes))
  1754. if (status >= 0)
  1755. status = -EFAULT;
  1756. unlock_page(page);
  1757. mark_page_accessed(page);
  1758. page_cache_release(page);
  1759. if (status < 0)
  1760. break;
  1761. balance_dirty_pages_ratelimited(mapping);
  1762. cond_resched();
  1763. } while (count);
  1764. *ppos = pos;
  1765. if (cached_page)
  1766. page_cache_release(cached_page);
  1767. /*
  1768. * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
  1769. */
  1770. if (likely(status >= 0)) {
  1771. if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1772. if (!a_ops->writepage || !is_sync_kiocb(iocb))
  1773. status = generic_osync_inode(inode, mapping,
  1774. OSYNC_METADATA|OSYNC_DATA);
  1775. }
  1776. }
  1777. /*
  1778. * If we get here for O_DIRECT writes then we must have fallen through
  1779. * to buffered writes (block instantiation inside i_size). So we sync
  1780. * the file data here, to try to honour O_DIRECT expectations.
  1781. */
  1782. if (unlikely(file->f_flags & O_DIRECT) && written)
  1783. status = filemap_write_and_wait(mapping);
  1784. pagevec_lru_add(&lru_pvec);
  1785. return written ? written : status;
  1786. }
  1787. EXPORT_SYMBOL(generic_file_buffered_write);
  1788. static ssize_t
  1789. __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
  1790. unsigned long nr_segs, loff_t *ppos)
  1791. {
  1792. struct file *file = iocb->ki_filp;
  1793. struct address_space * mapping = file->f_mapping;
  1794. size_t ocount; /* original count */
  1795. size_t count; /* after file limit checks */
  1796. struct inode *inode = mapping->host;
  1797. unsigned long seg;
  1798. loff_t pos;
  1799. ssize_t written;
  1800. ssize_t err;
  1801. ocount = 0;
  1802. for (seg = 0; seg < nr_segs; seg++) {
  1803. const struct iovec *iv = &iov[seg];
  1804. /*
  1805. * If any segment has a negative length, or the cumulative
  1806. * length ever wraps negative then return -EINVAL.
  1807. */
  1808. ocount += iv->iov_len;
  1809. if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
  1810. return -EINVAL;
  1811. if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
  1812. continue;
  1813. if (seg == 0)
  1814. return -EFAULT;
  1815. nr_segs = seg;
  1816. ocount -= iv->iov_len; /* This segment is no good */
  1817. break;
  1818. }
  1819. count = ocount;
  1820. pos = *ppos;
  1821. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1822. /* We can write back this queue in page reclaim */
  1823. current->backing_dev_info = mapping->backing_dev_info;
  1824. written = 0;
  1825. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1826. if (err)
  1827. goto out;
  1828. if (count == 0)
  1829. goto out;
  1830. err = remove_suid(file->f_dentry);
  1831. if (err)
  1832. goto out;
  1833. inode_update_time(inode, 1);
  1834. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1835. if (unlikely(file->f_flags & O_DIRECT)) {
  1836. written = generic_file_direct_write(iocb, iov,
  1837. &nr_segs, pos, ppos, count, ocount);
  1838. if (written < 0 || written == count)
  1839. goto out;
  1840. /*
  1841. * direct-io write to a hole: fall through to buffered I/O
  1842. * for completing the rest of the request.
  1843. */
  1844. pos += written;
  1845. count -= written;
  1846. }
  1847. written = generic_file_buffered_write(iocb, iov, nr_segs,
  1848. pos, ppos, count, written);
  1849. out:
  1850. current->backing_dev_info = NULL;
  1851. return written ? written : err;
  1852. }
  1853. EXPORT_SYMBOL(generic_file_aio_write_nolock);
  1854. ssize_t
  1855. generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
  1856. unsigned long nr_segs, loff_t *ppos)
  1857. {
  1858. struct file *file = iocb->ki_filp;
  1859. struct address_space *mapping = file->f_mapping;
  1860. struct inode *inode = mapping->host;
  1861. ssize_t ret;
  1862. loff_t pos = *ppos;
  1863. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
  1864. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1865. int err;
  1866. err = sync_page_range_nolock(inode, mapping, pos, ret);
  1867. if (err < 0)
  1868. ret = err;
  1869. }
  1870. return ret;
  1871. }
  1872. static ssize_t
  1873. __generic_file_write_nolock(struct file *file, const struct iovec *iov,
  1874. unsigned long nr_segs, loff_t *ppos)
  1875. {
  1876. struct kiocb kiocb;
  1877. ssize_t ret;
  1878. init_sync_kiocb(&kiocb, file);
  1879. ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
  1880. if (ret == -EIOCBQUEUED)
  1881. ret = wait_on_sync_kiocb(&kiocb);
  1882. return ret;
  1883. }
  1884. ssize_t
  1885. generic_file_write_nolock(struct file *file, const struct iovec *iov,
  1886. unsigned long nr_segs, loff_t *ppos)
  1887. {
  1888. struct kiocb kiocb;
  1889. ssize_t ret;
  1890. init_sync_kiocb(&kiocb, file);
  1891. ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
  1892. if (-EIOCBQUEUED == ret)
  1893. ret = wait_on_sync_kiocb(&kiocb);
  1894. return ret;
  1895. }
  1896. EXPORT_SYMBOL(generic_file_write_nolock);
  1897. ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
  1898. size_t count, loff_t pos)
  1899. {
  1900. struct file *file = iocb->ki_filp;
  1901. struct address_space *mapping = file->f_mapping;
  1902. struct inode *inode = mapping->host;
  1903. ssize_t ret;
  1904. struct iovec local_iov = { .iov_base = (void __user *)buf,
  1905. .iov_len = count };
  1906. BUG_ON(iocb->ki_pos != pos);
  1907. down(&inode->i_sem);
  1908. ret = __generic_file_aio_write_nolock(iocb, &local_iov, 1,
  1909. &iocb->ki_pos);
  1910. up(&inode->i_sem);
  1911. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1912. ssize_t err;
  1913. err = sync_page_range(inode, mapping, pos, ret);
  1914. if (err < 0)
  1915. ret = err;
  1916. }
  1917. return ret;
  1918. }
  1919. EXPORT_SYMBOL(generic_file_aio_write);
  1920. ssize_t generic_file_write(struct file *file, const char __user *buf,
  1921. size_t count, loff_t *ppos)
  1922. {
  1923. struct address_space *mapping = file->f_mapping;
  1924. struct inode *inode = mapping->host;
  1925. ssize_t ret;
  1926. struct iovec local_iov = { .iov_base = (void __user *)buf,
  1927. .iov_len = count };
  1928. down(&inode->i_sem);
  1929. ret = __generic_file_write_nolock(file, &local_iov, 1, ppos);
  1930. up(&inode->i_sem);
  1931. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1932. ssize_t err;
  1933. err = sync_page_range(inode, mapping, *ppos - ret, ret);
  1934. if (err < 0)
  1935. ret = err;
  1936. }
  1937. return ret;
  1938. }
  1939. EXPORT_SYMBOL(generic_file_write);
  1940. ssize_t generic_file_readv(struct file *filp, const struct iovec *iov,
  1941. unsigned long nr_segs, loff_t *ppos)
  1942. {
  1943. struct kiocb kiocb;
  1944. ssize_t ret;
  1945. init_sync_kiocb(&kiocb, filp);
  1946. ret = __generic_file_aio_read(&kiocb, iov, nr_segs, ppos);
  1947. if (-EIOCBQUEUED == ret)
  1948. ret = wait_on_sync_kiocb(&kiocb);
  1949. return ret;
  1950. }
  1951. EXPORT_SYMBOL(generic_file_readv);
  1952. ssize_t generic_file_writev(struct file *file, const struct iovec *iov,
  1953. unsigned long nr_segs, loff_t *ppos)
  1954. {
  1955. struct address_space *mapping = file->f_mapping;
  1956. struct inode *inode = mapping->host;
  1957. ssize_t ret;
  1958. down(&inode->i_sem);
  1959. ret = __generic_file_write_nolock(file, iov, nr_segs, ppos);
  1960. up(&inode->i_sem);
  1961. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1962. int err;
  1963. err = sync_page_range(inode, mapping, *ppos - ret, ret);
  1964. if (err < 0)
  1965. ret = err;
  1966. }
  1967. return ret;
  1968. }
  1969. EXPORT_SYMBOL(generic_file_writev);
  1970. /*
  1971. * Called under i_sem for writes to S_ISREG files. Returns -EIO if something
  1972. * went wrong during pagecache shootdown.
  1973. */
  1974. static ssize_t
  1975. generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  1976. loff_t offset, unsigned long nr_segs)
  1977. {
  1978. struct file *file = iocb->ki_filp;
  1979. struct address_space *mapping = file->f_mapping;
  1980. ssize_t retval;
  1981. size_t write_len = 0;
  1982. /*
  1983. * If it's a write, unmap all mmappings of the file up-front. This
  1984. * will cause any pte dirty bits to be propagated into the pageframes
  1985. * for the subsequent filemap_write_and_wait().
  1986. */
  1987. if (rw == WRITE) {
  1988. write_len = iov_length(iov, nr_segs);
  1989. if (mapping_mapped(mapping))
  1990. unmap_mapping_range(mapping, offset, write_len, 0);
  1991. }
  1992. retval = filemap_write_and_wait(mapping);
  1993. if (retval == 0) {
  1994. retval = mapping->a_ops->direct_IO(rw, iocb, iov,
  1995. offset, nr_segs);
  1996. if (rw == WRITE && mapping->nrpages) {
  1997. pgoff_t end = (offset + write_len - 1)
  1998. >> PAGE_CACHE_SHIFT;
  1999. int err = invalidate_inode_pages2_range(mapping,
  2000. offset >> PAGE_CACHE_SHIFT, end);
  2001. if (err)
  2002. retval = err;
  2003. }
  2004. }
  2005. return retval;
  2006. }