filemap.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  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_lock_page - locate, pin and lock a pagecache page
  552. * @mapping: the address_space to search
  553. * @offset: the page index
  554. *
  555. * Locates the desired pagecache page, locks it, increments its reference
  556. * count and returns its address.
  557. *
  558. * Returns zero if the page was not present. find_lock_page() may sleep.
  559. */
  560. struct page *find_lock_page(struct address_space *mapping,
  561. unsigned long offset)
  562. {
  563. struct page *page;
  564. read_lock_irq(&mapping->tree_lock);
  565. repeat:
  566. page = radix_tree_lookup(&mapping->page_tree, offset);
  567. if (page) {
  568. page_cache_get(page);
  569. if (TestSetPageLocked(page)) {
  570. read_unlock_irq(&mapping->tree_lock);
  571. __lock_page(page);
  572. read_lock_irq(&mapping->tree_lock);
  573. /* Has the page been truncated while we slept? */
  574. if (unlikely(page->mapping != mapping ||
  575. page->index != offset)) {
  576. unlock_page(page);
  577. page_cache_release(page);
  578. goto repeat;
  579. }
  580. }
  581. }
  582. read_unlock_irq(&mapping->tree_lock);
  583. return page;
  584. }
  585. EXPORT_SYMBOL(find_lock_page);
  586. /**
  587. * find_or_create_page - locate or add a pagecache page
  588. * @mapping: the page's address_space
  589. * @index: the page's index into the mapping
  590. * @gfp_mask: page allocation mode
  591. *
  592. * Locates a page in the pagecache. If the page is not present, a new page
  593. * is allocated using @gfp_mask and is added to the pagecache and to the VM's
  594. * LRU list. The returned page is locked and has its reference count
  595. * incremented.
  596. *
  597. * find_or_create_page() may sleep, even if @gfp_flags specifies an atomic
  598. * allocation!
  599. *
  600. * find_or_create_page() returns the desired page's address, or zero on
  601. * memory exhaustion.
  602. */
  603. struct page *find_or_create_page(struct address_space *mapping,
  604. unsigned long index, gfp_t gfp_mask)
  605. {
  606. struct page *page, *cached_page = NULL;
  607. int err;
  608. repeat:
  609. page = find_lock_page(mapping, index);
  610. if (!page) {
  611. if (!cached_page) {
  612. cached_page =
  613. __page_cache_alloc(gfp_mask);
  614. if (!cached_page)
  615. return NULL;
  616. }
  617. err = add_to_page_cache_lru(cached_page, mapping,
  618. index, gfp_mask);
  619. if (!err) {
  620. page = cached_page;
  621. cached_page = NULL;
  622. } else if (err == -EEXIST)
  623. goto repeat;
  624. }
  625. if (cached_page)
  626. page_cache_release(cached_page);
  627. return page;
  628. }
  629. EXPORT_SYMBOL(find_or_create_page);
  630. /**
  631. * find_get_pages - gang pagecache lookup
  632. * @mapping: The address_space to search
  633. * @start: The starting page index
  634. * @nr_pages: The maximum number of pages
  635. * @pages: Where the resulting pages are placed
  636. *
  637. * find_get_pages() will search for and return a group of up to
  638. * @nr_pages pages in the mapping. The pages are placed at @pages.
  639. * find_get_pages() takes a reference against the returned pages.
  640. *
  641. * The search returns a group of mapping-contiguous pages with ascending
  642. * indexes. There may be holes in the indices due to not-present pages.
  643. *
  644. * find_get_pages() returns the number of pages which were found.
  645. */
  646. unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
  647. unsigned int nr_pages, struct page **pages)
  648. {
  649. unsigned int i;
  650. unsigned int ret;
  651. read_lock_irq(&mapping->tree_lock);
  652. ret = radix_tree_gang_lookup(&mapping->page_tree,
  653. (void **)pages, start, nr_pages);
  654. for (i = 0; i < ret; i++)
  655. page_cache_get(pages[i]);
  656. read_unlock_irq(&mapping->tree_lock);
  657. return ret;
  658. }
  659. /**
  660. * find_get_pages_contig - gang contiguous pagecache lookup
  661. * @mapping: The address_space to search
  662. * @index: The starting page index
  663. * @nr_pages: The maximum number of pages
  664. * @pages: Where the resulting pages are placed
  665. *
  666. * find_get_pages_contig() works exactly like find_get_pages(), except
  667. * that the returned number of pages are guaranteed to be contiguous.
  668. *
  669. * find_get_pages_contig() returns the number of pages which were found.
  670. */
  671. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
  672. unsigned int nr_pages, struct page **pages)
  673. {
  674. unsigned int i;
  675. unsigned int ret;
  676. read_lock_irq(&mapping->tree_lock);
  677. ret = radix_tree_gang_lookup(&mapping->page_tree,
  678. (void **)pages, index, nr_pages);
  679. for (i = 0; i < ret; i++) {
  680. if (pages[i]->mapping == NULL || pages[i]->index != index)
  681. break;
  682. page_cache_get(pages[i]);
  683. index++;
  684. }
  685. read_unlock_irq(&mapping->tree_lock);
  686. return i;
  687. }
  688. EXPORT_SYMBOL(find_get_pages_contig);
  689. /**
  690. * find_get_pages_tag - find and return pages that match @tag
  691. * @mapping: the address_space to search
  692. * @index: the starting page index
  693. * @tag: the tag index
  694. * @nr_pages: the maximum number of pages
  695. * @pages: where the resulting pages are placed
  696. *
  697. * Like find_get_pages, except we only return pages which are tagged with
  698. * @tag. We update @index to index the next page for the traversal.
  699. */
  700. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  701. int tag, unsigned int nr_pages, struct page **pages)
  702. {
  703. unsigned int i;
  704. unsigned int ret;
  705. read_lock_irq(&mapping->tree_lock);
  706. ret = radix_tree_gang_lookup_tag(&mapping->page_tree,
  707. (void **)pages, *index, nr_pages, tag);
  708. for (i = 0; i < ret; i++)
  709. page_cache_get(pages[i]);
  710. if (ret)
  711. *index = pages[ret - 1]->index + 1;
  712. read_unlock_irq(&mapping->tree_lock);
  713. return ret;
  714. }
  715. EXPORT_SYMBOL(find_get_pages_tag);
  716. /**
  717. * grab_cache_page_nowait - returns locked page at given index in given cache
  718. * @mapping: target address_space
  719. * @index: the page index
  720. *
  721. * Same as grab_cache_page(), but do not wait if the page is unavailable.
  722. * This is intended for speculative data generators, where the data can
  723. * be regenerated if the page couldn't be grabbed. This routine should
  724. * be safe to call while holding the lock for another page.
  725. *
  726. * Clear __GFP_FS when allocating the page to avoid recursion into the fs
  727. * and deadlock against the caller's locked page.
  728. */
  729. struct page *
  730. grab_cache_page_nowait(struct address_space *mapping, unsigned long index)
  731. {
  732. struct page *page = find_get_page(mapping, index);
  733. if (page) {
  734. if (!TestSetPageLocked(page))
  735. return page;
  736. page_cache_release(page);
  737. return NULL;
  738. }
  739. page = __page_cache_alloc(mapping_gfp_mask(mapping) & ~__GFP_FS);
  740. if (page && add_to_page_cache_lru(page, mapping, index, GFP_KERNEL)) {
  741. page_cache_release(page);
  742. page = NULL;
  743. }
  744. return page;
  745. }
  746. EXPORT_SYMBOL(grab_cache_page_nowait);
  747. /*
  748. * CD/DVDs are error prone. When a medium error occurs, the driver may fail
  749. * a _large_ part of the i/o request. Imagine the worst scenario:
  750. *
  751. * ---R__________________________________________B__________
  752. * ^ reading here ^ bad block(assume 4k)
  753. *
  754. * read(R) => miss => readahead(R...B) => media error => frustrating retries
  755. * => failing the whole request => read(R) => read(R+1) =>
  756. * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
  757. * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
  758. * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
  759. *
  760. * It is going insane. Fix it by quickly scaling down the readahead size.
  761. */
  762. static void shrink_readahead_size_eio(struct file *filp,
  763. struct file_ra_state *ra)
  764. {
  765. if (!ra->ra_pages)
  766. return;
  767. ra->ra_pages /= 4;
  768. }
  769. /**
  770. * do_generic_mapping_read - generic file read routine
  771. * @mapping: address_space to be read
  772. * @_ra: file's readahead state
  773. * @filp: the file to read
  774. * @ppos: current file position
  775. * @desc: read_descriptor
  776. * @actor: read method
  777. *
  778. * This is a generic file read routine, and uses the
  779. * mapping->a_ops->readpage() function for the actual low-level stuff.
  780. *
  781. * This is really ugly. But the goto's actually try to clarify some
  782. * of the logic when it comes to error handling etc.
  783. *
  784. * Note the struct file* is only passed for the use of readpage.
  785. * It may be NULL.
  786. */
  787. void do_generic_mapping_read(struct address_space *mapping,
  788. struct file_ra_state *_ra,
  789. struct file *filp,
  790. loff_t *ppos,
  791. read_descriptor_t *desc,
  792. read_actor_t actor)
  793. {
  794. struct inode *inode = mapping->host;
  795. unsigned long index;
  796. unsigned long end_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. loff_t isize;
  803. struct page *cached_page;
  804. int error;
  805. struct file_ra_state ra = *_ra;
  806. cached_page = NULL;
  807. index = *ppos >> PAGE_CACHE_SHIFT;
  808. next_index = index;
  809. prev_index = ra.prev_index;
  810. prev_offset = ra.prev_offset;
  811. last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  812. offset = *ppos & ~PAGE_CACHE_MASK;
  813. isize = i_size_read(inode);
  814. if (!isize)
  815. goto out;
  816. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  817. for (;;) {
  818. struct page *page;
  819. unsigned long nr, ret;
  820. /* nr is the maximum number of bytes to copy from this page */
  821. nr = PAGE_CACHE_SIZE;
  822. if (index >= end_index) {
  823. if (index > end_index)
  824. goto out;
  825. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  826. if (nr <= offset) {
  827. goto out;
  828. }
  829. }
  830. nr = nr - offset;
  831. cond_resched();
  832. if (index == next_index)
  833. next_index = page_cache_readahead(mapping, &ra, filp,
  834. index, last_index - index);
  835. find_page:
  836. page = find_get_page(mapping, index);
  837. if (unlikely(page == NULL)) {
  838. handle_ra_miss(mapping, &ra, index);
  839. goto no_cached_page;
  840. }
  841. if (!PageUptodate(page))
  842. goto page_not_up_to_date;
  843. page_ok:
  844. /* If users can be writing to this page using arbitrary
  845. * virtual addresses, take care about potential aliasing
  846. * before reading the page on the kernel side.
  847. */
  848. if (mapping_writably_mapped(mapping))
  849. flush_dcache_page(page);
  850. /*
  851. * When a sequential read accesses a page several times,
  852. * only mark it as accessed the first time.
  853. */
  854. if (prev_index != index || offset != prev_offset)
  855. mark_page_accessed(page);
  856. prev_index = index;
  857. /*
  858. * Ok, we have the page, and it's up-to-date, so
  859. * now we can copy it to user space...
  860. *
  861. * The actor routine returns how many bytes were actually used..
  862. * NOTE! This may not be the same as how much of a user buffer
  863. * we filled up (we may be padding etc), so we can only update
  864. * "pos" here (the actor routine has to update the user buffer
  865. * pointers and the remaining count).
  866. */
  867. ret = actor(desc, page, offset, nr);
  868. offset += ret;
  869. index += offset >> PAGE_CACHE_SHIFT;
  870. offset &= ~PAGE_CACHE_MASK;
  871. prev_offset = offset;
  872. ra.prev_offset = offset;
  873. page_cache_release(page);
  874. if (ret == nr && desc->count)
  875. continue;
  876. goto out;
  877. page_not_up_to_date:
  878. /* Get exclusive access to the page ... */
  879. lock_page(page);
  880. /* Did it get truncated before we got the lock? */
  881. if (!page->mapping) {
  882. unlock_page(page);
  883. page_cache_release(page);
  884. continue;
  885. }
  886. /* Did somebody else fill it already? */
  887. if (PageUptodate(page)) {
  888. unlock_page(page);
  889. goto page_ok;
  890. }
  891. readpage:
  892. /* Start the actual read. The read will unlock the page. */
  893. error = mapping->a_ops->readpage(filp, page);
  894. if (unlikely(error)) {
  895. if (error == AOP_TRUNCATED_PAGE) {
  896. page_cache_release(page);
  897. goto find_page;
  898. }
  899. goto readpage_error;
  900. }
  901. if (!PageUptodate(page)) {
  902. lock_page(page);
  903. if (!PageUptodate(page)) {
  904. if (page->mapping == NULL) {
  905. /*
  906. * invalidate_inode_pages got it
  907. */
  908. unlock_page(page);
  909. page_cache_release(page);
  910. goto find_page;
  911. }
  912. unlock_page(page);
  913. error = -EIO;
  914. shrink_readahead_size_eio(filp, &ra);
  915. goto readpage_error;
  916. }
  917. unlock_page(page);
  918. }
  919. /*
  920. * i_size must be checked after we have done ->readpage.
  921. *
  922. * Checking i_size after the readpage allows us to calculate
  923. * the correct value for "nr", which means the zero-filled
  924. * part of the page is not copied back to userspace (unless
  925. * another truncate extends the file - this is desired though).
  926. */
  927. isize = i_size_read(inode);
  928. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  929. if (unlikely(!isize || index > end_index)) {
  930. page_cache_release(page);
  931. goto out;
  932. }
  933. /* nr is the maximum number of bytes to copy from this page */
  934. nr = PAGE_CACHE_SIZE;
  935. if (index == end_index) {
  936. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  937. if (nr <= offset) {
  938. page_cache_release(page);
  939. goto out;
  940. }
  941. }
  942. nr = nr - offset;
  943. goto page_ok;
  944. readpage_error:
  945. /* UHHUH! A synchronous read error occurred. Report it */
  946. desc->error = error;
  947. page_cache_release(page);
  948. goto out;
  949. no_cached_page:
  950. /*
  951. * Ok, it wasn't cached, so we need to create a new
  952. * page..
  953. */
  954. if (!cached_page) {
  955. cached_page = page_cache_alloc_cold(mapping);
  956. if (!cached_page) {
  957. desc->error = -ENOMEM;
  958. goto out;
  959. }
  960. }
  961. error = add_to_page_cache_lru(cached_page, mapping,
  962. index, GFP_KERNEL);
  963. if (error) {
  964. if (error == -EEXIST)
  965. goto find_page;
  966. desc->error = error;
  967. goto out;
  968. }
  969. page = cached_page;
  970. cached_page = NULL;
  971. goto readpage;
  972. }
  973. out:
  974. *_ra = ra;
  975. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  976. if (cached_page)
  977. page_cache_release(cached_page);
  978. if (filp)
  979. file_accessed(filp);
  980. }
  981. EXPORT_SYMBOL(do_generic_mapping_read);
  982. int file_read_actor(read_descriptor_t *desc, struct page *page,
  983. unsigned long offset, unsigned long size)
  984. {
  985. char *kaddr;
  986. unsigned long left, count = desc->count;
  987. if (size > count)
  988. size = count;
  989. /*
  990. * Faults on the destination of a read are common, so do it before
  991. * taking the kmap.
  992. */
  993. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  994. kaddr = kmap_atomic(page, KM_USER0);
  995. left = __copy_to_user_inatomic(desc->arg.buf,
  996. kaddr + offset, size);
  997. kunmap_atomic(kaddr, KM_USER0);
  998. if (left == 0)
  999. goto success;
  1000. }
  1001. /* Do it the slow way */
  1002. kaddr = kmap(page);
  1003. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1004. kunmap(page);
  1005. if (left) {
  1006. size -= left;
  1007. desc->error = -EFAULT;
  1008. }
  1009. success:
  1010. desc->count = count - size;
  1011. desc->written += size;
  1012. desc->arg.buf += size;
  1013. return size;
  1014. }
  1015. /*
  1016. * Performs necessary checks before doing a write
  1017. * @iov: io vector request
  1018. * @nr_segs: number of segments in the iovec
  1019. * @count: number of bytes to write
  1020. * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
  1021. *
  1022. * Adjust number of segments and amount of bytes to write (nr_segs should be
  1023. * properly initialized first). Returns appropriate error code that caller
  1024. * should return or zero in case that write should be allowed.
  1025. */
  1026. int generic_segment_checks(const struct iovec *iov,
  1027. unsigned long *nr_segs, size_t *count, int access_flags)
  1028. {
  1029. unsigned long seg;
  1030. size_t cnt = 0;
  1031. for (seg = 0; seg < *nr_segs; seg++) {
  1032. const struct iovec *iv = &iov[seg];
  1033. /*
  1034. * If any segment has a negative length, or the cumulative
  1035. * length ever wraps negative then return -EINVAL.
  1036. */
  1037. cnt += iv->iov_len;
  1038. if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
  1039. return -EINVAL;
  1040. if (access_ok(access_flags, iv->iov_base, iv->iov_len))
  1041. continue;
  1042. if (seg == 0)
  1043. return -EFAULT;
  1044. *nr_segs = seg;
  1045. cnt -= iv->iov_len; /* This segment is no good */
  1046. break;
  1047. }
  1048. *count = cnt;
  1049. return 0;
  1050. }
  1051. EXPORT_SYMBOL(generic_segment_checks);
  1052. /**
  1053. * generic_file_aio_read - generic filesystem read routine
  1054. * @iocb: kernel I/O control block
  1055. * @iov: io vector request
  1056. * @nr_segs: number of segments in the iovec
  1057. * @pos: current file position
  1058. *
  1059. * This is the "read()" routine for all filesystems
  1060. * that can use the page cache directly.
  1061. */
  1062. ssize_t
  1063. generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1064. unsigned long nr_segs, loff_t pos)
  1065. {
  1066. struct file *filp = iocb->ki_filp;
  1067. ssize_t retval;
  1068. unsigned long seg;
  1069. size_t count;
  1070. loff_t *ppos = &iocb->ki_pos;
  1071. count = 0;
  1072. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1073. if (retval)
  1074. return retval;
  1075. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1076. if (filp->f_flags & O_DIRECT) {
  1077. loff_t size;
  1078. struct address_space *mapping;
  1079. struct inode *inode;
  1080. mapping = filp->f_mapping;
  1081. inode = mapping->host;
  1082. retval = 0;
  1083. if (!count)
  1084. goto out; /* skip atime */
  1085. size = i_size_read(inode);
  1086. if (pos < size) {
  1087. retval = generic_file_direct_IO(READ, iocb,
  1088. iov, pos, nr_segs);
  1089. if (retval > 0)
  1090. *ppos = pos + retval;
  1091. }
  1092. if (likely(retval != 0)) {
  1093. file_accessed(filp);
  1094. goto out;
  1095. }
  1096. }
  1097. retval = 0;
  1098. if (count) {
  1099. for (seg = 0; seg < nr_segs; seg++) {
  1100. read_descriptor_t desc;
  1101. desc.written = 0;
  1102. desc.arg.buf = iov[seg].iov_base;
  1103. desc.count = iov[seg].iov_len;
  1104. if (desc.count == 0)
  1105. continue;
  1106. desc.error = 0;
  1107. do_generic_file_read(filp,ppos,&desc,file_read_actor);
  1108. retval += desc.written;
  1109. if (desc.error) {
  1110. retval = retval ?: desc.error;
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. out:
  1116. return retval;
  1117. }
  1118. EXPORT_SYMBOL(generic_file_aio_read);
  1119. int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
  1120. {
  1121. ssize_t written;
  1122. unsigned long count = desc->count;
  1123. struct file *file = desc->arg.data;
  1124. if (size > count)
  1125. size = count;
  1126. written = file->f_op->sendpage(file, page, offset,
  1127. size, &file->f_pos, size<count);
  1128. if (written < 0) {
  1129. desc->error = written;
  1130. written = 0;
  1131. }
  1132. desc->count = count - written;
  1133. desc->written += written;
  1134. return written;
  1135. }
  1136. ssize_t generic_file_sendfile(struct file *in_file, loff_t *ppos,
  1137. size_t count, read_actor_t actor, void *target)
  1138. {
  1139. read_descriptor_t desc;
  1140. if (!count)
  1141. return 0;
  1142. desc.written = 0;
  1143. desc.count = count;
  1144. desc.arg.data = target;
  1145. desc.error = 0;
  1146. do_generic_file_read(in_file, ppos, &desc, actor);
  1147. if (desc.written)
  1148. return desc.written;
  1149. return desc.error;
  1150. }
  1151. EXPORT_SYMBOL(generic_file_sendfile);
  1152. static ssize_t
  1153. do_readahead(struct address_space *mapping, struct file *filp,
  1154. unsigned long index, unsigned long nr)
  1155. {
  1156. if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
  1157. return -EINVAL;
  1158. force_page_cache_readahead(mapping, filp, index,
  1159. max_sane_readahead(nr));
  1160. return 0;
  1161. }
  1162. asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
  1163. {
  1164. ssize_t ret;
  1165. struct file *file;
  1166. ret = -EBADF;
  1167. file = fget(fd);
  1168. if (file) {
  1169. if (file->f_mode & FMODE_READ) {
  1170. struct address_space *mapping = file->f_mapping;
  1171. unsigned long start = offset >> PAGE_CACHE_SHIFT;
  1172. unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  1173. unsigned long len = end - start + 1;
  1174. ret = do_readahead(mapping, file, start, len);
  1175. }
  1176. fput(file);
  1177. }
  1178. return ret;
  1179. }
  1180. #ifdef CONFIG_MMU
  1181. static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
  1182. /**
  1183. * page_cache_read - adds requested page to the page cache if not already there
  1184. * @file: file to read
  1185. * @offset: page index
  1186. *
  1187. * This adds the requested page to the page cache if it isn't already there,
  1188. * and schedules an I/O to read in its contents from disk.
  1189. */
  1190. static int fastcall page_cache_read(struct file * file, unsigned long offset)
  1191. {
  1192. struct address_space *mapping = file->f_mapping;
  1193. struct page *page;
  1194. int ret;
  1195. do {
  1196. page = page_cache_alloc_cold(mapping);
  1197. if (!page)
  1198. return -ENOMEM;
  1199. ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
  1200. if (ret == 0)
  1201. ret = mapping->a_ops->readpage(file, page);
  1202. else if (ret == -EEXIST)
  1203. ret = 0; /* losing race to add is OK */
  1204. page_cache_release(page);
  1205. } while (ret == AOP_TRUNCATED_PAGE);
  1206. return ret;
  1207. }
  1208. #define MMAP_LOTSAMISS (100)
  1209. /**
  1210. * filemap_nopage - read in file data for page fault handling
  1211. * @area: the applicable vm_area
  1212. * @address: target address to read in
  1213. * @type: returned with VM_FAULT_{MINOR,MAJOR} if not %NULL
  1214. *
  1215. * filemap_nopage() is invoked via the vma operations vector for a
  1216. * mapped memory region to read in file data during a page fault.
  1217. *
  1218. * The goto's are kind of ugly, but this streamlines the normal case of having
  1219. * it in the page cache, and handles the special cases reasonably without
  1220. * having a lot of duplicated code.
  1221. */
  1222. struct page *filemap_nopage(struct vm_area_struct *area,
  1223. unsigned long address, int *type)
  1224. {
  1225. int error;
  1226. struct file *file = area->vm_file;
  1227. struct address_space *mapping = file->f_mapping;
  1228. struct file_ra_state *ra = &file->f_ra;
  1229. struct inode *inode = mapping->host;
  1230. struct page *page;
  1231. unsigned long size, pgoff;
  1232. int did_readaround = 0, majmin = VM_FAULT_MINOR;
  1233. pgoff = ((address-area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff;
  1234. retry_all:
  1235. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1236. if (pgoff >= size)
  1237. goto outside_data_content;
  1238. /* If we don't want any read-ahead, don't bother */
  1239. if (VM_RandomReadHint(area))
  1240. goto no_cached_page;
  1241. /*
  1242. * The readahead code wants to be told about each and every page
  1243. * so it can build and shrink its windows appropriately
  1244. *
  1245. * For sequential accesses, we use the generic readahead logic.
  1246. */
  1247. if (VM_SequentialReadHint(area))
  1248. page_cache_readahead(mapping, ra, file, pgoff, 1);
  1249. /*
  1250. * Do we have something in the page cache already?
  1251. */
  1252. retry_find:
  1253. page = find_get_page(mapping, pgoff);
  1254. if (!page) {
  1255. unsigned long ra_pages;
  1256. if (VM_SequentialReadHint(area)) {
  1257. handle_ra_miss(mapping, ra, pgoff);
  1258. goto no_cached_page;
  1259. }
  1260. ra->mmap_miss++;
  1261. /*
  1262. * Do we miss much more than hit in this file? If so,
  1263. * stop bothering with read-ahead. It will only hurt.
  1264. */
  1265. if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
  1266. goto no_cached_page;
  1267. /*
  1268. * To keep the pgmajfault counter straight, we need to
  1269. * check did_readaround, as this is an inner loop.
  1270. */
  1271. if (!did_readaround) {
  1272. majmin = VM_FAULT_MAJOR;
  1273. count_vm_event(PGMAJFAULT);
  1274. }
  1275. did_readaround = 1;
  1276. ra_pages = max_sane_readahead(file->f_ra.ra_pages);
  1277. if (ra_pages) {
  1278. pgoff_t start = 0;
  1279. if (pgoff > ra_pages / 2)
  1280. start = pgoff - ra_pages / 2;
  1281. do_page_cache_readahead(mapping, file, start, ra_pages);
  1282. }
  1283. page = find_get_page(mapping, pgoff);
  1284. if (!page)
  1285. goto no_cached_page;
  1286. }
  1287. if (!did_readaround)
  1288. ra->mmap_hit++;
  1289. /*
  1290. * Ok, found a page in the page cache, now we need to check
  1291. * that it's up-to-date.
  1292. */
  1293. if (!PageUptodate(page))
  1294. goto page_not_uptodate;
  1295. success:
  1296. /*
  1297. * Found the page and have a reference on it.
  1298. */
  1299. mark_page_accessed(page);
  1300. if (type)
  1301. *type = majmin;
  1302. return page;
  1303. outside_data_content:
  1304. /*
  1305. * An external ptracer can access pages that normally aren't
  1306. * accessible..
  1307. */
  1308. if (area->vm_mm == current->mm)
  1309. return NOPAGE_SIGBUS;
  1310. /* Fall through to the non-read-ahead case */
  1311. no_cached_page:
  1312. /*
  1313. * We're only likely to ever get here if MADV_RANDOM is in
  1314. * effect.
  1315. */
  1316. error = page_cache_read(file, pgoff);
  1317. /*
  1318. * The page we want has now been added to the page cache.
  1319. * In the unlikely event that someone removed it in the
  1320. * meantime, we'll just come back here and read it again.
  1321. */
  1322. if (error >= 0)
  1323. goto retry_find;
  1324. /*
  1325. * An error return from page_cache_read can result if the
  1326. * system is low on memory, or a problem occurs while trying
  1327. * to schedule I/O.
  1328. */
  1329. if (error == -ENOMEM)
  1330. return NOPAGE_OOM;
  1331. return NOPAGE_SIGBUS;
  1332. page_not_uptodate:
  1333. if (!did_readaround) {
  1334. majmin = VM_FAULT_MAJOR;
  1335. count_vm_event(PGMAJFAULT);
  1336. }
  1337. /*
  1338. * Umm, take care of errors if the page isn't up-to-date.
  1339. * Try to re-read it _once_. We do this synchronously,
  1340. * because there really aren't any performance issues here
  1341. * and we need to check for errors.
  1342. */
  1343. lock_page(page);
  1344. /* Somebody truncated the page on us? */
  1345. if (!page->mapping) {
  1346. unlock_page(page);
  1347. page_cache_release(page);
  1348. goto retry_all;
  1349. }
  1350. /* Somebody else successfully read it in? */
  1351. if (PageUptodate(page)) {
  1352. unlock_page(page);
  1353. goto success;
  1354. }
  1355. ClearPageError(page);
  1356. error = mapping->a_ops->readpage(file, page);
  1357. if (!error) {
  1358. wait_on_page_locked(page);
  1359. if (PageUptodate(page))
  1360. goto success;
  1361. } else if (error == AOP_TRUNCATED_PAGE) {
  1362. page_cache_release(page);
  1363. goto retry_find;
  1364. }
  1365. /*
  1366. * Things didn't work out. Return zero to tell the
  1367. * mm layer so, possibly freeing the page cache page first.
  1368. */
  1369. shrink_readahead_size_eio(file, ra);
  1370. page_cache_release(page);
  1371. return NOPAGE_SIGBUS;
  1372. }
  1373. EXPORT_SYMBOL(filemap_nopage);
  1374. static struct page * filemap_getpage(struct file *file, unsigned long pgoff,
  1375. int nonblock)
  1376. {
  1377. struct address_space *mapping = file->f_mapping;
  1378. struct page *page;
  1379. int error;
  1380. /*
  1381. * Do we have something in the page cache already?
  1382. */
  1383. retry_find:
  1384. page = find_get_page(mapping, pgoff);
  1385. if (!page) {
  1386. if (nonblock)
  1387. return NULL;
  1388. goto no_cached_page;
  1389. }
  1390. /*
  1391. * Ok, found a page in the page cache, now we need to check
  1392. * that it's up-to-date.
  1393. */
  1394. if (!PageUptodate(page)) {
  1395. if (nonblock) {
  1396. page_cache_release(page);
  1397. return NULL;
  1398. }
  1399. goto page_not_uptodate;
  1400. }
  1401. success:
  1402. /*
  1403. * Found the page and have a reference on it.
  1404. */
  1405. mark_page_accessed(page);
  1406. return page;
  1407. no_cached_page:
  1408. error = page_cache_read(file, pgoff);
  1409. /*
  1410. * The page we want has now been added to the page cache.
  1411. * In the unlikely event that someone removed it in the
  1412. * meantime, we'll just come back here and read it again.
  1413. */
  1414. if (error >= 0)
  1415. goto retry_find;
  1416. /*
  1417. * An error return from page_cache_read can result if the
  1418. * system is low on memory, or a problem occurs while trying
  1419. * to schedule I/O.
  1420. */
  1421. return NULL;
  1422. page_not_uptodate:
  1423. lock_page(page);
  1424. /* Did it get truncated while we waited for it? */
  1425. if (!page->mapping) {
  1426. unlock_page(page);
  1427. goto err;
  1428. }
  1429. /* Did somebody else get it up-to-date? */
  1430. if (PageUptodate(page)) {
  1431. unlock_page(page);
  1432. goto success;
  1433. }
  1434. error = mapping->a_ops->readpage(file, page);
  1435. if (!error) {
  1436. wait_on_page_locked(page);
  1437. if (PageUptodate(page))
  1438. goto success;
  1439. } else if (error == AOP_TRUNCATED_PAGE) {
  1440. page_cache_release(page);
  1441. goto retry_find;
  1442. }
  1443. /*
  1444. * Umm, take care of errors if the page isn't up-to-date.
  1445. * Try to re-read it _once_. We do this synchronously,
  1446. * because there really aren't any performance issues here
  1447. * and we need to check for errors.
  1448. */
  1449. lock_page(page);
  1450. /* Somebody truncated the page on us? */
  1451. if (!page->mapping) {
  1452. unlock_page(page);
  1453. goto err;
  1454. }
  1455. /* Somebody else successfully read it in? */
  1456. if (PageUptodate(page)) {
  1457. unlock_page(page);
  1458. goto success;
  1459. }
  1460. ClearPageError(page);
  1461. error = mapping->a_ops->readpage(file, page);
  1462. if (!error) {
  1463. wait_on_page_locked(page);
  1464. if (PageUptodate(page))
  1465. goto success;
  1466. } else if (error == AOP_TRUNCATED_PAGE) {
  1467. page_cache_release(page);
  1468. goto retry_find;
  1469. }
  1470. /*
  1471. * Things didn't work out. Return zero to tell the
  1472. * mm layer so, possibly freeing the page cache page first.
  1473. */
  1474. err:
  1475. page_cache_release(page);
  1476. return NULL;
  1477. }
  1478. int filemap_populate(struct vm_area_struct *vma, unsigned long addr,
  1479. unsigned long len, pgprot_t prot, unsigned long pgoff,
  1480. int nonblock)
  1481. {
  1482. struct file *file = vma->vm_file;
  1483. struct address_space *mapping = file->f_mapping;
  1484. struct inode *inode = mapping->host;
  1485. unsigned long size;
  1486. struct mm_struct *mm = vma->vm_mm;
  1487. struct page *page;
  1488. int err;
  1489. if (!nonblock)
  1490. force_page_cache_readahead(mapping, vma->vm_file,
  1491. pgoff, len >> PAGE_CACHE_SHIFT);
  1492. repeat:
  1493. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1494. if (pgoff + (len >> PAGE_CACHE_SHIFT) > size)
  1495. return -EINVAL;
  1496. page = filemap_getpage(file, pgoff, nonblock);
  1497. /* XXX: This is wrong, a filesystem I/O error may have happened. Fix that as
  1498. * done in shmem_populate calling shmem_getpage */
  1499. if (!page && !nonblock)
  1500. return -ENOMEM;
  1501. if (page) {
  1502. err = install_page(mm, vma, addr, page, prot);
  1503. if (err) {
  1504. page_cache_release(page);
  1505. return err;
  1506. }
  1507. } else if (vma->vm_flags & VM_NONLINEAR) {
  1508. /* No page was found just because we can't read it in now (being
  1509. * here implies nonblock != 0), but the page may exist, so set
  1510. * the PTE to fault it in later. */
  1511. err = install_file_pte(mm, vma, addr, pgoff, prot);
  1512. if (err)
  1513. return err;
  1514. }
  1515. len -= PAGE_SIZE;
  1516. addr += PAGE_SIZE;
  1517. pgoff++;
  1518. if (len)
  1519. goto repeat;
  1520. return 0;
  1521. }
  1522. EXPORT_SYMBOL(filemap_populate);
  1523. struct vm_operations_struct generic_file_vm_ops = {
  1524. .nopage = filemap_nopage,
  1525. .populate = filemap_populate,
  1526. };
  1527. /* This is used for a general mmap of a disk file */
  1528. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1529. {
  1530. struct address_space *mapping = file->f_mapping;
  1531. if (!mapping->a_ops->readpage)
  1532. return -ENOEXEC;
  1533. file_accessed(file);
  1534. vma->vm_ops = &generic_file_vm_ops;
  1535. return 0;
  1536. }
  1537. /*
  1538. * This is for filesystems which do not implement ->writepage.
  1539. */
  1540. int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
  1541. {
  1542. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  1543. return -EINVAL;
  1544. return generic_file_mmap(file, vma);
  1545. }
  1546. #else
  1547. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1548. {
  1549. return -ENOSYS;
  1550. }
  1551. int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
  1552. {
  1553. return -ENOSYS;
  1554. }
  1555. #endif /* CONFIG_MMU */
  1556. EXPORT_SYMBOL(generic_file_mmap);
  1557. EXPORT_SYMBOL(generic_file_readonly_mmap);
  1558. static struct page *__read_cache_page(struct address_space *mapping,
  1559. unsigned long index,
  1560. int (*filler)(void *,struct page*),
  1561. void *data)
  1562. {
  1563. struct page *page, *cached_page = NULL;
  1564. int err;
  1565. repeat:
  1566. page = find_get_page(mapping, index);
  1567. if (!page) {
  1568. if (!cached_page) {
  1569. cached_page = page_cache_alloc_cold(mapping);
  1570. if (!cached_page)
  1571. return ERR_PTR(-ENOMEM);
  1572. }
  1573. err = add_to_page_cache_lru(cached_page, mapping,
  1574. index, GFP_KERNEL);
  1575. if (err == -EEXIST)
  1576. goto repeat;
  1577. if (err < 0) {
  1578. /* Presumably ENOMEM for radix tree node */
  1579. page_cache_release(cached_page);
  1580. return ERR_PTR(err);
  1581. }
  1582. page = cached_page;
  1583. cached_page = NULL;
  1584. err = filler(data, page);
  1585. if (err < 0) {
  1586. page_cache_release(page);
  1587. page = ERR_PTR(err);
  1588. }
  1589. }
  1590. if (cached_page)
  1591. page_cache_release(cached_page);
  1592. return page;
  1593. }
  1594. /*
  1595. * Same as read_cache_page, but don't wait for page to become unlocked
  1596. * after submitting it to the filler.
  1597. */
  1598. struct page *read_cache_page_async(struct address_space *mapping,
  1599. unsigned long index,
  1600. int (*filler)(void *,struct page*),
  1601. void *data)
  1602. {
  1603. struct page *page;
  1604. int err;
  1605. retry:
  1606. page = __read_cache_page(mapping, index, filler, data);
  1607. if (IS_ERR(page))
  1608. return page;
  1609. mark_page_accessed(page);
  1610. if (PageUptodate(page))
  1611. goto out;
  1612. lock_page(page);
  1613. if (!page->mapping) {
  1614. unlock_page(page);
  1615. page_cache_release(page);
  1616. goto retry;
  1617. }
  1618. if (PageUptodate(page)) {
  1619. unlock_page(page);
  1620. goto out;
  1621. }
  1622. err = filler(data, page);
  1623. if (err < 0) {
  1624. page_cache_release(page);
  1625. return ERR_PTR(err);
  1626. }
  1627. out:
  1628. mark_page_accessed(page);
  1629. return page;
  1630. }
  1631. EXPORT_SYMBOL(read_cache_page_async);
  1632. /**
  1633. * read_cache_page - read into page cache, fill it if needed
  1634. * @mapping: the page's address_space
  1635. * @index: the page index
  1636. * @filler: function to perform the read
  1637. * @data: destination for read data
  1638. *
  1639. * Read into the page cache. If a page already exists, and PageUptodate() is
  1640. * not set, try to fill the page then wait for it to become unlocked.
  1641. *
  1642. * If the page does not get brought uptodate, return -EIO.
  1643. */
  1644. struct page *read_cache_page(struct address_space *mapping,
  1645. unsigned long index,
  1646. int (*filler)(void *,struct page*),
  1647. void *data)
  1648. {
  1649. struct page *page;
  1650. page = read_cache_page_async(mapping, index, filler, data);
  1651. if (IS_ERR(page))
  1652. goto out;
  1653. wait_on_page_locked(page);
  1654. if (!PageUptodate(page)) {
  1655. page_cache_release(page);
  1656. page = ERR_PTR(-EIO);
  1657. }
  1658. out:
  1659. return page;
  1660. }
  1661. EXPORT_SYMBOL(read_cache_page);
  1662. /*
  1663. * If the page was newly created, increment its refcount and add it to the
  1664. * caller's lru-buffering pagevec. This function is specifically for
  1665. * generic_file_write().
  1666. */
  1667. static inline struct page *
  1668. __grab_cache_page(struct address_space *mapping, unsigned long index,
  1669. struct page **cached_page, struct pagevec *lru_pvec)
  1670. {
  1671. int err;
  1672. struct page *page;
  1673. repeat:
  1674. page = find_lock_page(mapping, index);
  1675. if (!page) {
  1676. if (!*cached_page) {
  1677. *cached_page = page_cache_alloc(mapping);
  1678. if (!*cached_page)
  1679. return NULL;
  1680. }
  1681. err = add_to_page_cache(*cached_page, mapping,
  1682. index, GFP_KERNEL);
  1683. if (err == -EEXIST)
  1684. goto repeat;
  1685. if (err == 0) {
  1686. page = *cached_page;
  1687. page_cache_get(page);
  1688. if (!pagevec_add(lru_pvec, page))
  1689. __pagevec_lru_add(lru_pvec);
  1690. *cached_page = NULL;
  1691. }
  1692. }
  1693. return page;
  1694. }
  1695. /*
  1696. * The logic we want is
  1697. *
  1698. * if suid or (sgid and xgrp)
  1699. * remove privs
  1700. */
  1701. int should_remove_suid(struct dentry *dentry)
  1702. {
  1703. mode_t mode = dentry->d_inode->i_mode;
  1704. int kill = 0;
  1705. /* suid always must be killed */
  1706. if (unlikely(mode & S_ISUID))
  1707. kill = ATTR_KILL_SUID;
  1708. /*
  1709. * sgid without any exec bits is just a mandatory locking mark; leave
  1710. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1711. */
  1712. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1713. kill |= ATTR_KILL_SGID;
  1714. if (unlikely(kill && !capable(CAP_FSETID)))
  1715. return kill;
  1716. return 0;
  1717. }
  1718. EXPORT_SYMBOL(should_remove_suid);
  1719. int __remove_suid(struct dentry *dentry, int kill)
  1720. {
  1721. struct iattr newattrs;
  1722. newattrs.ia_valid = ATTR_FORCE | kill;
  1723. return notify_change(dentry, &newattrs);
  1724. }
  1725. int remove_suid(struct dentry *dentry)
  1726. {
  1727. int kill = should_remove_suid(dentry);
  1728. if (unlikely(kill))
  1729. return __remove_suid(dentry, kill);
  1730. return 0;
  1731. }
  1732. EXPORT_SYMBOL(remove_suid);
  1733. size_t
  1734. __filemap_copy_from_user_iovec_inatomic(char *vaddr,
  1735. const struct iovec *iov, size_t base, size_t bytes)
  1736. {
  1737. size_t copied = 0, left = 0;
  1738. while (bytes) {
  1739. char __user *buf = iov->iov_base + base;
  1740. int copy = min(bytes, iov->iov_len - base);
  1741. base = 0;
  1742. left = __copy_from_user_inatomic_nocache(vaddr, buf, copy);
  1743. copied += copy;
  1744. bytes -= copy;
  1745. vaddr += copy;
  1746. iov++;
  1747. if (unlikely(left))
  1748. break;
  1749. }
  1750. return copied - left;
  1751. }
  1752. /*
  1753. * Performs necessary checks before doing a write
  1754. *
  1755. * Can adjust writing position or amount of bytes to write.
  1756. * Returns appropriate error code that caller should return or
  1757. * zero in case that write should be allowed.
  1758. */
  1759. inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
  1760. {
  1761. struct inode *inode = file->f_mapping->host;
  1762. unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1763. if (unlikely(*pos < 0))
  1764. return -EINVAL;
  1765. if (!isblk) {
  1766. /* FIXME: this is for backwards compatibility with 2.4 */
  1767. if (file->f_flags & O_APPEND)
  1768. *pos = i_size_read(inode);
  1769. if (limit != RLIM_INFINITY) {
  1770. if (*pos >= limit) {
  1771. send_sig(SIGXFSZ, current, 0);
  1772. return -EFBIG;
  1773. }
  1774. if (*count > limit - (typeof(limit))*pos) {
  1775. *count = limit - (typeof(limit))*pos;
  1776. }
  1777. }
  1778. }
  1779. /*
  1780. * LFS rule
  1781. */
  1782. if (unlikely(*pos + *count > MAX_NON_LFS &&
  1783. !(file->f_flags & O_LARGEFILE))) {
  1784. if (*pos >= MAX_NON_LFS) {
  1785. send_sig(SIGXFSZ, current, 0);
  1786. return -EFBIG;
  1787. }
  1788. if (*count > MAX_NON_LFS - (unsigned long)*pos) {
  1789. *count = MAX_NON_LFS - (unsigned long)*pos;
  1790. }
  1791. }
  1792. /*
  1793. * Are we about to exceed the fs block limit ?
  1794. *
  1795. * If we have written data it becomes a short write. If we have
  1796. * exceeded without writing data we send a signal and return EFBIG.
  1797. * Linus frestrict idea will clean these up nicely..
  1798. */
  1799. if (likely(!isblk)) {
  1800. if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
  1801. if (*count || *pos > inode->i_sb->s_maxbytes) {
  1802. send_sig(SIGXFSZ, current, 0);
  1803. return -EFBIG;
  1804. }
  1805. /* zero-length writes at ->s_maxbytes are OK */
  1806. }
  1807. if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
  1808. *count = inode->i_sb->s_maxbytes - *pos;
  1809. } else {
  1810. #ifdef CONFIG_BLOCK
  1811. loff_t isize;
  1812. if (bdev_read_only(I_BDEV(inode)))
  1813. return -EPERM;
  1814. isize = i_size_read(inode);
  1815. if (*pos >= isize) {
  1816. if (*count || *pos > isize)
  1817. return -ENOSPC;
  1818. }
  1819. if (*pos + *count > isize)
  1820. *count = isize - *pos;
  1821. #else
  1822. return -EPERM;
  1823. #endif
  1824. }
  1825. return 0;
  1826. }
  1827. EXPORT_SYMBOL(generic_write_checks);
  1828. ssize_t
  1829. generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  1830. unsigned long *nr_segs, loff_t pos, loff_t *ppos,
  1831. size_t count, size_t ocount)
  1832. {
  1833. struct file *file = iocb->ki_filp;
  1834. struct address_space *mapping = file->f_mapping;
  1835. struct inode *inode = mapping->host;
  1836. ssize_t written;
  1837. if (count != ocount)
  1838. *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
  1839. written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
  1840. if (written > 0) {
  1841. loff_t end = pos + written;
  1842. if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
  1843. i_size_write(inode, end);
  1844. mark_inode_dirty(inode);
  1845. }
  1846. *ppos = end;
  1847. }
  1848. /*
  1849. * Sync the fs metadata but not the minor inode changes and
  1850. * of course not the data as we did direct DMA for the IO.
  1851. * i_mutex is held, which protects generic_osync_inode() from
  1852. * livelocking. AIO O_DIRECT ops attempt to sync metadata here.
  1853. */
  1854. if ((written >= 0 || written == -EIOCBQUEUED) &&
  1855. ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1856. int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
  1857. if (err < 0)
  1858. written = err;
  1859. }
  1860. return written;
  1861. }
  1862. EXPORT_SYMBOL(generic_file_direct_write);
  1863. ssize_t
  1864. generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
  1865. unsigned long nr_segs, loff_t pos, loff_t *ppos,
  1866. size_t count, ssize_t written)
  1867. {
  1868. struct file *file = iocb->ki_filp;
  1869. struct address_space * mapping = file->f_mapping;
  1870. const struct address_space_operations *a_ops = mapping->a_ops;
  1871. struct inode *inode = mapping->host;
  1872. long status = 0;
  1873. struct page *page;
  1874. struct page *cached_page = NULL;
  1875. size_t bytes;
  1876. struct pagevec lru_pvec;
  1877. const struct iovec *cur_iov = iov; /* current iovec */
  1878. size_t iov_base = 0; /* offset in the current iovec */
  1879. char __user *buf;
  1880. pagevec_init(&lru_pvec, 0);
  1881. /*
  1882. * handle partial DIO write. Adjust cur_iov if needed.
  1883. */
  1884. if (likely(nr_segs == 1))
  1885. buf = iov->iov_base + written;
  1886. else {
  1887. filemap_set_next_iovec(&cur_iov, &iov_base, written);
  1888. buf = cur_iov->iov_base + iov_base;
  1889. }
  1890. do {
  1891. unsigned long index;
  1892. unsigned long offset;
  1893. size_t copied;
  1894. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1895. index = pos >> PAGE_CACHE_SHIFT;
  1896. bytes = PAGE_CACHE_SIZE - offset;
  1897. /* Limit the size of the copy to the caller's write size */
  1898. bytes = min(bytes, count);
  1899. /* We only need to worry about prefaulting when writes are from
  1900. * user-space. NFSd uses vfs_writev with several non-aligned
  1901. * segments in the vector, and limiting to one segment a time is
  1902. * a noticeable performance for re-write
  1903. */
  1904. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1905. /*
  1906. * Limit the size of the copy to that of the current
  1907. * segment, because fault_in_pages_readable() doesn't
  1908. * know how to walk segments.
  1909. */
  1910. bytes = min(bytes, cur_iov->iov_len - iov_base);
  1911. /*
  1912. * Bring in the user page that we will copy from
  1913. * _first_. Otherwise there's a nasty deadlock on
  1914. * copying from the same page as we're writing to,
  1915. * without it being marked up-to-date.
  1916. */
  1917. fault_in_pages_readable(buf, bytes);
  1918. }
  1919. page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
  1920. if (!page) {
  1921. status = -ENOMEM;
  1922. break;
  1923. }
  1924. if (unlikely(bytes == 0)) {
  1925. status = 0;
  1926. copied = 0;
  1927. goto zero_length_segment;
  1928. }
  1929. status = a_ops->prepare_write(file, page, offset, offset+bytes);
  1930. if (unlikely(status)) {
  1931. loff_t isize = i_size_read(inode);
  1932. if (status != AOP_TRUNCATED_PAGE)
  1933. unlock_page(page);
  1934. page_cache_release(page);
  1935. if (status == AOP_TRUNCATED_PAGE)
  1936. continue;
  1937. /*
  1938. * prepare_write() may have instantiated a few blocks
  1939. * outside i_size. Trim these off again.
  1940. */
  1941. if (pos + bytes > isize)
  1942. vmtruncate(inode, isize);
  1943. break;
  1944. }
  1945. if (likely(nr_segs == 1))
  1946. copied = filemap_copy_from_user(page, offset,
  1947. buf, bytes);
  1948. else
  1949. copied = filemap_copy_from_user_iovec(page, offset,
  1950. cur_iov, iov_base, bytes);
  1951. flush_dcache_page(page);
  1952. status = a_ops->commit_write(file, page, offset, offset+bytes);
  1953. if (status == AOP_TRUNCATED_PAGE) {
  1954. page_cache_release(page);
  1955. continue;
  1956. }
  1957. zero_length_segment:
  1958. if (likely(copied >= 0)) {
  1959. if (!status)
  1960. status = copied;
  1961. if (status >= 0) {
  1962. written += status;
  1963. count -= status;
  1964. pos += status;
  1965. buf += status;
  1966. if (unlikely(nr_segs > 1)) {
  1967. filemap_set_next_iovec(&cur_iov,
  1968. &iov_base, status);
  1969. if (count)
  1970. buf = cur_iov->iov_base +
  1971. iov_base;
  1972. } else {
  1973. iov_base += status;
  1974. }
  1975. }
  1976. }
  1977. if (unlikely(copied != bytes))
  1978. if (status >= 0)
  1979. status = -EFAULT;
  1980. unlock_page(page);
  1981. mark_page_accessed(page);
  1982. page_cache_release(page);
  1983. if (status < 0)
  1984. break;
  1985. balance_dirty_pages_ratelimited(mapping);
  1986. cond_resched();
  1987. } while (count);
  1988. *ppos = pos;
  1989. if (cached_page)
  1990. page_cache_release(cached_page);
  1991. /*
  1992. * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
  1993. */
  1994. if (likely(status >= 0)) {
  1995. if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1996. if (!a_ops->writepage || !is_sync_kiocb(iocb))
  1997. status = generic_osync_inode(inode, mapping,
  1998. OSYNC_METADATA|OSYNC_DATA);
  1999. }
  2000. }
  2001. /*
  2002. * If we get here for O_DIRECT writes then we must have fallen through
  2003. * to buffered writes (block instantiation inside i_size). So we sync
  2004. * the file data here, to try to honour O_DIRECT expectations.
  2005. */
  2006. if (unlikely(file->f_flags & O_DIRECT) && written)
  2007. status = filemap_write_and_wait(mapping);
  2008. pagevec_lru_add(&lru_pvec);
  2009. return written ? written : status;
  2010. }
  2011. EXPORT_SYMBOL(generic_file_buffered_write);
  2012. static ssize_t
  2013. __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
  2014. unsigned long nr_segs, loff_t *ppos)
  2015. {
  2016. struct file *file = iocb->ki_filp;
  2017. struct address_space * mapping = file->f_mapping;
  2018. size_t ocount; /* original count */
  2019. size_t count; /* after file limit checks */
  2020. struct inode *inode = mapping->host;
  2021. loff_t pos;
  2022. ssize_t written;
  2023. ssize_t err;
  2024. ocount = 0;
  2025. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  2026. if (err)
  2027. return err;
  2028. count = ocount;
  2029. pos = *ppos;
  2030. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  2031. /* We can write back this queue in page reclaim */
  2032. current->backing_dev_info = mapping->backing_dev_info;
  2033. written = 0;
  2034. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  2035. if (err)
  2036. goto out;
  2037. if (count == 0)
  2038. goto out;
  2039. err = remove_suid(file->f_path.dentry);
  2040. if (err)
  2041. goto out;
  2042. file_update_time(file);
  2043. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  2044. if (unlikely(file->f_flags & O_DIRECT)) {
  2045. loff_t endbyte;
  2046. ssize_t written_buffered;
  2047. written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
  2048. ppos, count, ocount);
  2049. if (written < 0 || written == count)
  2050. goto out;
  2051. /*
  2052. * direct-io write to a hole: fall through to buffered I/O
  2053. * for completing the rest of the request.
  2054. */
  2055. pos += written;
  2056. count -= written;
  2057. written_buffered = generic_file_buffered_write(iocb, iov,
  2058. nr_segs, pos, ppos, count,
  2059. written);
  2060. /*
  2061. * If generic_file_buffered_write() retuned a synchronous error
  2062. * then we want to return the number of bytes which were
  2063. * direct-written, or the error code if that was zero. Note
  2064. * that this differs from normal direct-io semantics, which
  2065. * will return -EFOO even if some bytes were written.
  2066. */
  2067. if (written_buffered < 0) {
  2068. err = written_buffered;
  2069. goto out;
  2070. }
  2071. /*
  2072. * We need to ensure that the page cache pages are written to
  2073. * disk and invalidated to preserve the expected O_DIRECT
  2074. * semantics.
  2075. */
  2076. endbyte = pos + written_buffered - written - 1;
  2077. err = do_sync_mapping_range(file->f_mapping, pos, endbyte,
  2078. SYNC_FILE_RANGE_WAIT_BEFORE|
  2079. SYNC_FILE_RANGE_WRITE|
  2080. SYNC_FILE_RANGE_WAIT_AFTER);
  2081. if (err == 0) {
  2082. written = written_buffered;
  2083. invalidate_mapping_pages(mapping,
  2084. pos >> PAGE_CACHE_SHIFT,
  2085. endbyte >> PAGE_CACHE_SHIFT);
  2086. } else {
  2087. /*
  2088. * We don't know how much we wrote, so just return
  2089. * the number of bytes which were direct-written
  2090. */
  2091. }
  2092. } else {
  2093. written = generic_file_buffered_write(iocb, iov, nr_segs,
  2094. pos, ppos, count, written);
  2095. }
  2096. out:
  2097. current->backing_dev_info = NULL;
  2098. return written ? written : err;
  2099. }
  2100. ssize_t generic_file_aio_write_nolock(struct kiocb *iocb,
  2101. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  2102. {
  2103. struct file *file = iocb->ki_filp;
  2104. struct address_space *mapping = file->f_mapping;
  2105. struct inode *inode = mapping->host;
  2106. ssize_t ret;
  2107. BUG_ON(iocb->ki_pos != pos);
  2108. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  2109. &iocb->ki_pos);
  2110. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2111. ssize_t err;
  2112. err = sync_page_range_nolock(inode, mapping, pos, ret);
  2113. if (err < 0)
  2114. ret = err;
  2115. }
  2116. return ret;
  2117. }
  2118. EXPORT_SYMBOL(generic_file_aio_write_nolock);
  2119. ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  2120. unsigned long nr_segs, loff_t pos)
  2121. {
  2122. struct file *file = iocb->ki_filp;
  2123. struct address_space *mapping = file->f_mapping;
  2124. struct inode *inode = mapping->host;
  2125. ssize_t ret;
  2126. BUG_ON(iocb->ki_pos != pos);
  2127. mutex_lock(&inode->i_mutex);
  2128. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  2129. &iocb->ki_pos);
  2130. mutex_unlock(&inode->i_mutex);
  2131. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  2132. ssize_t err;
  2133. err = sync_page_range(inode, mapping, pos, ret);
  2134. if (err < 0)
  2135. ret = err;
  2136. }
  2137. return ret;
  2138. }
  2139. EXPORT_SYMBOL(generic_file_aio_write);
  2140. /*
  2141. * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
  2142. * went wrong during pagecache shootdown.
  2143. */
  2144. static ssize_t
  2145. generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  2146. loff_t offset, unsigned long nr_segs)
  2147. {
  2148. struct file *file = iocb->ki_filp;
  2149. struct address_space *mapping = file->f_mapping;
  2150. ssize_t retval;
  2151. size_t write_len;
  2152. pgoff_t end = 0; /* silence gcc */
  2153. /*
  2154. * If it's a write, unmap all mmappings of the file up-front. This
  2155. * will cause any pte dirty bits to be propagated into the pageframes
  2156. * for the subsequent filemap_write_and_wait().
  2157. */
  2158. if (rw == WRITE) {
  2159. write_len = iov_length(iov, nr_segs);
  2160. end = (offset + write_len - 1) >> PAGE_CACHE_SHIFT;
  2161. if (mapping_mapped(mapping))
  2162. unmap_mapping_range(mapping, offset, write_len, 0);
  2163. }
  2164. retval = filemap_write_and_wait(mapping);
  2165. if (retval)
  2166. goto out;
  2167. /*
  2168. * After a write we want buffered reads to be sure to go to disk to get
  2169. * the new data. We invalidate clean cached page from the region we're
  2170. * about to write. We do this *before* the write so that we can return
  2171. * -EIO without clobbering -EIOCBQUEUED from ->direct_IO().
  2172. */
  2173. if (rw == WRITE && mapping->nrpages) {
  2174. retval = invalidate_inode_pages2_range(mapping,
  2175. offset >> PAGE_CACHE_SHIFT, end);
  2176. if (retval)
  2177. goto out;
  2178. }
  2179. retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
  2180. if (retval)
  2181. goto out;
  2182. /*
  2183. * Finally, try again to invalidate clean pages which might have been
  2184. * faulted in by get_user_pages() if the source of the write was an
  2185. * mmap()ed region of the file we're writing. That's a pretty crazy
  2186. * thing to do, so we don't support it 100%. If this invalidation
  2187. * fails and we have -EIOCBQUEUED we ignore the failure.
  2188. */
  2189. if (rw == WRITE && mapping->nrpages) {
  2190. int err = invalidate_inode_pages2_range(mapping,
  2191. offset >> PAGE_CACHE_SHIFT, end);
  2192. if (err && retval >= 0)
  2193. retval = err;
  2194. }
  2195. out:
  2196. return retval;
  2197. }
  2198. /**
  2199. * try_to_release_page() - release old fs-specific metadata on a page
  2200. *
  2201. * @page: the page which the kernel is trying to free
  2202. * @gfp_mask: memory allocation flags (and I/O mode)
  2203. *
  2204. * The address_space is to try to release any data against the page
  2205. * (presumably at page->private). If the release was successful, return `1'.
  2206. * Otherwise return zero.
  2207. *
  2208. * The @gfp_mask argument specifies whether I/O may be performed to release
  2209. * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
  2210. *
  2211. * NOTE: @gfp_mask may go away, and this function may become non-blocking.
  2212. */
  2213. int try_to_release_page(struct page *page, gfp_t gfp_mask)
  2214. {
  2215. struct address_space * const mapping = page->mapping;
  2216. BUG_ON(!PageLocked(page));
  2217. if (PageWriteback(page))
  2218. return 0;
  2219. if (mapping && mapping->a_ops->releasepage)
  2220. return mapping->a_ops->releasepage(page, gfp_mask);
  2221. return try_to_free_buffers(page);
  2222. }
  2223. EXPORT_SYMBOL(try_to_release_page);