filemap.c 64 KB

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