filemap.c 67 KB

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