filemap.c 59 KB

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