filemap.c 71 KB

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