filemap.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241
  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. read_lock_irq(&mapping->tree_lock);
  566. repeat:
  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. read_lock_irq(&mapping->tree_lock);
  574. /* Has the page been truncated while we slept? */
  575. if (unlikely(page->mapping != mapping ||
  576. page->index != offset)) {
  577. unlock_page(page);
  578. page_cache_release(page);
  579. goto repeat;
  580. }
  581. }
  582. }
  583. read_unlock_irq(&mapping->tree_lock);
  584. return page;
  585. }
  586. EXPORT_SYMBOL(find_lock_page);
  587. /**
  588. * find_or_create_page - locate or add a pagecache page
  589. * @mapping: the page's address_space
  590. * @index: the page's index into the mapping
  591. * @gfp_mask: page allocation mode
  592. *
  593. * Locates a page in the pagecache. If the page is not present, a new page
  594. * is allocated using @gfp_mask and is added to the pagecache and to the VM's
  595. * LRU list. The returned page is locked and has its reference count
  596. * incremented.
  597. *
  598. * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
  599. * allocation!
  600. *
  601. * find_or_create_page() returns the desired page's address, or zero on
  602. * memory exhaustion.
  603. */
  604. struct page *find_or_create_page(struct address_space *mapping,
  605. pgoff_t index, gfp_t gfp_mask)
  606. {
  607. struct page *page, *cached_page = NULL;
  608. int err;
  609. repeat:
  610. page = find_lock_page(mapping, index);
  611. if (!page) {
  612. if (!cached_page) {
  613. cached_page =
  614. __page_cache_alloc(gfp_mask);
  615. if (!cached_page)
  616. return NULL;
  617. }
  618. err = add_to_page_cache_lru(cached_page, mapping,
  619. index, gfp_mask);
  620. if (!err) {
  621. page = cached_page;
  622. cached_page = NULL;
  623. } else if (err == -EEXIST)
  624. goto repeat;
  625. }
  626. if (cached_page)
  627. page_cache_release(cached_page);
  628. return page;
  629. }
  630. EXPORT_SYMBOL(find_or_create_page);
  631. /**
  632. * find_get_pages - gang pagecache lookup
  633. * @mapping: The address_space to search
  634. * @start: The starting page index
  635. * @nr_pages: The maximum number of pages
  636. * @pages: Where the resulting pages are placed
  637. *
  638. * find_get_pages() will search for and return a group of up to
  639. * @nr_pages pages in the mapping. The pages are placed at @pages.
  640. * find_get_pages() takes a reference against the returned pages.
  641. *
  642. * The search returns a group of mapping-contiguous pages with ascending
  643. * indexes. There may be holes in the indices due to not-present pages.
  644. *
  645. * find_get_pages() returns the number of pages which were found.
  646. */
  647. unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
  648. unsigned int nr_pages, struct page **pages)
  649. {
  650. unsigned int i;
  651. unsigned int ret;
  652. read_lock_irq(&mapping->tree_lock);
  653. ret = radix_tree_gang_lookup(&mapping->page_tree,
  654. (void **)pages, start, nr_pages);
  655. for (i = 0; i < ret; i++)
  656. page_cache_get(pages[i]);
  657. read_unlock_irq(&mapping->tree_lock);
  658. return ret;
  659. }
  660. /**
  661. * find_get_pages_contig - gang contiguous pagecache lookup
  662. * @mapping: The address_space to search
  663. * @index: The starting page index
  664. * @nr_pages: The maximum number of pages
  665. * @pages: Where the resulting pages are placed
  666. *
  667. * find_get_pages_contig() works exactly like find_get_pages(), except
  668. * that the returned number of pages are guaranteed to be contiguous.
  669. *
  670. * find_get_pages_contig() returns the number of pages which were found.
  671. */
  672. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
  673. unsigned int nr_pages, struct page **pages)
  674. {
  675. unsigned int i;
  676. unsigned int ret;
  677. read_lock_irq(&mapping->tree_lock);
  678. ret = radix_tree_gang_lookup(&mapping->page_tree,
  679. (void **)pages, index, nr_pages);
  680. for (i = 0; i < ret; i++) {
  681. if (pages[i]->mapping == NULL || pages[i]->index != index)
  682. break;
  683. page_cache_get(pages[i]);
  684. index++;
  685. }
  686. read_unlock_irq(&mapping->tree_lock);
  687. return i;
  688. }
  689. EXPORT_SYMBOL(find_get_pages_contig);
  690. /**
  691. * find_get_pages_tag - find and return pages that match @tag
  692. * @mapping: the address_space to search
  693. * @index: the starting page index
  694. * @tag: the tag index
  695. * @nr_pages: the maximum number of pages
  696. * @pages: where the resulting pages are placed
  697. *
  698. * Like find_get_pages, except we only return pages which are tagged with
  699. * @tag. We update @index to index the next page for the traversal.
  700. */
  701. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  702. int tag, unsigned int nr_pages, struct page **pages)
  703. {
  704. unsigned int i;
  705. unsigned int ret;
  706. read_lock_irq(&mapping->tree_lock);
  707. ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
  708. (void **)pages, *index, nr_pages, tag);
  709. for (i = 0; i < ret; i++)
  710. page_cache_get(pages[i]);
  711. if (ret)
  712. *index = pages[ret - 1]->index + 1;
  713. read_unlock_irq(&mapping->tree_lock);
  714. return ret;
  715. }
  716. EXPORT_SYMBOL(find_get_pages_tag);
  717. /**
  718. * grab_cache_page_nowait - returns locked page at given index in given cache
  719. * @mapping: target address_space
  720. * @index: the page index
  721. *
  722. * Same as grab_cache_page(), but do not wait if the page is unavailable.
  723. * This is intended for speculative data generators, where the data can
  724. * be regenerated if the page couldn't be grabbed. This routine should
  725. * be safe to call while holding the lock for another page.
  726. *
  727. * Clear __GFP_FS when allocating the page to avoid recursion into the fs
  728. * and deadlock against the caller's locked page.
  729. */
  730. struct page *
  731. grab_cache_page_nowait(struct address_space *mapping, pgoff_t index)
  732. {
  733. struct page *page = find_get_page(mapping, index);
  734. if (page) {
  735. if (!TestSetPageLocked(page))
  736. return page;
  737. page_cache_release(page);
  738. return NULL;
  739. }
  740. page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
  741. if (page && add_to_page_cache_lru(page, mapping, index, GFP_KERNEL)) {
  742. page_cache_release(page);
  743. page = NULL;
  744. }
  745. return page;
  746. }
  747. EXPORT_SYMBOL(grab_cache_page_nowait);
  748. /*
  749. * CD/DVDs are error prone. When a medium error occurs, the driver may fail
  750. * a _large_ part of the i/o request. Imagine the worst scenario:
  751. *
  752. * ---R__________________________________________B__________
  753. * ^ reading here ^ bad block(assume 4k)
  754. *
  755. * read(R) => miss => readahead(R...B) => media error => frustrating retries
  756. * => failing the whole request => read(R) => read(R+1) =>
  757. * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
  758. * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
  759. * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
  760. *
  761. * It is going insane. Fix it by quickly scaling down the readahead size.
  762. */
  763. static void shrink_readahead_size_eio(struct file *filp,
  764. struct file_ra_state *ra)
  765. {
  766. if (!ra->ra_pages)
  767. return;
  768. ra->ra_pages /= 4;
  769. }
  770. /**
  771. * do_generic_mapping_read - generic file read routine
  772. * @mapping: address_space to be read
  773. * @_ra: file's readahead state
  774. * @filp: the file to read
  775. * @ppos: current file position
  776. * @desc: read_descriptor
  777. * @actor: read method
  778. *
  779. * This is a generic file read routine, and uses the
  780. * mapping->a_ops->readpage() function for the actual low-level stuff.
  781. *
  782. * This is really ugly. But the goto's actually try to clarify some
  783. * of the logic when it comes to error handling etc.
  784. *
  785. * Note the struct file* is only passed for the use of readpage.
  786. * It may be NULL.
  787. */
  788. void do_generic_mapping_read(struct address_space *mapping,
  789. struct file_ra_state *ra,
  790. struct file *filp,
  791. loff_t *ppos,
  792. read_descriptor_t *desc,
  793. read_actor_t actor)
  794. {
  795. struct inode *inode = mapping->host;
  796. pgoff_t index;
  797. pgoff_t last_index;
  798. pgoff_t prev_index;
  799. unsigned long offset; /* offset into pagecache page */
  800. unsigned int prev_offset;
  801. struct page *cached_page;
  802. int error;
  803. cached_page = NULL;
  804. index = *ppos >> PAGE_CACHE_SHIFT;
  805. prev_index = ra->prev_pos >> PAGE_CACHE_SHIFT;
  806. prev_offset = ra->prev_pos & (PAGE_CACHE_SIZE-1);
  807. last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  808. offset = *ppos & ~PAGE_CACHE_MASK;
  809. for (;;) {
  810. struct page *page;
  811. pgoff_t end_index;
  812. loff_t isize;
  813. unsigned long nr, ret;
  814. cond_resched();
  815. find_page:
  816. page = find_get_page(mapping, index);
  817. if (!page) {
  818. page_cache_sync_readahead(mapping,
  819. ra, filp,
  820. index, last_index - index);
  821. page = find_get_page(mapping, index);
  822. if (unlikely(page == NULL))
  823. goto no_cached_page;
  824. }
  825. if (PageReadahead(page)) {
  826. page_cache_async_readahead(mapping,
  827. ra, filp, page,
  828. index, last_index - index);
  829. }
  830. if (!PageUptodate(page))
  831. goto page_not_up_to_date;
  832. page_ok:
  833. /*
  834. * i_size must be checked after we know the page is Uptodate.
  835. *
  836. * Checking i_size after the check allows us to calculate
  837. * the correct value for "nr", which means the zero-filled
  838. * part of the page is not copied back to userspace (unless
  839. * another truncate extends the file - this is desired though).
  840. */
  841. isize = i_size_read(inode);
  842. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  843. if (unlikely(!isize || index > end_index)) {
  844. page_cache_release(page);
  845. goto out;
  846. }
  847. /* nr is the maximum number of bytes to copy from this page */
  848. nr = PAGE_CACHE_SIZE;
  849. if (index == end_index) {
  850. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  851. if (nr <= offset) {
  852. page_cache_release(page);
  853. goto out;
  854. }
  855. }
  856. nr = nr - offset;
  857. /* If users can be writing to this page using arbitrary
  858. * virtual addresses, take care about potential aliasing
  859. * before reading the page on the kernel side.
  860. */
  861. if (mapping_writably_mapped(mapping))
  862. flush_dcache_page(page);
  863. /*
  864. * When a sequential read accesses a page several times,
  865. * only mark it as accessed the first time.
  866. */
  867. if (prev_index != index || offset != prev_offset)
  868. mark_page_accessed(page);
  869. prev_index = index;
  870. /*
  871. * Ok, we have the page, and it's up-to-date, so
  872. * now we can copy it to user space...
  873. *
  874. * The actor routine returns how many bytes were actually used..
  875. * NOTE! This may not be the same as how much of a user buffer
  876. * we filled up (we may be padding etc), so we can only update
  877. * "pos" here (the actor routine has to update the user buffer
  878. * pointers and the remaining count).
  879. */
  880. ret = actor(desc, page, offset, nr);
  881. offset += ret;
  882. index += offset >> PAGE_CACHE_SHIFT;
  883. offset &= ~PAGE_CACHE_MASK;
  884. prev_offset = offset;
  885. page_cache_release(page);
  886. if (ret == nr && desc->count)
  887. continue;
  888. goto out;
  889. page_not_up_to_date:
  890. /* Get exclusive access to the page ... */
  891. lock_page(page);
  892. /* Did it get truncated before we got the lock? */
  893. if (!page->mapping) {
  894. unlock_page(page);
  895. page_cache_release(page);
  896. continue;
  897. }
  898. /* Did somebody else fill it already? */
  899. if (PageUptodate(page)) {
  900. unlock_page(page);
  901. goto page_ok;
  902. }
  903. readpage:
  904. /* Start the actual read. The read will unlock the page. */
  905. error = mapping->a_ops->readpage(filp, page);
  906. if (unlikely(error)) {
  907. if (error == AOP_TRUNCATED_PAGE) {
  908. page_cache_release(page);
  909. goto find_page;
  910. }
  911. goto readpage_error;
  912. }
  913. if (!PageUptodate(page)) {
  914. lock_page(page);
  915. if (!PageUptodate(page)) {
  916. if (page->mapping == NULL) {
  917. /*
  918. * invalidate_inode_pages got it
  919. */
  920. unlock_page(page);
  921. page_cache_release(page);
  922. goto find_page;
  923. }
  924. unlock_page(page);
  925. error = -EIO;
  926. shrink_readahead_size_eio(filp, ra);
  927. goto readpage_error;
  928. }
  929. unlock_page(page);
  930. }
  931. goto page_ok;
  932. readpage_error:
  933. /* UHHUH! A synchronous read error occurred. Report it */
  934. desc->error = error;
  935. page_cache_release(page);
  936. goto out;
  937. no_cached_page:
  938. /*
  939. * Ok, it wasn't cached, so we need to create a new
  940. * page..
  941. */
  942. if (!cached_page) {
  943. cached_page = page_cache_alloc_cold(mapping);
  944. if (!cached_page) {
  945. desc->error = -ENOMEM;
  946. goto out;
  947. }
  948. }
  949. error = add_to_page_cache_lru(cached_page, mapping,
  950. index, GFP_KERNEL);
  951. if (error) {
  952. if (error == -EEXIST)
  953. goto find_page;
  954. desc->error = error;
  955. goto out;
  956. }
  957. page = cached_page;
  958. cached_page = NULL;
  959. goto readpage;
  960. }
  961. out:
  962. ra->prev_pos = prev_index;
  963. ra->prev_pos <<= PAGE_CACHE_SHIFT;
  964. ra->prev_pos |= prev_offset;
  965. *ppos = ((loff_t)index << PAGE_CACHE_SHIFT) + offset;
  966. if (cached_page)
  967. page_cache_release(cached_page);
  968. if (filp)
  969. file_accessed(filp);
  970. }
  971. EXPORT_SYMBOL(do_generic_mapping_read);
  972. int file_read_actor(read_descriptor_t *desc, struct page *page,
  973. unsigned long offset, unsigned long size)
  974. {
  975. char *kaddr;
  976. unsigned long left, count = desc->count;
  977. if (size > count)
  978. size = count;
  979. /*
  980. * Faults on the destination of a read are common, so do it before
  981. * taking the kmap.
  982. */
  983. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  984. kaddr = kmap_atomic(page, KM_USER0);
  985. left = __copy_to_user_inatomic(desc->arg.buf,
  986. kaddr + offset, size);
  987. kunmap_atomic(kaddr, KM_USER0);
  988. if (left == 0)
  989. goto success;
  990. }
  991. /* Do it the slow way */
  992. kaddr = kmap(page);
  993. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  994. kunmap(page);
  995. if (left) {
  996. size -= left;
  997. desc->error = -EFAULT;
  998. }
  999. success:
  1000. desc->count = count - size;
  1001. desc->written += size;
  1002. desc->arg.buf += size;
  1003. return size;
  1004. }
  1005. /*
  1006. * Performs necessary checks before doing a write
  1007. * @iov: io vector request
  1008. * @nr_segs: number of segments in the iovec
  1009. * @count: number of bytes to write
  1010. * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
  1011. *
  1012. * Adjust number of segments and amount of bytes to write (nr_segs should be
  1013. * properly initialized first). Returns appropriate error code that caller
  1014. * should return or zero in case that write should be allowed.
  1015. */
  1016. int generic_segment_checks(const struct iovec *iov,
  1017. unsigned long *nr_segs, size_t *count, int access_flags)
  1018. {
  1019. unsigned long seg;
  1020. size_t cnt = 0;
  1021. for (seg = 0; seg < *nr_segs; seg++) {
  1022. const struct iovec *iv = &iov[seg];
  1023. /*
  1024. * If any segment has a negative length, or the cumulative
  1025. * length ever wraps negative then return -EINVAL.
  1026. */
  1027. cnt += iv->iov_len;
  1028. if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
  1029. return -EINVAL;
  1030. if (access_ok(access_flags, iv->iov_base, iv->iov_len))
  1031. continue;
  1032. if (seg == 0)
  1033. return -EFAULT;
  1034. *nr_segs = seg;
  1035. cnt -= iv->iov_len; /* This segment is no good */
  1036. break;
  1037. }
  1038. *count = cnt;
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL(generic_segment_checks);
  1042. /**
  1043. * generic_file_aio_read - generic filesystem read routine
  1044. * @iocb: kernel I/O control block
  1045. * @iov: io vector request
  1046. * @nr_segs: number of segments in the iovec
  1047. * @pos: current file position
  1048. *
  1049. * This is the "read()" routine for all filesystems
  1050. * that can use the page cache directly.
  1051. */
  1052. ssize_t
  1053. generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1054. unsigned long nr_segs, loff_t pos)
  1055. {
  1056. struct file *filp = iocb->ki_filp;
  1057. ssize_t retval;
  1058. unsigned long seg;
  1059. size_t count;
  1060. loff_t *ppos = &iocb->ki_pos;
  1061. count = 0;
  1062. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1063. if (retval)
  1064. return retval;
  1065. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1066. if (filp->f_flags & O_DIRECT) {
  1067. loff_t size;
  1068. struct address_space *mapping;
  1069. struct inode *inode;
  1070. mapping = filp->f_mapping;
  1071. inode = mapping->host;
  1072. retval = 0;
  1073. if (!count)
  1074. goto out; /* skip atime */
  1075. size = i_size_read(inode);
  1076. if (pos < size) {
  1077. retval = generic_file_direct_IO(READ, iocb,
  1078. iov, pos, nr_segs);
  1079. if (retval > 0)
  1080. *ppos = pos + retval;
  1081. }
  1082. if (likely(retval != 0)) {
  1083. file_accessed(filp);
  1084. goto out;
  1085. }
  1086. }
  1087. retval = 0;
  1088. if (count) {
  1089. for (seg = 0; seg < nr_segs; seg++) {
  1090. read_descriptor_t desc;
  1091. desc.written = 0;
  1092. desc.arg.buf = iov[seg].iov_base;
  1093. desc.count = iov[seg].iov_len;
  1094. if (desc.count == 0)
  1095. continue;
  1096. desc.error = 0;
  1097. do_generic_file_read(filp,ppos,&desc,file_read_actor);
  1098. retval += desc.written;
  1099. if (desc.error) {
  1100. retval = retval ?: desc.error;
  1101. break;
  1102. }
  1103. if (desc.count > 0)
  1104. break;
  1105. }
  1106. }
  1107. out:
  1108. return retval;
  1109. }
  1110. EXPORT_SYMBOL(generic_file_aio_read);
  1111. static ssize_t
  1112. do_readahead(struct address_space *mapping, struct file *filp,
  1113. pgoff_t index, unsigned long nr)
  1114. {
  1115. if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
  1116. return -EINVAL;
  1117. force_page_cache_readahead(mapping, filp, index,
  1118. max_sane_readahead(nr));
  1119. return 0;
  1120. }
  1121. asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
  1122. {
  1123. ssize_t ret;
  1124. struct file *file;
  1125. ret = -EBADF;
  1126. file = fget(fd);
  1127. if (file) {
  1128. if (file->f_mode & FMODE_READ) {
  1129. struct address_space *mapping = file->f_mapping;
  1130. pgoff_t start = offset >> PAGE_CACHE_SHIFT;
  1131. pgoff_t end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  1132. unsigned long len = end - start + 1;
  1133. ret = do_readahead(mapping, file, start, len);
  1134. }
  1135. fput(file);
  1136. }
  1137. return ret;
  1138. }
  1139. #ifdef CONFIG_MMU
  1140. /**
  1141. * page_cache_read - adds requested page to the page cache if not already there
  1142. * @file: file to read
  1143. * @offset: page index
  1144. *
  1145. * This adds the requested page to the page cache if it isn't already there,
  1146. * and schedules an I/O to read in its contents from disk.
  1147. */
  1148. static int fastcall page_cache_read(struct file * file, pgoff_t offset)
  1149. {
  1150. struct address_space *mapping = file->f_mapping;
  1151. struct page *page;
  1152. int ret;
  1153. do {
  1154. page = page_cache_alloc_cold(mapping);
  1155. if (!page)
  1156. return -ENOMEM;
  1157. ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
  1158. if (ret == 0)
  1159. ret = mapping->a_ops->readpage(file, page);
  1160. else if (ret == -EEXIST)
  1161. ret = 0; /* losing race to add is OK */
  1162. page_cache_release(page);
  1163. } while (ret == AOP_TRUNCATED_PAGE);
  1164. return ret;
  1165. }
  1166. #define MMAP_LOTSAMISS (100)
  1167. /**
  1168. * filemap_fault - read in file data for page fault handling
  1169. * @vma: vma in which the fault was taken
  1170. * @vmf: struct vm_fault containing details of the fault
  1171. *
  1172. * filemap_fault() is invoked via the vma operations vector for a
  1173. * mapped memory region to read in file data during a page fault.
  1174. *
  1175. * The goto's are kind of ugly, but this streamlines the normal case of having
  1176. * it in the page cache, and handles the special cases reasonably without
  1177. * having a lot of duplicated code.
  1178. */
  1179. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1180. {
  1181. int error;
  1182. struct file *file = vma->vm_file;
  1183. struct address_space *mapping = file->f_mapping;
  1184. struct file_ra_state *ra = &file->f_ra;
  1185. struct inode *inode = mapping->host;
  1186. struct page *page;
  1187. unsigned long size;
  1188. int did_readaround = 0;
  1189. int ret = 0;
  1190. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1191. if (vmf->pgoff >= size)
  1192. goto outside_data_content;
  1193. /* If we don't want any read-ahead, don't bother */
  1194. if (VM_RandomReadHint(vma))
  1195. goto no_cached_page;
  1196. /*
  1197. * Do we have something in the page cache already?
  1198. */
  1199. retry_find:
  1200. page = find_lock_page(mapping, vmf->pgoff);
  1201. /*
  1202. * For sequential accesses, we use the generic readahead logic.
  1203. */
  1204. if (VM_SequentialReadHint(vma)) {
  1205. if (!page) {
  1206. page_cache_sync_readahead(mapping, ra, file,
  1207. vmf->pgoff, 1);
  1208. page = find_lock_page(mapping, vmf->pgoff);
  1209. if (!page)
  1210. goto no_cached_page;
  1211. }
  1212. if (PageReadahead(page)) {
  1213. page_cache_async_readahead(mapping, ra, file, page,
  1214. vmf->pgoff, 1);
  1215. }
  1216. }
  1217. if (!page) {
  1218. unsigned long ra_pages;
  1219. ra->mmap_miss++;
  1220. /*
  1221. * Do we miss much more than hit in this file? If so,
  1222. * stop bothering with read-ahead. It will only hurt.
  1223. */
  1224. if (ra->mmap_miss > MMAP_LOTSAMISS)
  1225. goto no_cached_page;
  1226. /*
  1227. * To keep the pgmajfault counter straight, we need to
  1228. * check did_readaround, as this is an inner loop.
  1229. */
  1230. if (!did_readaround) {
  1231. ret = VM_FAULT_MAJOR;
  1232. count_vm_event(PGMAJFAULT);
  1233. }
  1234. did_readaround = 1;
  1235. ra_pages = max_sane_readahead(file->f_ra.ra_pages);
  1236. if (ra_pages) {
  1237. pgoff_t start = 0;
  1238. if (vmf->pgoff > ra_pages / 2)
  1239. start = vmf->pgoff - ra_pages / 2;
  1240. do_page_cache_readahead(mapping, file, start, ra_pages);
  1241. }
  1242. page = find_lock_page(mapping, vmf->pgoff);
  1243. if (!page)
  1244. goto no_cached_page;
  1245. }
  1246. if (!did_readaround)
  1247. ra->mmap_miss--;
  1248. /*
  1249. * We have a locked page in the page cache, now we need to check
  1250. * that it's up-to-date. If not, it is going to be due to an error.
  1251. */
  1252. if (unlikely(!PageUptodate(page)))
  1253. goto page_not_uptodate;
  1254. /* Must recheck i_size under page lock */
  1255. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1256. if (unlikely(vmf->pgoff >= size)) {
  1257. unlock_page(page);
  1258. page_cache_release(page);
  1259. goto outside_data_content;
  1260. }
  1261. /*
  1262. * Found the page and have a reference on it.
  1263. */
  1264. mark_page_accessed(page);
  1265. ra->prev_pos = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1266. vmf->page = page;
  1267. return ret | VM_FAULT_LOCKED;
  1268. outside_data_content:
  1269. /*
  1270. * An external ptracer can access pages that normally aren't
  1271. * accessible..
  1272. */
  1273. if (vma->vm_mm == current->mm)
  1274. return VM_FAULT_SIGBUS;
  1275. /* Fall through to the non-read-ahead case */
  1276. no_cached_page:
  1277. /*
  1278. * We're only likely to ever get here if MADV_RANDOM is in
  1279. * effect.
  1280. */
  1281. error = page_cache_read(file, vmf->pgoff);
  1282. /*
  1283. * The page we want has now been added to the page cache.
  1284. * In the unlikely event that someone removed it in the
  1285. * meantime, we'll just come back here and read it again.
  1286. */
  1287. if (error >= 0)
  1288. goto retry_find;
  1289. /*
  1290. * An error return from page_cache_read can result if the
  1291. * system is low on memory, or a problem occurs while trying
  1292. * to schedule I/O.
  1293. */
  1294. if (error == -ENOMEM)
  1295. return VM_FAULT_OOM;
  1296. return VM_FAULT_SIGBUS;
  1297. page_not_uptodate:
  1298. /* IO error path */
  1299. if (!did_readaround) {
  1300. ret = VM_FAULT_MAJOR;
  1301. count_vm_event(PGMAJFAULT);
  1302. }
  1303. /*
  1304. * Umm, take care of errors if the page isn't up-to-date.
  1305. * Try to re-read it _once_. We do this synchronously,
  1306. * because there really aren't any performance issues here
  1307. * and we need to check for errors.
  1308. */
  1309. ClearPageError(page);
  1310. error = mapping->a_ops->readpage(file, page);
  1311. page_cache_release(page);
  1312. if (!error || error == AOP_TRUNCATED_PAGE)
  1313. goto retry_find;
  1314. /* Things didn't work out. Return zero to tell the mm layer so. */
  1315. shrink_readahead_size_eio(file, ra);
  1316. return VM_FAULT_SIGBUS;
  1317. }
  1318. EXPORT_SYMBOL(filemap_fault);
  1319. struct vm_operations_struct generic_file_vm_ops = {
  1320. .fault = filemap_fault,
  1321. };
  1322. /* This is used for a general mmap of a disk file */
  1323. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1324. {
  1325. struct address_space *mapping = file->f_mapping;
  1326. if (!mapping->a_ops->readpage)
  1327. return -ENOEXEC;
  1328. file_accessed(file);
  1329. vma->vm_ops = &generic_file_vm_ops;
  1330. vma->vm_flags |= VM_CAN_NONLINEAR;
  1331. return 0;
  1332. }
  1333. /*
  1334. * This is for filesystems which do not implement ->writepage.
  1335. */
  1336. int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
  1337. {
  1338. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  1339. return -EINVAL;
  1340. return generic_file_mmap(file, vma);
  1341. }
  1342. #else
  1343. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1344. {
  1345. return -ENOSYS;
  1346. }
  1347. int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
  1348. {
  1349. return -ENOSYS;
  1350. }
  1351. #endif /* CONFIG_MMU */
  1352. EXPORT_SYMBOL(generic_file_mmap);
  1353. EXPORT_SYMBOL(generic_file_readonly_mmap);
  1354. static struct page *__read_cache_page(struct address_space *mapping,
  1355. pgoff_t index,
  1356. int (*filler)(void *,struct page*),
  1357. void *data)
  1358. {
  1359. struct page *page, *cached_page = NULL;
  1360. int err;
  1361. repeat:
  1362. page = find_get_page(mapping, index);
  1363. if (!page) {
  1364. if (!cached_page) {
  1365. cached_page = page_cache_alloc_cold(mapping);
  1366. if (!cached_page)
  1367. return ERR_PTR(-ENOMEM);
  1368. }
  1369. err = add_to_page_cache_lru(cached_page, mapping,
  1370. index, GFP_KERNEL);
  1371. if (err == -EEXIST)
  1372. goto repeat;
  1373. if (err < 0) {
  1374. /* Presumably ENOMEM for radix tree node */
  1375. page_cache_release(cached_page);
  1376. return ERR_PTR(err);
  1377. }
  1378. page = cached_page;
  1379. cached_page = NULL;
  1380. err = filler(data, page);
  1381. if (err < 0) {
  1382. page_cache_release(page);
  1383. page = ERR_PTR(err);
  1384. }
  1385. }
  1386. if (cached_page)
  1387. page_cache_release(cached_page);
  1388. return page;
  1389. }
  1390. /*
  1391. * Same as read_cache_page, but don't wait for page to become unlocked
  1392. * after submitting it to the filler.
  1393. */
  1394. struct page *read_cache_page_async(struct address_space *mapping,
  1395. pgoff_t index,
  1396. int (*filler)(void *,struct page*),
  1397. void *data)
  1398. {
  1399. struct page *page;
  1400. int err;
  1401. retry:
  1402. page = __read_cache_page(mapping, index, filler, data);
  1403. if (IS_ERR(page))
  1404. return page;
  1405. if (PageUptodate(page))
  1406. goto out;
  1407. lock_page(page);
  1408. if (!page->mapping) {
  1409. unlock_page(page);
  1410. page_cache_release(page);
  1411. goto retry;
  1412. }
  1413. if (PageUptodate(page)) {
  1414. unlock_page(page);
  1415. goto out;
  1416. }
  1417. err = filler(data, page);
  1418. if (err < 0) {
  1419. page_cache_release(page);
  1420. return ERR_PTR(err);
  1421. }
  1422. out:
  1423. mark_page_accessed(page);
  1424. return page;
  1425. }
  1426. EXPORT_SYMBOL(read_cache_page_async);
  1427. /**
  1428. * read_cache_page - read into page cache, fill it if needed
  1429. * @mapping: the page's address_space
  1430. * @index: the page index
  1431. * @filler: function to perform the read
  1432. * @data: destination for read data
  1433. *
  1434. * Read into the page cache. If a page already exists, and PageUptodate() is
  1435. * not set, try to fill the page then wait for it to become unlocked.
  1436. *
  1437. * If the page does not get brought uptodate, return -EIO.
  1438. */
  1439. struct page *read_cache_page(struct address_space *mapping,
  1440. pgoff_t index,
  1441. int (*filler)(void *,struct page*),
  1442. void *data)
  1443. {
  1444. struct page *page;
  1445. page = read_cache_page_async(mapping, index, filler, data);
  1446. if (IS_ERR(page))
  1447. goto out;
  1448. wait_on_page_locked(page);
  1449. if (!PageUptodate(page)) {
  1450. page_cache_release(page);
  1451. page = ERR_PTR(-EIO);
  1452. }
  1453. out:
  1454. return page;
  1455. }
  1456. EXPORT_SYMBOL(read_cache_page);
  1457. /*
  1458. * If the page was newly created, increment its refcount and add it to the
  1459. * caller's lru-buffering pagevec. This function is specifically for
  1460. * generic_file_write().
  1461. */
  1462. static inline struct page *
  1463. __grab_cache_page(struct address_space *mapping, unsigned long index,
  1464. struct page **cached_page, struct pagevec *lru_pvec)
  1465. {
  1466. int err;
  1467. struct page *page;
  1468. repeat:
  1469. page = find_lock_page(mapping, index);
  1470. if (!page) {
  1471. if (!*cached_page) {
  1472. *cached_page = page_cache_alloc(mapping);
  1473. if (!*cached_page)
  1474. return NULL;
  1475. }
  1476. err = add_to_page_cache(*cached_page, mapping,
  1477. index, GFP_KERNEL);
  1478. if (err == -EEXIST)
  1479. goto repeat;
  1480. if (err == 0) {
  1481. page = *cached_page;
  1482. page_cache_get(page);
  1483. if (!pagevec_add(lru_pvec, page))
  1484. __pagevec_lru_add(lru_pvec);
  1485. *cached_page = NULL;
  1486. }
  1487. }
  1488. return page;
  1489. }
  1490. /*
  1491. * The logic we want is
  1492. *
  1493. * if suid or (sgid and xgrp)
  1494. * remove privs
  1495. */
  1496. int should_remove_suid(struct dentry *dentry)
  1497. {
  1498. mode_t mode = dentry->d_inode->i_mode;
  1499. int kill = 0;
  1500. /* suid always must be killed */
  1501. if (unlikely(mode & S_ISUID))
  1502. kill = ATTR_KILL_SUID;
  1503. /*
  1504. * sgid without any exec bits is just a mandatory locking mark; leave
  1505. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1506. */
  1507. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1508. kill |= ATTR_KILL_SGID;
  1509. if (unlikely(kill && !capable(CAP_FSETID)))
  1510. return kill;
  1511. return 0;
  1512. }
  1513. EXPORT_SYMBOL(should_remove_suid);
  1514. int __remove_suid(struct dentry *dentry, int kill)
  1515. {
  1516. struct iattr newattrs;
  1517. newattrs.ia_valid = ATTR_FORCE | kill;
  1518. return notify_change(dentry, &newattrs);
  1519. }
  1520. int remove_suid(struct dentry *dentry)
  1521. {
  1522. int kill = should_remove_suid(dentry);
  1523. if (unlikely(kill))
  1524. return __remove_suid(dentry, kill);
  1525. return 0;
  1526. }
  1527. EXPORT_SYMBOL(remove_suid);
  1528. size_t
  1529. __filemap_copy_from_user_iovec_inatomic(char *vaddr,
  1530. const struct iovec *iov, size_t base, size_t bytes)
  1531. {
  1532. size_t copied = 0, left = 0;
  1533. while (bytes) {
  1534. char __user *buf = iov->iov_base + base;
  1535. int copy = min(bytes, iov->iov_len - base);
  1536. base = 0;
  1537. left = __copy_from_user_inatomic_nocache(vaddr, buf, copy);
  1538. copied += copy;
  1539. bytes -= copy;
  1540. vaddr += copy;
  1541. iov++;
  1542. if (unlikely(left))
  1543. break;
  1544. }
  1545. return copied - left;
  1546. }
  1547. /*
  1548. * Performs necessary checks before doing a write
  1549. *
  1550. * Can adjust writing position or amount of bytes to write.
  1551. * Returns appropriate error code that caller should return or
  1552. * zero in case that write should be allowed.
  1553. */
  1554. inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
  1555. {
  1556. struct inode *inode = file->f_mapping->host;
  1557. unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1558. if (unlikely(*pos < 0))
  1559. return -EINVAL;
  1560. if (!isblk) {
  1561. /* FIXME: this is for backwards compatibility with 2.4 */
  1562. if (file->f_flags & O_APPEND)
  1563. *pos = i_size_read(inode);
  1564. if (limit != RLIM_INFINITY) {
  1565. if (*pos >= limit) {
  1566. send_sig(SIGXFSZ, current, 0);
  1567. return -EFBIG;
  1568. }
  1569. if (*count > limit - (typeof(limit))*pos) {
  1570. *count = limit - (typeof(limit))*pos;
  1571. }
  1572. }
  1573. }
  1574. /*
  1575. * LFS rule
  1576. */
  1577. if (unlikely(*pos + *count > MAX_NON_LFS &&
  1578. !(file->f_flags & O_LARGEFILE))) {
  1579. if (*pos >= MAX_NON_LFS) {
  1580. return -EFBIG;
  1581. }
  1582. if (*count > MAX_NON_LFS - (unsigned long)*pos) {
  1583. *count = MAX_NON_LFS - (unsigned long)*pos;
  1584. }
  1585. }
  1586. /*
  1587. * Are we about to exceed the fs block limit ?
  1588. *
  1589. * If we have written data it becomes a short write. If we have
  1590. * exceeded without writing data we send a signal and return EFBIG.
  1591. * Linus frestrict idea will clean these up nicely..
  1592. */
  1593. if (likely(!isblk)) {
  1594. if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
  1595. if (*count || *pos > inode->i_sb->s_maxbytes) {
  1596. return -EFBIG;
  1597. }
  1598. /* zero-length writes at ->s_maxbytes are OK */
  1599. }
  1600. if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
  1601. *count = inode->i_sb->s_maxbytes - *pos;
  1602. } else {
  1603. #ifdef CONFIG_BLOCK
  1604. loff_t isize;
  1605. if (bdev_read_only(I_BDEV(inode)))
  1606. return -EPERM;
  1607. isize = i_size_read(inode);
  1608. if (*pos >= isize) {
  1609. if (*count || *pos > isize)
  1610. return -ENOSPC;
  1611. }
  1612. if (*pos + *count > isize)
  1613. *count = isize - *pos;
  1614. #else
  1615. return -EPERM;
  1616. #endif
  1617. }
  1618. return 0;
  1619. }
  1620. EXPORT_SYMBOL(generic_write_checks);
  1621. ssize_t
  1622. generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  1623. unsigned long *nr_segs, loff_t pos, loff_t *ppos,
  1624. size_t count, size_t ocount)
  1625. {
  1626. struct file *file = iocb->ki_filp;
  1627. struct address_space *mapping = file->f_mapping;
  1628. struct inode *inode = mapping->host;
  1629. ssize_t written;
  1630. if (count != ocount)
  1631. *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
  1632. written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
  1633. if (written > 0) {
  1634. loff_t end = pos + written;
  1635. if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
  1636. i_size_write(inode, end);
  1637. mark_inode_dirty(inode);
  1638. }
  1639. *ppos = end;
  1640. }
  1641. /*
  1642. * Sync the fs metadata but not the minor inode changes and
  1643. * of course not the data as we did direct DMA for the IO.
  1644. * i_mutex is held, which protects generic_osync_inode() from
  1645. * livelocking. AIO O_DIRECT ops attempt to sync metadata here.
  1646. */
  1647. if ((written >= 0 || written == -EIOCBQUEUED) &&
  1648. ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1649. int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
  1650. if (err < 0)
  1651. written = err;
  1652. }
  1653. return written;
  1654. }
  1655. EXPORT_SYMBOL(generic_file_direct_write);
  1656. ssize_t
  1657. generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
  1658. unsigned long nr_segs, loff_t pos, loff_t *ppos,
  1659. size_t count, ssize_t written)
  1660. {
  1661. struct file *file = iocb->ki_filp;
  1662. struct address_space * mapping = file->f_mapping;
  1663. const struct address_space_operations *a_ops = mapping->a_ops;
  1664. struct inode *inode = mapping->host;
  1665. long status = 0;
  1666. struct page *page;
  1667. struct page *cached_page = NULL;
  1668. size_t bytes;
  1669. struct pagevec lru_pvec;
  1670. const struct iovec *cur_iov = iov; /* current iovec */
  1671. size_t iov_base = 0; /* offset in the current iovec */
  1672. char __user *buf;
  1673. pagevec_init(&lru_pvec, 0);
  1674. /*
  1675. * handle partial DIO write. Adjust cur_iov if needed.
  1676. */
  1677. if (likely(nr_segs == 1))
  1678. buf = iov->iov_base + written;
  1679. else {
  1680. filemap_set_next_iovec(&cur_iov, &iov_base, written);
  1681. buf = cur_iov->iov_base + iov_base;
  1682. }
  1683. do {
  1684. unsigned long index;
  1685. unsigned long offset;
  1686. size_t copied;
  1687. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1688. index = pos >> PAGE_CACHE_SHIFT;
  1689. bytes = PAGE_CACHE_SIZE - offset;
  1690. /* Limit the size of the copy to the caller's write size */
  1691. bytes = min(bytes, count);
  1692. /* We only need to worry about prefaulting when writes are from
  1693. * user-space. NFSd uses vfs_writev with several non-aligned
  1694. * segments in the vector, and limiting to one segment a time is
  1695. * a noticeable performance for re-write
  1696. */
  1697. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1698. /*
  1699. * Limit the size of the copy to that of the current
  1700. * segment, because fault_in_pages_readable() doesn't
  1701. * know how to walk segments.
  1702. */
  1703. bytes = min(bytes, cur_iov->iov_len - iov_base);
  1704. /*
  1705. * Bring in the user page that we will copy from
  1706. * _first_. Otherwise there's a nasty deadlock on
  1707. * copying from the same page as we're writing to,
  1708. * without it being marked up-to-date.
  1709. */
  1710. fault_in_pages_readable(buf, bytes);
  1711. }
  1712. page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
  1713. if (!page) {
  1714. status = -ENOMEM;
  1715. break;
  1716. }
  1717. if (unlikely(bytes == 0)) {
  1718. status = 0;
  1719. copied = 0;
  1720. goto zero_length_segment;
  1721. }
  1722. status = a_ops->prepare_write(file, page, offset, offset+bytes);
  1723. if (unlikely(status)) {
  1724. loff_t isize = i_size_read(inode);
  1725. if (status != AOP_TRUNCATED_PAGE)
  1726. unlock_page(page);
  1727. page_cache_release(page);
  1728. if (status == AOP_TRUNCATED_PAGE)
  1729. continue;
  1730. /*
  1731. * prepare_write() may have instantiated a few blocks
  1732. * outside i_size. Trim these off again.
  1733. */
  1734. if (pos + bytes > isize)
  1735. vmtruncate(inode, isize);
  1736. break;
  1737. }
  1738. if (likely(nr_segs == 1))
  1739. copied = filemap_copy_from_user(page, offset,
  1740. buf, bytes);
  1741. else
  1742. copied = filemap_copy_from_user_iovec(page, offset,
  1743. cur_iov, iov_base, bytes);
  1744. flush_dcache_page(page);
  1745. status = a_ops->commit_write(file, page, offset, offset+bytes);
  1746. if (status == AOP_TRUNCATED_PAGE) {
  1747. page_cache_release(page);
  1748. continue;
  1749. }
  1750. zero_length_segment:
  1751. if (likely(copied >= 0)) {
  1752. if (!status)
  1753. status = copied;
  1754. if (status >= 0) {
  1755. written += status;
  1756. count -= status;
  1757. pos += status;
  1758. buf += status;
  1759. if (unlikely(nr_segs > 1)) {
  1760. filemap_set_next_iovec(&cur_iov,
  1761. &iov_base, status);
  1762. if (count)
  1763. buf = cur_iov->iov_base +
  1764. iov_base;
  1765. } else {
  1766. iov_base += status;
  1767. }
  1768. }
  1769. }
  1770. if (unlikely(copied != bytes))
  1771. if (status >= 0)
  1772. status = -EFAULT;
  1773. unlock_page(page);
  1774. mark_page_accessed(page);
  1775. page_cache_release(page);
  1776. if (status < 0)
  1777. break;
  1778. balance_dirty_pages_ratelimited(mapping);
  1779. cond_resched();
  1780. } while (count);
  1781. *ppos = pos;
  1782. if (cached_page)
  1783. page_cache_release(cached_page);
  1784. /*
  1785. * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
  1786. */
  1787. if (likely(status >= 0)) {
  1788. if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1789. if (!a_ops->writepage || !is_sync_kiocb(iocb))
  1790. status = generic_osync_inode(inode, mapping,
  1791. OSYNC_METADATA|OSYNC_DATA);
  1792. }
  1793. }
  1794. /*
  1795. * If we get here for O_DIRECT writes then we must have fallen through
  1796. * to buffered writes (block instantiation inside i_size). So we sync
  1797. * the file data here, to try to honour O_DIRECT expectations.
  1798. */
  1799. if (unlikely(file->f_flags & O_DIRECT) && written)
  1800. status = filemap_write_and_wait(mapping);
  1801. pagevec_lru_add(&lru_pvec);
  1802. return written ? written : status;
  1803. }
  1804. EXPORT_SYMBOL(generic_file_buffered_write);
  1805. static ssize_t
  1806. __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
  1807. unsigned long nr_segs, loff_t *ppos)
  1808. {
  1809. struct file *file = iocb->ki_filp;
  1810. struct address_space * mapping = file->f_mapping;
  1811. size_t ocount; /* original count */
  1812. size_t count; /* after file limit checks */
  1813. struct inode *inode = mapping->host;
  1814. loff_t pos;
  1815. ssize_t written;
  1816. ssize_t err;
  1817. ocount = 0;
  1818. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1819. if (err)
  1820. return err;
  1821. count = ocount;
  1822. pos = *ppos;
  1823. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1824. /* We can write back this queue in page reclaim */
  1825. current->backing_dev_info = mapping->backing_dev_info;
  1826. written = 0;
  1827. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1828. if (err)
  1829. goto out;
  1830. if (count == 0)
  1831. goto out;
  1832. err = remove_suid(file->f_path.dentry);
  1833. if (err)
  1834. goto out;
  1835. file_update_time(file);
  1836. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1837. if (unlikely(file->f_flags & O_DIRECT)) {
  1838. loff_t endbyte;
  1839. ssize_t written_buffered;
  1840. written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
  1841. ppos, count, ocount);
  1842. if (written < 0 || written == count)
  1843. goto out;
  1844. /*
  1845. * direct-io write to a hole: fall through to buffered I/O
  1846. * for completing the rest of the request.
  1847. */
  1848. pos += written;
  1849. count -= written;
  1850. written_buffered = generic_file_buffered_write(iocb, iov,
  1851. nr_segs, pos, ppos, count,
  1852. written);
  1853. /*
  1854. * If generic_file_buffered_write() retuned a synchronous error
  1855. * then we want to return the number of bytes which were
  1856. * direct-written, or the error code if that was zero. Note
  1857. * that this differs from normal direct-io semantics, which
  1858. * will return -EFOO even if some bytes were written.
  1859. */
  1860. if (written_buffered < 0) {
  1861. err = written_buffered;
  1862. goto out;
  1863. }
  1864. /*
  1865. * We need to ensure that the page cache pages are written to
  1866. * disk and invalidated to preserve the expected O_DIRECT
  1867. * semantics.
  1868. */
  1869. endbyte = pos + written_buffered - written - 1;
  1870. err = do_sync_mapping_range(file->f_mapping, pos, endbyte,
  1871. SYNC_FILE_RANGE_WAIT_BEFORE|
  1872. SYNC_FILE_RANGE_WRITE|
  1873. SYNC_FILE_RANGE_WAIT_AFTER);
  1874. if (err == 0) {
  1875. written = written_buffered;
  1876. invalidate_mapping_pages(mapping,
  1877. pos >> PAGE_CACHE_SHIFT,
  1878. endbyte >> PAGE_CACHE_SHIFT);
  1879. } else {
  1880. /*
  1881. * We don't know how much we wrote, so just return
  1882. * the number of bytes which were direct-written
  1883. */
  1884. }
  1885. } else {
  1886. written = generic_file_buffered_write(iocb, iov, nr_segs,
  1887. pos, ppos, count, written);
  1888. }
  1889. out:
  1890. current->backing_dev_info = NULL;
  1891. return written ? written : err;
  1892. }
  1893. ssize_t generic_file_aio_write_nolock(struct kiocb *iocb,
  1894. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1895. {
  1896. struct file *file = iocb->ki_filp;
  1897. struct address_space *mapping = file->f_mapping;
  1898. struct inode *inode = mapping->host;
  1899. ssize_t ret;
  1900. BUG_ON(iocb->ki_pos != pos);
  1901. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1902. &iocb->ki_pos);
  1903. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1904. ssize_t err;
  1905. err = sync_page_range_nolock(inode, mapping, pos, ret);
  1906. if (err < 0)
  1907. ret = err;
  1908. }
  1909. return ret;
  1910. }
  1911. EXPORT_SYMBOL(generic_file_aio_write_nolock);
  1912. ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1913. unsigned long nr_segs, loff_t pos)
  1914. {
  1915. struct file *file = iocb->ki_filp;
  1916. struct address_space *mapping = file->f_mapping;
  1917. struct inode *inode = mapping->host;
  1918. ssize_t ret;
  1919. BUG_ON(iocb->ki_pos != pos);
  1920. mutex_lock(&inode->i_mutex);
  1921. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1922. &iocb->ki_pos);
  1923. mutex_unlock(&inode->i_mutex);
  1924. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1925. ssize_t err;
  1926. err = sync_page_range(inode, mapping, pos, ret);
  1927. if (err < 0)
  1928. ret = err;
  1929. }
  1930. return ret;
  1931. }
  1932. EXPORT_SYMBOL(generic_file_aio_write);
  1933. /*
  1934. * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
  1935. * went wrong during pagecache shootdown.
  1936. */
  1937. static ssize_t
  1938. generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  1939. loff_t offset, unsigned long nr_segs)
  1940. {
  1941. struct file *file = iocb->ki_filp;
  1942. struct address_space *mapping = file->f_mapping;
  1943. ssize_t retval;
  1944. size_t write_len;
  1945. pgoff_t end = 0; /* silence gcc */
  1946. /*
  1947. * If it's a write, unmap all mmappings of the file up-front. This
  1948. * will cause any pte dirty bits to be propagated into the pageframes
  1949. * for the subsequent filemap_write_and_wait().
  1950. */
  1951. if (rw == WRITE) {
  1952. write_len = iov_length(iov, nr_segs);
  1953. end = (offset + write_len - 1) >> PAGE_CACHE_SHIFT;
  1954. if (mapping_mapped(mapping))
  1955. unmap_mapping_range(mapping, offset, write_len, 0);
  1956. }
  1957. retval = filemap_write_and_wait(mapping);
  1958. if (retval)
  1959. goto out;
  1960. /*
  1961. * After a write we want buffered reads to be sure to go to disk to get
  1962. * the new data. We invalidate clean cached page from the region we're
  1963. * about to write. We do this *before* the write so that we can return
  1964. * -EIO without clobbering -EIOCBQUEUED from ->direct_IO().
  1965. */
  1966. if (rw == WRITE && mapping->nrpages) {
  1967. retval = invalidate_inode_pages2_range(mapping,
  1968. offset >> PAGE_CACHE_SHIFT, end);
  1969. if (retval)
  1970. goto out;
  1971. }
  1972. retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
  1973. if (retval)
  1974. goto out;
  1975. /*
  1976. * Finally, try again to invalidate clean pages which might have been
  1977. * faulted in by get_user_pages() if the source of the write was an
  1978. * mmap()ed region of the file we're writing. That's a pretty crazy
  1979. * thing to do, so we don't support it 100%. If this invalidation
  1980. * fails and we have -EIOCBQUEUED we ignore the failure.
  1981. */
  1982. if (rw == WRITE && mapping->nrpages) {
  1983. int err = invalidate_inode_pages2_range(mapping,
  1984. offset >> PAGE_CACHE_SHIFT, end);
  1985. if (err && retval >= 0)
  1986. retval = err;
  1987. }
  1988. out:
  1989. return retval;
  1990. }
  1991. /**
  1992. * try_to_release_page() - release old fs-specific metadata on a page
  1993. *
  1994. * @page: the page which the kernel is trying to free
  1995. * @gfp_mask: memory allocation flags (and I/O mode)
  1996. *
  1997. * The address_space is to try to release any data against the page
  1998. * (presumably at page->private). If the release was successful, return `1'.
  1999. * Otherwise return zero.
  2000. *
  2001. * The @gfp_mask argument specifies whether I/O may be performed to release
  2002. * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
  2003. *
  2004. * NOTE: @gfp_mask may go away, and this function may become non-blocking.
  2005. */
  2006. int try_to_release_page(struct page *page, gfp_t gfp_mask)
  2007. {
  2008. struct address_space * const mapping = page->mapping;
  2009. BUG_ON(!PageLocked(page));
  2010. if (PageWriteback(page))
  2011. return 0;
  2012. if (mapping && mapping->a_ops->releasepage)
  2013. return mapping->a_ops->releasepage(page, gfp_mask);
  2014. return try_to_free_buffers(page);
  2015. }
  2016. EXPORT_SYMBOL(try_to_release_page);