filemap.c 68 KB

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