filemap.c 64 KB

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