filemap.c 63 KB

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