filemap.c 58 KB

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