filemap.c 57 KB

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