filemap.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  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. * ->i_mutex (generic_file_buffered_write)
  73. * ->mmap_sem (fault_in_pages_readable->do_page_fault)
  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(gfp_t gfp)
  427. {
  428. if (cpuset_do_page_mem_spread()) {
  429. int n = cpuset_mem_spread_node();
  430. return alloc_pages_node(n, gfp, 0);
  431. }
  432. return alloc_pages(gfp, 0);
  433. }
  434. EXPORT_SYMBOL(__page_cache_alloc);
  435. #endif
  436. static int __sleep_on_page_lock(void *word)
  437. {
  438. io_schedule();
  439. return 0;
  440. }
  441. /*
  442. * In order to wait for pages to become available there must be
  443. * waitqueues associated with pages. By using a hash table of
  444. * waitqueues where the bucket discipline is to maintain all
  445. * waiters on the same queue and wake all when any of the pages
  446. * become available, and for the woken contexts to check to be
  447. * sure the appropriate page became available, this saves space
  448. * at a cost of "thundering herd" phenomena during rare hash
  449. * collisions.
  450. */
  451. static wait_queue_head_t *page_waitqueue(struct page *page)
  452. {
  453. const struct zone *zone = page_zone(page);
  454. return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
  455. }
  456. static inline void wake_up_page(struct page *page, int bit)
  457. {
  458. __wake_up_bit(page_waitqueue(page), &page->flags, bit);
  459. }
  460. void fastcall wait_on_page_bit(struct page *page, int bit_nr)
  461. {
  462. DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
  463. if (test_bit(bit_nr, &page->flags))
  464. __wait_on_bit(page_waitqueue(page), &wait, sync_page,
  465. TASK_UNINTERRUPTIBLE);
  466. }
  467. EXPORT_SYMBOL(wait_on_page_bit);
  468. /**
  469. * unlock_page - unlock a locked page
  470. * @page: the page
  471. *
  472. * Unlocks the page and wakes up sleepers in ___wait_on_page_locked().
  473. * Also wakes sleepers in wait_on_page_writeback() because the wakeup
  474. * mechananism between PageLocked pages and PageWriteback pages is shared.
  475. * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
  476. *
  477. * The first mb is necessary to safely close the critical section opened by the
  478. * TestSetPageLocked(), the second mb is necessary to enforce ordering between
  479. * the clear_bit and the read of the waitqueue (to avoid SMP races with a
  480. * parallel wait_on_page_locked()).
  481. */
  482. void fastcall unlock_page(struct page *page)
  483. {
  484. smp_mb__before_clear_bit();
  485. if (!TestClearPageLocked(page))
  486. BUG();
  487. smp_mb__after_clear_bit();
  488. wake_up_page(page, PG_locked);
  489. }
  490. EXPORT_SYMBOL(unlock_page);
  491. /**
  492. * end_page_writeback - end writeback against a page
  493. * @page: the page
  494. */
  495. void end_page_writeback(struct page *page)
  496. {
  497. if (!TestClearPageReclaim(page) || rotate_reclaimable_page(page)) {
  498. if (!test_clear_page_writeback(page))
  499. BUG();
  500. }
  501. smp_mb__after_clear_bit();
  502. wake_up_page(page, PG_writeback);
  503. }
  504. EXPORT_SYMBOL(end_page_writeback);
  505. /**
  506. * __lock_page - get a lock on the page, assuming we need to sleep to get it
  507. * @page: the page to lock
  508. *
  509. * Ugly. Running sync_page() in state TASK_UNINTERRUPTIBLE is scary. If some
  510. * random driver's requestfn sets TASK_RUNNING, we could busywait. However
  511. * chances are that on the second loop, the block layer's plug list is empty,
  512. * so sync_page() will then return in state TASK_UNINTERRUPTIBLE.
  513. */
  514. void fastcall __lock_page(struct page *page)
  515. {
  516. DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
  517. __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,
  518. TASK_UNINTERRUPTIBLE);
  519. }
  520. EXPORT_SYMBOL(__lock_page);
  521. /*
  522. * Variant of lock_page that does not require the caller to hold a reference
  523. * on the page's mapping.
  524. */
  525. void fastcall __lock_page_nosync(struct page *page)
  526. {
  527. DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
  528. __wait_on_bit_lock(page_waitqueue(page), &wait, __sleep_on_page_lock,
  529. TASK_UNINTERRUPTIBLE);
  530. }
  531. /**
  532. * find_get_page - find and get a page reference
  533. * @mapping: the address_space to search
  534. * @offset: the page index
  535. *
  536. * Is there a pagecache struct page at the given (mapping, offset) tuple?
  537. * If yes, increment its refcount and return it; if no, return NULL.
  538. */
  539. struct page * find_get_page(struct address_space *mapping, unsigned long offset)
  540. {
  541. struct page *page;
  542. read_lock_irq(&mapping->tree_lock);
  543. page = radix_tree_lookup(&mapping->page_tree, offset);
  544. if (page)
  545. page_cache_get(page);
  546. read_unlock_irq(&mapping->tree_lock);
  547. return page;
  548. }
  549. EXPORT_SYMBOL(find_get_page);
  550. /**
  551. * find_lock_page - locate, pin and lock a pagecache page
  552. * @mapping: the address_space to search
  553. * @offset: the page index
  554. *
  555. * Locates the desired pagecache page, locks it, increments its reference
  556. * count and returns its address.
  557. *
  558. * Returns zero if the page was not present. find_lock_page() may sleep.
  559. */
  560. struct page *find_lock_page(struct address_space *mapping,
  561. unsigned long offset)
  562. {
  563. struct page *page;
  564. read_lock_irq(&mapping->tree_lock);
  565. repeat:
  566. page = radix_tree_lookup(&mapping->page_tree, offset);
  567. if (page) {
  568. page_cache_get(page);
  569. if (TestSetPageLocked(page)) {
  570. read_unlock_irq(&mapping->tree_lock);
  571. __lock_page(page);
  572. read_lock_irq(&mapping->tree_lock);
  573. /* Has the page been truncated while we slept? */
  574. if (unlikely(page->mapping != mapping ||
  575. page->index != offset)) {
  576. unlock_page(page);
  577. page_cache_release(page);
  578. goto repeat;
  579. }
  580. }
  581. }
  582. read_unlock_irq(&mapping->tree_lock);
  583. return page;
  584. }
  585. EXPORT_SYMBOL(find_lock_page);
  586. /**
  587. * find_or_create_page - locate or add a pagecache page
  588. * @mapping: the page's address_space
  589. * @index: the page's index into the mapping
  590. * @gfp_mask: page allocation mode
  591. *
  592. * Locates a page in the pagecache. If the page is not present, a new page
  593. * is allocated using @gfp_mask and is added to the pagecache and to the VM's
  594. * LRU list. The returned page is locked and has its reference count
  595. * incremented.
  596. *
  597. * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
  598. * allocation!
  599. *
  600. * find_or_create_page() returns the desired page's address, or zero on
  601. * memory exhaustion.
  602. */
  603. struct page *find_or_create_page(struct address_space *mapping,
  604. unsigned long index, gfp_t gfp_mask)
  605. {
  606. struct page *page, *cached_page = NULL;
  607. int err;
  608. repeat:
  609. page = find_lock_page(mapping, index);
  610. if (!page) {
  611. if (!cached_page) {
  612. cached_page = alloc_page(gfp_mask);
  613. if (!cached_page)
  614. return NULL;
  615. }
  616. err = add_to_page_cache_lru(cached_page, mapping,
  617. index, gfp_mask);
  618. if (!err) {
  619. page = cached_page;
  620. cached_page = NULL;
  621. } else if (err == -EEXIST)
  622. goto repeat;
  623. }
  624. if (cached_page)
  625. page_cache_release(cached_page);
  626. return page;
  627. }
  628. EXPORT_SYMBOL(find_or_create_page);
  629. /**
  630. * find_get_pages - gang pagecache lookup
  631. * @mapping: The address_space to search
  632. * @start: The starting page index
  633. * @nr_pages: The maximum number of pages
  634. * @pages: Where the resulting pages are placed
  635. *
  636. * find_get_pages() will search for and return a group of up to
  637. * @nr_pages pages in the mapping. The pages are placed at @pages.
  638. * find_get_pages() takes a reference against the returned pages.
  639. *
  640. * The search returns a group of mapping-contiguous pages with ascending
  641. * indexes. There may be holes in the indices due to not-present pages.
  642. *
  643. * find_get_pages() returns the number of pages which were found.
  644. */
  645. unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
  646. unsigned int nr_pages, struct page **pages)
  647. {
  648. unsigned int i;
  649. unsigned int ret;
  650. read_lock_irq(&mapping->tree_lock);
  651. ret = radix_tree_gang_lookup(&mapping->page_tree,
  652. (void **)pages, start, nr_pages);
  653. for (i = 0; i < ret; i++)
  654. page_cache_get(pages[i]);
  655. read_unlock_irq(&mapping->tree_lock);
  656. return ret;
  657. }
  658. /**
  659. * find_get_pages_contig - gang contiguous pagecache lookup
  660. * @mapping: The address_space to search
  661. * @index: The starting page index
  662. * @nr_pages: The maximum number of pages
  663. * @pages: Where the resulting pages are placed
  664. *
  665. * find_get_pages_contig() works exactly like find_get_pages(), except
  666. * that the returned number of pages are guaranteed to be contiguous.
  667. *
  668. * find_get_pages_contig() returns the number of pages which were found.
  669. */
  670. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
  671. unsigned int nr_pages, struct page **pages)
  672. {
  673. unsigned int i;
  674. unsigned int ret;
  675. read_lock_irq(&mapping->tree_lock);
  676. ret = radix_tree_gang_lookup(&mapping->page_tree,
  677. (void **)pages, index, nr_pages);
  678. for (i = 0; i < ret; i++) {
  679. if (pages[i]->mapping == NULL || pages[i]->index != index)
  680. break;
  681. page_cache_get(pages[i]);
  682. index++;
  683. }
  684. read_unlock_irq(&mapping->tree_lock);
  685. return i;
  686. }
  687. EXPORT_SYMBOL(find_get_pages_contig);
  688. /**
  689. * find_get_pages_tag - find and return pages that match @tag
  690. * @mapping: the address_space to search
  691. * @index: the starting page index
  692. * @tag: the tag index
  693. * @nr_pages: the maximum number of pages
  694. * @pages: where the resulting pages are placed
  695. *
  696. * Like find_get_pages, except we only return pages which are tagged with
  697. * @tag. We update @index to index the next page for the traversal.
  698. */
  699. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  700. int tag, unsigned int nr_pages, struct page **pages)
  701. {
  702. unsigned int i;
  703. unsigned int ret;
  704. read_lock_irq(&mapping->tree_lock);
  705. ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
  706. (void **)pages, *index, nr_pages, tag);
  707. for (i = 0; i < ret; i++)
  708. page_cache_get(pages[i]);
  709. if (ret)
  710. *index = pages[ret - 1]->index + 1;
  711. read_unlock_irq(&mapping->tree_lock);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL(find_get_pages_tag);
  715. /**
  716. * grab_cache_page_nowait - returns locked page at given index in given cache
  717. * @mapping: target address_space
  718. * @index: the page index
  719. *
  720. * Same as grab_cache_page(), but do not wait if the page is unavailable.
  721. * This is intended for speculative data generators, where the data can
  722. * be regenerated if the page couldn't be grabbed. This routine should
  723. * be safe to call while holding the lock for another page.
  724. *
  725. * Clear __GFP_FS when allocating the page to avoid recursion into the fs
  726. * and deadlock against the caller's locked page.
  727. */
  728. struct page *
  729. grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
  730. {
  731. struct page *page = find_get_page(mapping, index);
  732. if (page) {
  733. if (!TestSetPageLocked(page))
  734. return page;
  735. page_cache_release(page);
  736. return NULL;
  737. }
  738. page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
  739. if (page && add_to_page_cache_lru(page, mapping, index, GFP_KERNEL)) {
  740. page_cache_release(page);
  741. page = NULL;
  742. }
  743. return page;
  744. }
  745. EXPORT_SYMBOL(grab_cache_page_nowait);
  746. /*
  747. * CD/DVDs are error prone. When a medium error occurs, the driver may fail
  748. * a _large_ part of the i/o request. Imagine the worst scenario:
  749. *
  750. * ---R__________________________________________B__________
  751. * ^ reading here ^ bad block(assume 4k)
  752. *
  753. * read(R) => miss => readahead(R...B) => media error => frustrating retries
  754. * => failing the whole request => read(R) => read(R+1) =>
  755. * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
  756. * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
  757. * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
  758. *
  759. * It is going insane. Fix it by quickly scaling down the readahead size.
  760. */
  761. static void shrink_readahead_size_eio(struct file *filp,
  762. struct file_ra_state *ra)
  763. {
  764. if (!ra->ra_pages)
  765. return;
  766. ra->ra_pages /= 4;
  767. }
  768. /**
  769. * do_generic_mapping_read - generic file read routine
  770. * @mapping: address_space to be read
  771. * @_ra: file's readahead state
  772. * @filp: the file to read
  773. * @ppos: current file position
  774. * @desc: read_descriptor
  775. * @actor: read method
  776. *
  777. * This is a generic file read routine, and uses the
  778. * mapping->a_ops->readpage() function for the actual low-level stuff.
  779. *
  780. * This is really ugly. But the goto's actually try to clarify some
  781. * of the logic when it comes to error handling etc.
  782. *
  783. * Note the struct file* is only passed for the use of readpage.
  784. * It may be NULL.
  785. */
  786. void do_generic_mapping_read(struct address_space *mapping,
  787. struct file_ra_state *_ra,
  788. struct file *filp,
  789. loff_t *ppos,
  790. read_descriptor_t *desc,
  791. read_actor_t actor)
  792. {
  793. struct inode *inode = mapping->host;
  794. unsigned long index;
  795. unsigned long end_index;
  796. unsigned long offset;
  797. unsigned long last_index;
  798. unsigned long next_index;
  799. unsigned long prev_index;
  800. unsigned int prev_offset;
  801. loff_t isize;
  802. struct page *cached_page;
  803. int error;
  804. struct file_ra_state ra = *_ra;
  805. cached_page = NULL;
  806. index = *ppos >> PAGE_CACHE_SHIFT;
  807. next_index = index;
  808. prev_index = ra.prev_index;
  809. prev_offset = ra.prev_offset;
  810. last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  811. offset = *ppos & ~PAGE_CACHE_MASK;
  812. isize = i_size_read(inode);
  813. if (!isize)
  814. goto out;
  815. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  816. for (;;) {
  817. struct page *page;
  818. unsigned long nr, ret;
  819. /* nr is the maximum number of bytes to copy from this page */
  820. nr = PAGE_CACHE_SIZE;
  821. if (index >= end_index) {
  822. if (index > end_index)
  823. goto out;
  824. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  825. if (nr <= offset) {
  826. goto out;
  827. }
  828. }
  829. nr = nr - offset;
  830. cond_resched();
  831. if (index == next_index)
  832. next_index = page_cache_readahead(mapping, &ra, filp,
  833. index, last_index - index);
  834. find_page:
  835. page = find_get_page(mapping, index);
  836. if (unlikely(page == NULL)) {
  837. handle_ra_miss(mapping, &ra, index);
  838. goto no_cached_page;
  839. }
  840. if (!PageUptodate(page))
  841. goto page_not_up_to_date;
  842. page_ok:
  843. /* If users can be writing to this page using arbitrary
  844. * virtual addresses, take care about potential aliasing
  845. * before reading the page on the kernel side.
  846. */
  847. if (mapping_writably_mapped(mapping))
  848. flush_dcache_page(page);
  849. /*
  850. * When a sequential read accesses a page several times,
  851. * only mark it as accessed the first time.
  852. */
  853. if (prev_index != index || offset != prev_offset)
  854. mark_page_accessed(page);
  855. prev_index = index;
  856. /*
  857. * Ok, we have the page, and it's up-to-date, so
  858. * now we can copy it to user space...
  859. *
  860. * The actor routine returns how many bytes were actually used..
  861. * NOTE! This may not be the same as how much of a user buffer
  862. * we filled up (we may be padding etc), so we can only update
  863. * "pos" here (the actor routine has to update the user buffer
  864. * pointers and the remaining count).
  865. */
  866. ret = actor(desc, page, offset, nr);
  867. offset += ret;
  868. index += offset >> PAGE_CACHE_SHIFT;
  869. offset &= ~PAGE_CACHE_MASK;
  870. prev_offset = offset;
  871. ra.prev_offset = offset;
  872. page_cache_release(page);
  873. if (ret == nr && desc->count)
  874. continue;
  875. goto out;
  876. page_not_up_to_date:
  877. /* Get exclusive access to the page ... */
  878. lock_page(page);
  879. /* Did it get truncated before we got the lock? */
  880. if (!page->mapping) {
  881. unlock_page(page);
  882. page_cache_release(page);
  883. continue;
  884. }
  885. /* Did somebody else fill it already? */
  886. if (PageUptodate(page)) {
  887. unlock_page(page);
  888. goto page_ok;
  889. }
  890. readpage:
  891. /* Start the actual read. The read will unlock the page. */
  892. error = mapping->a_ops->readpage(filp, page);
  893. if (unlikely(error)) {
  894. if (error == AOP_TRUNCATED_PAGE) {
  895. page_cache_release(page);
  896. goto find_page;
  897. }
  898. goto readpage_error;
  899. }
  900. if (!PageUptodate(page)) {
  901. lock_page(page);
  902. if (!PageUptodate(page)) {
  903. if (page->mapping == NULL) {
  904. /*
  905. * invalidate_inode_pages got it
  906. */
  907. unlock_page(page);
  908. page_cache_release(page);
  909. goto find_page;
  910. }
  911. unlock_page(page);
  912. error = -EIO;
  913. shrink_readahead_size_eio(filp, &ra);
  914. goto readpage_error;
  915. }
  916. unlock_page(page);
  917. }
  918. /*
  919. * i_size must be checked after we have done ->readpage.
  920. *
  921. * Checking i_size after the readpage allows us to calculate
  922. * the correct value for "nr", which means the zero-filled
  923. * part of the page is not copied back to userspace (unless
  924. * another truncate extends the file - this is desired though).
  925. */
  926. isize = i_size_read(inode);
  927. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  928. if (unlikely(!isize || index > end_index)) {
  929. page_cache_release(page);
  930. goto out;
  931. }
  932. /* nr is the maximum number of bytes to copy from this page */
  933. nr = PAGE_CACHE_SIZE;
  934. if (index == end_index) {
  935. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  936. if (nr <= offset) {
  937. page_cache_release(page);
  938. goto out;
  939. }
  940. }
  941. nr = nr - offset;
  942. goto page_ok;
  943. readpage_error:
  944. /* UHHUH! A synchronous read error occurred. Report it */
  945. desc->error = error;
  946. page_cache_release(page);
  947. goto out;
  948. no_cached_page:
  949. /*
  950. * Ok, it wasn't cached, so we need to create a new
  951. * page..
  952. */
  953. if (!cached_page) {
  954. cached_page = page_cache_alloc_cold(mapping);
  955. if (!cached_page) {
  956. desc->error = -ENOMEM;
  957. goto out;
  958. }
  959. }
  960. error = add_to_page_cache_lru(cached_page, mapping,
  961. index, GFP_KERNEL);
  962. if (error) {
  963. if (error == -EEXIST)
  964. goto find_page;
  965. desc->error = error;
  966. goto out;
  967. }
  968. page = cached_page;
  969. cached_page = NULL;
  970. goto readpage;
  971. }
  972. out:
  973. *_ra = ra;
  974. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  975. if (cached_page)
  976. page_cache_release(cached_page);
  977. if (filp)
  978. file_accessed(filp);
  979. }
  980. EXPORT_SYMBOL(do_generic_mapping_read);
  981. int file_read_actor(read_descriptor_t *desc, struct page *page,
  982. unsigned long offset, unsigned long size)
  983. {
  984. char *kaddr;
  985. unsigned long left, count = desc->count;
  986. if (size > count)
  987. size = count;
  988. /*
  989. * Faults on the destination of a read are common, so do it before
  990. * taking the kmap.
  991. */
  992. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  993. kaddr = kmap_atomic(page, KM_USER0);
  994. left = __copy_to_user_inatomic(desc->arg.buf,
  995. kaddr + offset, size);
  996. kunmap_atomic(kaddr, KM_USER0);
  997. if (left == 0)
  998. goto success;
  999. }
  1000. /* Do it the slow way */
  1001. kaddr = kmap(page);
  1002. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1003. kunmap(page);
  1004. if (left) {
  1005. size -= left;
  1006. desc->error = -EFAULT;
  1007. }
  1008. success:
  1009. desc->count = count - size;
  1010. desc->written += size;
  1011. desc->arg.buf += size;
  1012. return size;
  1013. }
  1014. /*
  1015. * Performs necessary checks before doing a write
  1016. * @iov: io vector request
  1017. * @nr_segs: number of segments in the iovec
  1018. * @count: number of bytes to write
  1019. * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
  1020. *
  1021. * Adjust number of segments and amount of bytes to write (nr_segs should be
  1022. * properly initialized first). Returns appropriate error code that caller
  1023. * should return or zero in case that write should be allowed.
  1024. */
  1025. int generic_segment_checks(const struct iovec *iov,
  1026. unsigned long *nr_segs, size_t *count, int access_flags)
  1027. {
  1028. unsigned long seg;
  1029. size_t cnt = 0;
  1030. for (seg = 0; seg < *nr_segs; seg++) {
  1031. const struct iovec *iv = &iov[seg];
  1032. /*
  1033. * If any segment has a negative length, or the cumulative
  1034. * length ever wraps negative then return -EINVAL.
  1035. */
  1036. cnt += iv->iov_len;
  1037. if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
  1038. return -EINVAL;
  1039. if (access_ok(access_flags, iv->iov_base, iv->iov_len))
  1040. continue;
  1041. if (seg == 0)
  1042. return -EFAULT;
  1043. *nr_segs = seg;
  1044. cnt -= iv->iov_len; /* This segment is no good */
  1045. break;
  1046. }
  1047. *count = cnt;
  1048. return 0;
  1049. }
  1050. EXPORT_SYMBOL(generic_segment_checks);
  1051. /**
  1052. * generic_file_aio_read - generic filesystem read routine
  1053. * @iocb: kernel I/O control block
  1054. * @iov: io vector request
  1055. * @nr_segs: number of segments in the iovec
  1056. * @pos: current file position
  1057. *
  1058. * This is the "read()" routine for all filesystems
  1059. * that can use the page cache directly.
  1060. */
  1061. ssize_t
  1062. generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1063. unsigned long nr_segs, loff_t pos)
  1064. {
  1065. struct file *filp = iocb->ki_filp;
  1066. ssize_t retval;
  1067. unsigned long seg;
  1068. size_t count;
  1069. loff_t *ppos = &iocb->ki_pos;
  1070. count = 0;
  1071. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1072. if (retval)
  1073. return retval;
  1074. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1075. if (filp->f_flags & O_DIRECT) {
  1076. loff_t size;
  1077. struct address_space *mapping;
  1078. struct inode *inode;
  1079. mapping = filp->f_mapping;
  1080. inode = mapping->host;
  1081. retval = 0;
  1082. if (!count)
  1083. goto out; /* skip atime */
  1084. size = i_size_read(inode);
  1085. if (pos < size) {
  1086. retval = generic_file_direct_IO(READ, iocb,
  1087. iov, pos, nr_segs);
  1088. if (retval > 0)
  1089. *ppos = pos + retval;
  1090. }
  1091. if (likely(retval != 0)) {
  1092. file_accessed(filp);
  1093. goto out;
  1094. }
  1095. }
  1096. retval = 0;
  1097. if (count) {
  1098. for (seg = 0; seg < nr_segs; seg++) {
  1099. read_descriptor_t desc;
  1100. desc.written = 0;
  1101. desc.arg.buf = iov[seg].iov_base;
  1102. desc.count = iov[seg].iov_len;
  1103. if (desc.count == 0)
  1104. continue;
  1105. desc.error = 0;
  1106. do_generic_file_read(filp,ppos,&desc,file_read_actor);
  1107. retval += desc.written;
  1108. if (desc.error) {
  1109. retval = retval ?: desc.error;
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. out:
  1115. return retval;
  1116. }
  1117. EXPORT_SYMBOL(generic_file_aio_read);
  1118. int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
  1119. {
  1120. ssize_t written;
  1121. unsigned long count = desc->count;
  1122. struct file *file = desc->arg.data;
  1123. if (size > count)
  1124. size = count;
  1125. written = file->f_op->sendpage(file, page, offset,
  1126. size, &file->f_pos, size<count);
  1127. if (written < 0) {
  1128. desc->error = written;
  1129. written = 0;
  1130. }
  1131. desc->count = count - written;
  1132. desc->written += written;
  1133. return written;
  1134. }
  1135. ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
  1136. size_t count, read_actor_t actor, void *target)
  1137. {
  1138. read_descriptor_t desc;
  1139. if (!count)
  1140. return 0;
  1141. desc.written = 0;
  1142. desc.count = count;
  1143. desc.arg.data = target;
  1144. desc.error = 0;
  1145. do_generic_file_read(in_file, ppos, &desc, actor);
  1146. if (desc.written)
  1147. return desc.written;
  1148. return desc.error;
  1149. }
  1150. EXPORT_SYMBOL(generic_file_sendfile);
  1151. static ssize_t
  1152. do_readahead(struct address_space *mapping, struct file *filp,
  1153. unsigned long index, unsigned long nr)
  1154. {
  1155. if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
  1156. return -EINVAL;
  1157. force_page_cache_readahead(mapping, filp, index,
  1158. max_sane_readahead(nr));
  1159. return 0;
  1160. }
  1161. asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
  1162. {
  1163. ssize_t ret;
  1164. struct file *file;
  1165. ret = -EBADF;
  1166. file = fget(fd);
  1167. if (file) {
  1168. if (file->f_mode & FMODE_READ) {
  1169. struct address_space *mapping = file->f_mapping;
  1170. unsigned long start = offset >> PAGE_CACHE_SHIFT;
  1171. unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  1172. unsigned long len = end - start + 1;
  1173. ret = do_readahead(mapping, file, start, len);
  1174. }
  1175. fput(file);
  1176. }
  1177. return ret;
  1178. }
  1179. #ifdef CONFIG_MMU
  1180. static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
  1181. /**
  1182. * page_cache_read - adds requested page to the page cache if not already there
  1183. * @file: file to read
  1184. * @offset: page index
  1185. *
  1186. * This adds the requested page to the page cache if it isn't already there,
  1187. * and schedules an I/O to read in its contents from disk.
  1188. */
  1189. static int fastcall page_cache_read(struct file * file, unsigned long offset)
  1190. {
  1191. struct address_space *mapping = file->f_mapping;
  1192. struct page *page;
  1193. int ret;
  1194. do {
  1195. page = page_cache_alloc_cold(mapping);
  1196. if (!page)
  1197. return -ENOMEM;
  1198. ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
  1199. if (ret == 0)
  1200. ret = mapping->a_ops->readpage(file, page);
  1201. else if (ret == -EEXIST)
  1202. ret = 0; /* losing race to add is OK */
  1203. page_cache_release(page);
  1204. } while (ret == AOP_TRUNCATED_PAGE);
  1205. return ret;
  1206. }
  1207. #define MMAP_LOTSAMISS (100)
  1208. /**
  1209. * filemap_nopage - read in file data for page fault handling
  1210. * @area: the applicable vm_area
  1211. * @address: target address to read in
  1212. * @type: returned with VM_FAULT_{MINOR,MAJOR} if not %NULL
  1213. *
  1214. * filemap_nopage() is invoked via the vma operations vector for a
  1215. * mapped memory region to read in file data during a page fault.
  1216. *
  1217. * The goto's are kind of ugly, but this streamlines the normal case of having
  1218. * it in the page cache, and handles the special cases reasonably without
  1219. * having a lot of duplicated code.
  1220. */
  1221. struct page *filemap_nopage(struct vm_area_struct *area,
  1222. unsigned long address, int *type)
  1223. {
  1224. int error;
  1225. struct file *file = area->vm_file;
  1226. struct address_space *mapping = file->f_mapping;
  1227. struct file_ra_state *ra = &file->f_ra;
  1228. struct inode *inode = mapping->host;
  1229. struct page *page;
  1230. unsigned long size, pgoff;
  1231. int did_readaround = 0, majmin = VM_FAULT_MINOR;
  1232. pgoff = ((address-area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
  1233. retry_all:
  1234. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1235. if (pgoff >= size)
  1236. goto outside_data_content;
  1237. /* If we don't want any read-ahead, don't bother */
  1238. if (VM_RandomReadHint(area))
  1239. goto no_cached_page;
  1240. /*
  1241. * The readahead code wants to be told about each and every page
  1242. * so it can build and shrink its windows appropriately
  1243. *
  1244. * For sequential accesses, we use the generic readahead logic.
  1245. */
  1246. if (VM_SequentialReadHint(area))
  1247. page_cache_readahead(mapping, ra, file, pgoff, 1);
  1248. /*
  1249. * Do we have something in the page cache already?
  1250. */
  1251. retry_find:
  1252. page = find_get_page(mapping, pgoff);
  1253. if (!page) {
  1254. unsigned long ra_pages;
  1255. if (VM_SequentialReadHint(area)) {
  1256. handle_ra_miss(mapping, ra, pgoff);
  1257. goto no_cached_page;
  1258. }
  1259. ra->mmap_miss++;
  1260. /*
  1261. * Do we miss much more than hit in this file? If so,
  1262. * stop bothering with read-ahead. It will only hurt.
  1263. */
  1264. if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
  1265. goto no_cached_page;
  1266. /*
  1267. * To keep the pgmajfault counter straight, we need to
  1268. * check did_readaround, as this is an inner loop.
  1269. */
  1270. if (!did_readaround) {
  1271. majmin = VM_FAULT_MAJOR;
  1272. count_vm_event(PGMAJFAULT);
  1273. }
  1274. did_readaround = 1;
  1275. ra_pages = max_sane_readahead(file->f_ra.ra_pages);
  1276. if (ra_pages) {
  1277. pgoff_t start = 0;
  1278. if (pgoff > ra_pages / 2)
  1279. start = pgoff - ra_pages / 2;
  1280. do_page_cache_readahead(mapping, file, start, ra_pages);
  1281. }
  1282. page = find_get_page(mapping, pgoff);
  1283. if (!page)
  1284. goto no_cached_page;
  1285. }
  1286. if (!did_readaround)
  1287. ra->mmap_hit++;
  1288. /*
  1289. * Ok, found a page in the page cache, now we need to check
  1290. * that it's up-to-date.
  1291. */
  1292. if (!PageUptodate(page))
  1293. goto page_not_uptodate;
  1294. success:
  1295. /*
  1296. * Found the page and have a reference on it.
  1297. */
  1298. mark_page_accessed(page);
  1299. if (type)
  1300. *type = majmin;
  1301. return page;
  1302. outside_data_content:
  1303. /*
  1304. * An external ptracer can access pages that normally aren't
  1305. * accessible..
  1306. */
  1307. if (area->vm_mm == current->mm)
  1308. return NOPAGE_SIGBUS;
  1309. /* Fall through to the non-read-ahead case */
  1310. no_cached_page:
  1311. /*
  1312. * We're only likely to ever get here if MADV_RANDOM is in
  1313. * effect.
  1314. */
  1315. error = page_cache_read(file, pgoff);
  1316. /*
  1317. * The page we want has now been added to the page cache.
  1318. * In the unlikely event that someone removed it in the
  1319. * meantime, we'll just come back here and read it again.
  1320. */
  1321. if (error >= 0)
  1322. goto retry_find;
  1323. /*
  1324. * An error return from page_cache_read can result if the
  1325. * system is low on memory, or a problem occurs while trying
  1326. * to schedule I/O.
  1327. */
  1328. if (error == -ENOMEM)
  1329. return NOPAGE_OOM;
  1330. return NOPAGE_SIGBUS;
  1331. page_not_uptodate:
  1332. if (!did_readaround) {
  1333. majmin = VM_FAULT_MAJOR;
  1334. count_vm_event(PGMAJFAULT);
  1335. }
  1336. /*
  1337. * Umm, take care of errors if the page isn't up-to-date.
  1338. * Try to re-read it _once_. We do this synchronously,
  1339. * because there really aren't any performance issues here
  1340. * and we need to check for errors.
  1341. */
  1342. lock_page(page);
  1343. /* Somebody truncated the page on us? */
  1344. if (!page->mapping) {
  1345. unlock_page(page);
  1346. page_cache_release(page);
  1347. goto retry_all;
  1348. }
  1349. /* Somebody else successfully read it in? */
  1350. if (PageUptodate(page)) {
  1351. unlock_page(page);
  1352. goto success;
  1353. }
  1354. ClearPageError(page);
  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. * Things didn't work out. Return zero to tell the
  1366. * mm layer so, possibly freeing the page cache page first.
  1367. */
  1368. shrink_readahead_size_eio(file, ra);
  1369. page_cache_release(page);
  1370. return NOPAGE_SIGBUS;
  1371. }
  1372. EXPORT_SYMBOL(filemap_nopage);
  1373. static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
  1374. int nonblock)
  1375. {
  1376. struct address_space *mapping = file->f_mapping;
  1377. struct page *page;
  1378. int error;
  1379. /*
  1380. * Do we have something in the page cache already?
  1381. */
  1382. retry_find:
  1383. page = find_get_page(mapping, pgoff);
  1384. if (!page) {
  1385. if (nonblock)
  1386. return NULL;
  1387. goto no_cached_page;
  1388. }
  1389. /*
  1390. * Ok, found a page in the page cache, now we need to check
  1391. * that it's up-to-date.
  1392. */
  1393. if (!PageUptodate(page)) {
  1394. if (nonblock) {
  1395. page_cache_release(page);
  1396. return NULL;
  1397. }
  1398. goto page_not_uptodate;
  1399. }
  1400. success:
  1401. /*
  1402. * Found the page and have a reference on it.
  1403. */
  1404. mark_page_accessed(page);
  1405. return page;
  1406. no_cached_page:
  1407. error = page_cache_read(file, pgoff);
  1408. /*
  1409. * The page we want has now been added to the page cache.
  1410. * In the unlikely event that someone removed it in the
  1411. * meantime, we'll just come back here and read it again.
  1412. */
  1413. if (error >= 0)
  1414. goto retry_find;
  1415. /*
  1416. * An error return from page_cache_read can result if the
  1417. * system is low on memory, or a problem occurs while trying
  1418. * to schedule I/O.
  1419. */
  1420. return NULL;
  1421. page_not_uptodate:
  1422. lock_page(page);
  1423. /* Did it get truncated while we waited for it? */
  1424. if (!page->mapping) {
  1425. unlock_page(page);
  1426. goto err;
  1427. }
  1428. /* Did somebody else get it up-to-date? */
  1429. if (PageUptodate(page)) {
  1430. unlock_page(page);
  1431. goto success;
  1432. }
  1433. error = mapping->a_ops->readpage(file, page);
  1434. if (!error) {
  1435. wait_on_page_locked(page);
  1436. if (PageUptodate(page))
  1437. goto success;
  1438. } else if (error == AOP_TRUNCATED_PAGE) {
  1439. page_cache_release(page);
  1440. goto retry_find;
  1441. }
  1442. /*
  1443. * Umm, take care of errors if the page isn't up-to-date.
  1444. * Try to re-read it _once_. We do this synchronously,
  1445. * because there really aren't any performance issues here
  1446. * and we need to check for errors.
  1447. */
  1448. lock_page(page);
  1449. /* Somebody truncated the page on us? */
  1450. if (!page->mapping) {
  1451. unlock_page(page);
  1452. goto err;
  1453. }
  1454. /* Somebody else successfully read it in? */
  1455. if (PageUptodate(page)) {
  1456. unlock_page(page);
  1457. goto success;
  1458. }
  1459. ClearPageError(page);
  1460. error = mapping->a_ops->readpage(file, page);
  1461. if (!error) {
  1462. wait_on_page_locked(page);
  1463. if (PageUptodate(page))
  1464. goto success;
  1465. } else if (error == AOP_TRUNCATED_PAGE) {
  1466. page_cache_release(page);
  1467. goto retry_find;
  1468. }
  1469. /*
  1470. * Things didn't work out. Return zero to tell the
  1471. * mm layer so, possibly freeing the page cache page first.
  1472. */
  1473. err:
  1474. page_cache_release(page);
  1475. return NULL;
  1476. }
  1477. int filemap_populate(struct vm_area_struct *vma, unsigned long addr,
  1478. unsigned long len, pgprot_t prot, unsigned long pgoff,
  1479. int nonblock)
  1480. {
  1481. struct file *file = vma->vm_file;
  1482. struct address_space *mapping = file->f_mapping;
  1483. struct inode *inode = mapping->host;
  1484. unsigned long size;
  1485. struct mm_struct *mm = vma->vm_mm;
  1486. struct page *page;
  1487. int err;
  1488. if (!nonblock)
  1489. force_page_cache_readahead(mapping, vma->vm_file,
  1490. pgoff, len >> PAGE_CACHE_SHIFT);
  1491. repeat:
  1492. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1493. if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
  1494. return -EINVAL;
  1495. page = filemap_getpage(file, pgoff, nonblock);
  1496. /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
  1497. * done in shmem_populate calling shmem_getpage */
  1498. if (!page && !nonblock)
  1499. return -ENOMEM;
  1500. if (page) {
  1501. err = install_page(mm, vma, addr, page, prot);
  1502. if (err) {
  1503. page_cache_release(page);
  1504. return err;
  1505. }
  1506. } else if (vma->vm_flags & VM_NONLINEAR) {
  1507. /* No page was found just because we can't read it in now (being
  1508. * here implies nonblock != 0), but the page may exist, so set
  1509. * the PTE to fault it in later. */
  1510. err = install_file_pte(mm, vma, addr, pgoff, prot);
  1511. if (err)
  1512. return err;
  1513. }
  1514. len -= PAGE_SIZE;
  1515. addr += PAGE_SIZE;
  1516. pgoff++;
  1517. if (len)
  1518. goto repeat;
  1519. return 0;
  1520. }
  1521. EXPORT_SYMBOL(filemap_populate);
  1522. struct vm_operations_struct generic_file_vm_ops = {
  1523. .nopage = filemap_nopage,
  1524. .populate = filemap_populate,
  1525. };
  1526. /* This is used for a general mmap of a disk file */
  1527. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1528. {
  1529. struct address_space *mapping = file->f_mapping;
  1530. if (!mapping->a_ops->readpage)
  1531. return -ENOEXEC;
  1532. file_accessed(file);
  1533. vma->vm_ops = &generic_file_vm_ops;
  1534. return 0;
  1535. }
  1536. /*
  1537. * This is for filesystems which do not implement ->writepage.
  1538. */
  1539. int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
  1540. {
  1541. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  1542. return -EINVAL;
  1543. return generic_file_mmap(file, vma);
  1544. }
  1545. #else
  1546. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1547. {
  1548. return -ENOSYS;
  1549. }
  1550. int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
  1551. {
  1552. return -ENOSYS;
  1553. }
  1554. #endif /* CONFIG_MMU */
  1555. EXPORT_SYMBOL(generic_file_mmap);
  1556. EXPORT_SYMBOL(generic_file_readonly_mmap);
  1557. static struct page *__read_cache_page(struct address_space *mapping,
  1558. unsigned long index,
  1559. int (*filler)(void *,struct page*),
  1560. void *data)
  1561. {
  1562. struct page *page, *cached_page = NULL;
  1563. int err;
  1564. repeat:
  1565. page = find_get_page(mapping, index);
  1566. if (!page) {
  1567. if (!cached_page) {
  1568. cached_page = page_cache_alloc_cold(mapping);
  1569. if (!cached_page)
  1570. return ERR_PTR(-ENOMEM);
  1571. }
  1572. err = add_to_page_cache_lru(cached_page, mapping,
  1573. index, GFP_KERNEL);
  1574. if (err == -EEXIST)
  1575. goto repeat;
  1576. if (err < 0) {
  1577. /* Presumably ENOMEM for radix tree node */
  1578. page_cache_release(cached_page);
  1579. return ERR_PTR(err);
  1580. }
  1581. page = cached_page;
  1582. cached_page = NULL;
  1583. err = filler(data, page);
  1584. if (err < 0) {
  1585. page_cache_release(page);
  1586. page = ERR_PTR(err);
  1587. }
  1588. }
  1589. if (cached_page)
  1590. page_cache_release(cached_page);
  1591. return page;
  1592. }
  1593. /*
  1594. * Same as read_cache_page, but don't wait for page to become unlocked
  1595. * after submitting it to the filler.
  1596. */
  1597. struct page *read_cache_page_async(struct address_space *mapping,
  1598. unsigned long index,
  1599. int (*filler)(void *,struct page*),
  1600. void *data)
  1601. {
  1602. struct page *page;
  1603. int err;
  1604. retry:
  1605. page = __read_cache_page(mapping, index, filler, data);
  1606. if (IS_ERR(page))
  1607. return page;
  1608. mark_page_accessed(page);
  1609. if (PageUptodate(page))
  1610. goto out;
  1611. lock_page(page);
  1612. if (!page->mapping) {
  1613. unlock_page(page);
  1614. page_cache_release(page);
  1615. goto retry;
  1616. }
  1617. if (PageUptodate(page)) {
  1618. unlock_page(page);
  1619. goto out;
  1620. }
  1621. err = filler(data, page);
  1622. if (err < 0) {
  1623. page_cache_release(page);
  1624. return ERR_PTR(err);
  1625. }
  1626. out:
  1627. mark_page_accessed(page);
  1628. return page;
  1629. }
  1630. EXPORT_SYMBOL(read_cache_page_async);
  1631. /**
  1632. * read_cache_page - read into page cache, fill it if needed
  1633. * @mapping: the page's address_space
  1634. * @index: the page index
  1635. * @filler: function to perform the read
  1636. * @data: destination for read data
  1637. *
  1638. * Read into the page cache. If a page already exists, and PageUptodate() is
  1639. * not set, try to fill the page then wait for it to become unlocked.
  1640. *
  1641. * If the page does not get brought uptodate, return -EIO.
  1642. */
  1643. struct page *read_cache_page(struct address_space *mapping,
  1644. unsigned long index,
  1645. int (*filler)(void *,struct page*),
  1646. void *data)
  1647. {
  1648. struct page *page;
  1649. page = read_cache_page_async(mapping, index, filler, data);
  1650. if (IS_ERR(page))
  1651. goto out;
  1652. wait_on_page_locked(page);
  1653. if (!PageUptodate(page)) {
  1654. page_cache_release(page);
  1655. page = ERR_PTR(-EIO);
  1656. }
  1657. out:
  1658. return page;
  1659. }
  1660. EXPORT_SYMBOL(read_cache_page);
  1661. /*
  1662. * If the page was newly created, increment its refcount and add it to the
  1663. * caller's lru-buffering pagevec. This function is specifically for
  1664. * generic_file_write().
  1665. */
  1666. static inline struct page *
  1667. __grab_cache_page(struct address_space *mapping, unsigned long index,
  1668. struct page **cached_page, struct pagevec *lru_pvec)
  1669. {
  1670. int err;
  1671. struct page *page;
  1672. repeat:
  1673. page = find_lock_page(mapping, index);
  1674. if (!page) {
  1675. if (!*cached_page) {
  1676. *cached_page = page_cache_alloc(mapping);
  1677. if (!*cached_page)
  1678. return NULL;
  1679. }
  1680. err = add_to_page_cache(*cached_page, mapping,
  1681. index, GFP_KERNEL);
  1682. if (err == -EEXIST)
  1683. goto repeat;
  1684. if (err == 0) {
  1685. page = *cached_page;
  1686. page_cache_get(page);
  1687. if (!pagevec_add(lru_pvec, page))
  1688. __pagevec_lru_add(lru_pvec);
  1689. *cached_page = NULL;
  1690. }
  1691. }
  1692. return page;
  1693. }
  1694. /*
  1695. * The logic we want is
  1696. *
  1697. * if suid or (sgid and xgrp)
  1698. * remove privs
  1699. */
  1700. int should_remove_suid(struct dentry *dentry)
  1701. {
  1702. mode_t mode = dentry->d_inode->i_mode;
  1703. int kill = 0;
  1704. /* suid always must be killed */
  1705. if (unlikely(mode & S_ISUID))
  1706. kill = ATTR_KILL_SUID;
  1707. /*
  1708. * sgid without any exec bits is just a mandatory locking mark; leave
  1709. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1710. */
  1711. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1712. kill |= ATTR_KILL_SGID;
  1713. if (unlikely(kill && !capable(CAP_FSETID)))
  1714. return kill;
  1715. return 0;
  1716. }
  1717. EXPORT_SYMBOL(should_remove_suid);
  1718. int __remove_suid(struct dentry *dentry, int kill)
  1719. {
  1720. struct iattr newattrs;
  1721. newattrs.ia_valid = ATTR_FORCE | kill;
  1722. return notify_change(dentry, &newattrs);
  1723. }
  1724. int remove_suid(struct dentry *dentry)
  1725. {
  1726. int kill = should_remove_suid(dentry);
  1727. if (unlikely(kill))
  1728. return __remove_suid(dentry, kill);
  1729. return 0;
  1730. }
  1731. EXPORT_SYMBOL(remove_suid);
  1732. size_t
  1733. __filemap_copy_from_user_iovec_inatomic(char *vaddr,
  1734. const struct iovec *iov, size_t base, size_t bytes)
  1735. {
  1736. size_t copied = 0, left = 0;
  1737. while (bytes) {
  1738. char __user *buf = iov->iov_base + base;
  1739. int copy = min(bytes, iov->iov_len - base);
  1740. base = 0;
  1741. left = __copy_from_user_inatomic_nocache(vaddr, buf, copy);
  1742. copied += copy;
  1743. bytes -= copy;
  1744. vaddr += copy;
  1745. iov++;
  1746. if (unlikely(left))
  1747. break;
  1748. }
  1749. return copied - left;
  1750. }
  1751. /*
  1752. * Performs necessary checks before doing a write
  1753. *
  1754. * Can adjust writing position or amount of bytes to write.
  1755. * Returns appropriate error code that caller should return or
  1756. * zero in case that write should be allowed.
  1757. */
  1758. inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
  1759. {
  1760. struct inode *inode = file->f_mapping->host;
  1761. unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1762. if (unlikely(*pos < 0))
  1763. return -EINVAL;
  1764. if (!isblk) {
  1765. /* FIXME: this is for backwards compatibility with 2.4 */
  1766. if (file->f_flags & O_APPEND)
  1767. *pos = i_size_read(inode);
  1768. if (limit != RLIM_INFINITY) {
  1769. if (*pos >= limit) {
  1770. send_sig(SIGXFSZ, current, 0);
  1771. return -EFBIG;
  1772. }
  1773. if (*count > limit - (typeof(limit))*pos) {
  1774. *count = limit - (typeof(limit))*pos;
  1775. }
  1776. }
  1777. }
  1778. /*
  1779. * LFS rule
  1780. */
  1781. if (unlikely(*pos + *count > MAX_NON_LFS &&
  1782. !(file->f_flags & O_LARGEFILE))) {
  1783. if (*pos >= MAX_NON_LFS) {
  1784. send_sig(SIGXFSZ, current, 0);
  1785. return -EFBIG;
  1786. }
  1787. if (*count > MAX_NON_LFS - (unsigned long)*pos) {
  1788. *count = MAX_NON_LFS - (unsigned long)*pos;
  1789. }
  1790. }
  1791. /*
  1792. * Are we about to exceed the fs block limit ?
  1793. *
  1794. * If we have written data it becomes a short write. If we have
  1795. * exceeded without writing data we send a signal and return EFBIG.
  1796. * Linus frestrict idea will clean these up nicely..
  1797. */
  1798. if (likely(!isblk)) {
  1799. if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
  1800. if (*count || *pos > inode->i_sb->s_maxbytes) {
  1801. send_sig(SIGXFSZ, current, 0);
  1802. return -EFBIG;
  1803. }
  1804. /* zero-length writes at ->s_maxbytes are OK */
  1805. }
  1806. if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
  1807. *count = inode->i_sb->s_maxbytes - *pos;
  1808. } else {
  1809. #ifdef CONFIG_BLOCK
  1810. loff_t isize;
  1811. if (bdev_read_only(I_BDEV(inode)))
  1812. return -EPERM;
  1813. isize = i_size_read(inode);
  1814. if (*pos >= isize) {
  1815. if (*count || *pos > isize)
  1816. return -ENOSPC;
  1817. }
  1818. if (*pos + *count > isize)
  1819. *count = isize - *pos;
  1820. #else
  1821. return -EPERM;
  1822. #endif
  1823. }
  1824. return 0;
  1825. }
  1826. EXPORT_SYMBOL(generic_write_checks);
  1827. ssize_t
  1828. generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  1829. unsigned long *nr_segs, loff_t pos, loff_t *ppos,
  1830. size_t count, size_t ocount)
  1831. {
  1832. struct file *file = iocb->ki_filp;
  1833. struct address_space *mapping = file->f_mapping;
  1834. struct inode *inode = mapping->host;
  1835. ssize_t written;
  1836. if (count != ocount)
  1837. *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
  1838. written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
  1839. if (written > 0) {
  1840. loff_t end = pos + written;
  1841. if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
  1842. i_size_write(inode, end);
  1843. mark_inode_dirty(inode);
  1844. }
  1845. *ppos = end;
  1846. }
  1847. /*
  1848. * Sync the fs metadata but not the minor inode changes and
  1849. * of course not the data as we did direct DMA for the IO.
  1850. * i_mutex is held, which protects generic_osync_inode() from
  1851. * livelocking. AIO O_DIRECT ops attempt to sync metadata here.
  1852. */
  1853. if ((written >= 0 || written == -EIOCBQUEUED) &&
  1854. ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1855. int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
  1856. if (err < 0)
  1857. written = err;
  1858. }
  1859. return written;
  1860. }
  1861. EXPORT_SYMBOL(generic_file_direct_write);
  1862. ssize_t
  1863. generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
  1864. unsigned long nr_segs, loff_t pos, loff_t *ppos,
  1865. size_t count, ssize_t written)
  1866. {
  1867. struct file *file = iocb->ki_filp;
  1868. struct address_space * mapping = file->f_mapping;
  1869. const struct address_space_operations *a_ops = mapping->a_ops;
  1870. struct inode *inode = mapping->host;
  1871. long status = 0;
  1872. struct page *page;
  1873. struct page *cached_page = NULL;
  1874. size_t bytes;
  1875. struct pagevec lru_pvec;
  1876. const struct iovec *cur_iov = iov; /* current iovec */
  1877. size_t iov_base = 0; /* offset in the current iovec */
  1878. char __user *buf;
  1879. pagevec_init(&lru_pvec, 0);
  1880. /*
  1881. * handle partial DIO write. Adjust cur_iov if needed.
  1882. */
  1883. if (likely(nr_segs == 1))
  1884. buf = iov->iov_base + written;
  1885. else {
  1886. filemap_set_next_iovec(&cur_iov, &iov_base, written);
  1887. buf = cur_iov->iov_base + iov_base;
  1888. }
  1889. do {
  1890. unsigned long index;
  1891. unsigned long offset;
  1892. size_t copied;
  1893. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1894. index = pos >> PAGE_CACHE_SHIFT;
  1895. bytes = PAGE_CACHE_SIZE - offset;
  1896. /* Limit the size of the copy to the caller's write size */
  1897. bytes = min(bytes, count);
  1898. /* We only need to worry about prefaulting when writes are from
  1899. * user-space. NFSd uses vfs_writev with several non-aligned
  1900. * segments in the vector, and limiting to one segment a time is
  1901. * a noticeable performance for re-write
  1902. */
  1903. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1904. /*
  1905. * Limit the size of the copy to that of the current
  1906. * segment, because fault_in_pages_readable() doesn't
  1907. * know how to walk segments.
  1908. */
  1909. bytes = min(bytes, cur_iov->iov_len - iov_base);
  1910. /*
  1911. * Bring in the user page that we will copy from
  1912. * _first_. Otherwise there's a nasty deadlock on
  1913. * copying from the same page as we're writing to,
  1914. * without it being marked up-to-date.
  1915. */
  1916. fault_in_pages_readable(buf, bytes);
  1917. }
  1918. page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
  1919. if (!page) {
  1920. status = -ENOMEM;
  1921. break;
  1922. }
  1923. if (unlikely(bytes == 0)) {
  1924. status = 0;
  1925. copied = 0;
  1926. goto zero_length_segment;
  1927. }
  1928. status = a_ops->prepare_write(file, page, offset, offset+bytes);
  1929. if (unlikely(status)) {
  1930. loff_t isize = i_size_read(inode);
  1931. if (status != AOP_TRUNCATED_PAGE)
  1932. unlock_page(page);
  1933. page_cache_release(page);
  1934. if (status == AOP_TRUNCATED_PAGE)
  1935. continue;
  1936. /*
  1937. * prepare_write() may have instantiated a few blocks
  1938. * outside i_size. Trim these off again.
  1939. */
  1940. if (pos + bytes > isize)
  1941. vmtruncate(inode, isize);
  1942. break;
  1943. }
  1944. if (likely(nr_segs == 1))
  1945. copied = filemap_copy_from_user(page, offset,
  1946. buf, bytes);
  1947. else
  1948. copied = filemap_copy_from_user_iovec(page, offset,
  1949. cur_iov, iov_base, bytes);
  1950. flush_dcache_page(page);
  1951. status = a_ops->commit_write(file, page, offset, offset+bytes);
  1952. if (status == AOP_TRUNCATED_PAGE) {
  1953. page_cache_release(page);
  1954. continue;
  1955. }
  1956. zero_length_segment:
  1957. if (likely(copied >= 0)) {
  1958. if (!status)
  1959. status = copied;
  1960. if (status >= 0) {
  1961. written += status;
  1962. count -= status;
  1963. pos += status;
  1964. buf += status;
  1965. if (unlikely(nr_segs > 1)) {
  1966. filemap_set_next_iovec(&cur_iov,
  1967. &iov_base, status);
  1968. if (count)
  1969. buf = cur_iov->iov_base +
  1970. iov_base;
  1971. } else {
  1972. iov_base += status;
  1973. }
  1974. }
  1975. }
  1976. if (unlikely(copied != bytes))
  1977. if (status >= 0)
  1978. status = -EFAULT;
  1979. unlock_page(page);
  1980. mark_page_accessed(page);
  1981. page_cache_release(page);
  1982. if (status < 0)
  1983. break;
  1984. balance_dirty_pages_ratelimited(mapping);
  1985. cond_resched();
  1986. } while (count);
  1987. *ppos = pos;
  1988. if (cached_page)
  1989. page_cache_release(cached_page);
  1990. /*
  1991. * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
  1992. */
  1993. if (likely(status >= 0)) {
  1994. if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1995. if (!a_ops->writepage || !is_sync_kiocb(iocb))
  1996. status = generic_osync_inode(inode, mapping,
  1997. OSYNC_METADATA|OSYNC_DATA);
  1998. }
  1999. }
  2000. /*
  2001. * If we get here for O_DIRECT writes then we must have fallen through
  2002. * to buffered writes (block instantiation inside i_size). So we sync
  2003. * the file data here, to try to honour O_DIRECT expectations.
  2004. */
  2005. if (unlikely(file->f_flags & O_DIRECT) && written)
  2006. status = filemap_write_and_wait(mapping);
  2007. pagevec_lru_add(&lru_pvec);
  2008. return written ? written : status;
  2009. }
  2010. EXPORT_SYMBOL(generic_file_buffered_write);
  2011. static ssize_t
  2012. __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
  2013. unsigned long nr_segs, loff_t *ppos)
  2014. {
  2015. struct file *file = iocb->ki_filp;
  2016. struct address_space * mapping = file->f_mapping;
  2017. size_t ocount; /* original count */
  2018. size_t count; /* after file limit checks */
  2019. struct inode *inode = mapping->host;
  2020. loff_t pos;
  2021. ssize_t written;
  2022. ssize_t err;
  2023. ocount = 0;
  2024. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  2025. if (err)
  2026. return err;
  2027. count = ocount;
  2028. pos = *ppos;
  2029. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  2030. /* We can write back this queue in page reclaim */
  2031. current->backing_dev_info = mapping->backing_dev_info;
  2032. written = 0;
  2033. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  2034. if (err)
  2035. goto out;
  2036. if (count == 0)
  2037. goto out;
  2038. err = remove_suid(file->f_path.dentry);
  2039. if (err)
  2040. goto out;
  2041. file_update_time(file);
  2042. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  2043. if (unlikely(file->f_flags & O_DIRECT)) {
  2044. loff_t endbyte;
  2045. ssize_t written_buffered;
  2046. written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
  2047. ppos, count, ocount);
  2048. if (written < 0 || written == count)
  2049. goto out;
  2050. /*
  2051. * direct-io write to a hole: fall through to buffered I/O
  2052. * for completing the rest of the request.
  2053. */
  2054. pos += written;
  2055. count -= written;
  2056. written_buffered = generic_file_buffered_write(iocb, iov,
  2057. nr_segs, pos, ppos, count,
  2058. written);
  2059. /*
  2060. * If generic_file_buffered_write() retuned a synchronous error
  2061. * then we want to return the number of bytes which were
  2062. * direct-written, or the error code if that was zero. Note
  2063. * that this differs from normal direct-io semantics, which
  2064. * will return -EFOO even if some bytes were written.
  2065. */
  2066. if (written_buffered < 0) {
  2067. err = written_buffered;
  2068. goto out;
  2069. }
  2070. /*
  2071. * We need to ensure that the page cache pages are written to
  2072. * disk and invalidated to preserve the expected O_DIRECT
  2073. * semantics.
  2074. */
  2075. endbyte = pos + written_buffered - written - 1;
  2076. err = do_sync_mapping_range(file->f_mapping, pos, endbyte,
  2077. SYNC_FILE_RANGE_WAIT_BEFORE|
  2078. SYNC_FILE_RANGE_WRITE|
  2079. SYNC_FILE_RANGE_WAIT_AFTER);
  2080. if (err == 0) {
  2081. written = written_buffered;
  2082. invalidate_mapping_pages(mapping,
  2083. pos >> PAGE_CACHE_SHIFT,
  2084. endbyte >> PAGE_CACHE_SHIFT);
  2085. } else {
  2086. /*
  2087. * We don't know how much we wrote, so just return
  2088. * the number of bytes which were direct-written
  2089. */
  2090. }
  2091. } else {
  2092. written = generic_file_buffered_write(iocb, iov, nr_segs,
  2093. pos, ppos, count, written);
  2094. }
  2095. out:
  2096. current->backing_dev_info = NULL;
  2097. return written ? written : err;
  2098. }
  2099. ssize_t generic_file_aio_write_nolock(struct kiocb *iocb,
  2100. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  2101. {
  2102. struct file *file = iocb->ki_filp;
  2103. struct address_space *mapping = file->f_mapping;
  2104. struct inode *inode = mapping->host;
  2105. ssize_t ret;
  2106. BUG_ON(iocb->ki_pos != pos);
  2107. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  2108. &iocb->ki_pos);
  2109. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2110. ssize_t err;
  2111. err = sync_page_range_nolock(inode, mapping, pos, ret);
  2112. if (err < 0)
  2113. ret = err;
  2114. }
  2115. return ret;
  2116. }
  2117. EXPORT_SYMBOL(generic_file_aio_write_nolock);
  2118. ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  2119. unsigned long nr_segs, loff_t pos)
  2120. {
  2121. struct file *file = iocb->ki_filp;
  2122. struct address_space *mapping = file->f_mapping;
  2123. struct inode *inode = mapping->host;
  2124. ssize_t ret;
  2125. BUG_ON(iocb->ki_pos != pos);
  2126. mutex_lock(&inode->i_mutex);
  2127. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  2128. &iocb->ki_pos);
  2129. mutex_unlock(&inode->i_mutex);
  2130. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2131. ssize_t err;
  2132. err = sync_page_range(inode, mapping, pos, ret);
  2133. if (err < 0)
  2134. ret = err;
  2135. }
  2136. return ret;
  2137. }
  2138. EXPORT_SYMBOL(generic_file_aio_write);
  2139. /*
  2140. * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
  2141. * went wrong during pagecache shootdown.
  2142. */
  2143. static ssize_t
  2144. generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  2145. loff_t offset, unsigned long nr_segs)
  2146. {
  2147. struct file *file = iocb->ki_filp;
  2148. struct address_space *mapping = file->f_mapping;
  2149. ssize_t retval;
  2150. size_t write_len;
  2151. pgoff_t end = 0; /* silence gcc */
  2152. /*
  2153. * If it's a write, unmap all mmappings of the file up-front. This
  2154. * will cause any pte dirty bits to be propagated into the pageframes
  2155. * for the subsequent filemap_write_and_wait().
  2156. */
  2157. if (rw == WRITE) {
  2158. write_len = iov_length(iov, nr_segs);
  2159. end = (offset + write_len - 1) >> PAGE_CACHE_SHIFT;
  2160. if (mapping_mapped(mapping))
  2161. unmap_mapping_range(mapping, offset, write_len, 0);
  2162. }
  2163. retval = filemap_write_and_wait(mapping);
  2164. if (retval)
  2165. goto out;
  2166. /*
  2167. * After a write we want buffered reads to be sure to go to disk to get
  2168. * the new data. We invalidate clean cached page from the region we're
  2169. * about to write. We do this *before* the write so that we can return
  2170. * -EIO without clobbering -EIOCBQUEUED from ->direct_IO().
  2171. */
  2172. if (rw == WRITE && mapping->nrpages) {
  2173. retval = invalidate_inode_pages2_range(mapping,
  2174. offset >> PAGE_CACHE_SHIFT, end);
  2175. if (retval)
  2176. goto out;
  2177. }
  2178. retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
  2179. if (retval)
  2180. goto out;
  2181. /*
  2182. * Finally, try again to invalidate clean pages which might have been
  2183. * faulted in by get_user_pages() if the source of the write was an
  2184. * mmap()ed region of the file we're writing. That's a pretty crazy
  2185. * thing to do, so we don't support it 100%. If this invalidation
  2186. * fails and we have -EIOCBQUEUED we ignore the failure.
  2187. */
  2188. if (rw == WRITE && mapping->nrpages) {
  2189. int err = invalidate_inode_pages2_range(mapping,
  2190. offset >> PAGE_CACHE_SHIFT, end);
  2191. if (err && retval >= 0)
  2192. retval = err;
  2193. }
  2194. out:
  2195. return retval;
  2196. }
  2197. /**
  2198. * try_to_release_page() - release old fs-specific metadata on a page
  2199. *
  2200. * @page: the page which the kernel is trying to free
  2201. * @gfp_mask: memory allocation flags (and I/O mode)
  2202. *
  2203. * The address_space is to try to release any data against the page
  2204. * (presumably at page->private). If the release was successful, return `1'.
  2205. * Otherwise return zero.
  2206. *
  2207. * The @gfp_mask argument specifies whether I/O may be performed to release
  2208. * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
  2209. *
  2210. * NOTE: @gfp_mask may go away, and this function may become non-blocking.
  2211. */
  2212. int try_to_release_page(struct page *page, gfp_t gfp_mask)
  2213. {
  2214. struct address_space * const mapping = page->mapping;
  2215. BUG_ON(!PageLocked(page));
  2216. if (PageWriteback(page))
  2217. return 0;
  2218. if (mapping && mapping->a_ops->releasepage)
  2219. return mapping->a_ops->releasepage(page, gfp_mask);
  2220. return try_to_free_buffers(page);
  2221. }
  2222. EXPORT_SYMBOL(try_to_release_page);