filemap.c 68 KB

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