filemap.c 69 KB

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