filemap.c 59 KB

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