filemap.c 64 KB

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