filemap.c 58 KB

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