filemap.c 68 KB

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