filemap.c 63 KB

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