filemap.c 63 KB

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