filemap.c 67 KB

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