filemap.c 67 KB

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