filemap.c 63 KB

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