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