filemap.c 67 KB

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