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