filemap.c 58 KB

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