filemap.c 59 KB

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