filemap.c 63 KB

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