filemap.c 56 KB

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