filemap.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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 offset;
  798. unsigned long last_index;
  799. unsigned long next_index;
  800. unsigned long prev_index;
  801. unsigned int prev_offset;
  802. struct page *cached_page;
  803. int error;
  804. struct file_ra_state ra = *_ra;
  805. cached_page = NULL;
  806. index = *ppos >> PAGE_CACHE_SHIFT;
  807. next_index = index;
  808. prev_index = ra.prev_index;
  809. prev_offset = ra.prev_offset;
  810. last_index = (*ppos + desc->count + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  811. offset = *ppos & ~PAGE_CACHE_MASK;
  812. for (;;) {
  813. struct page *page;
  814. unsigned long end_index;
  815. loff_t isize;
  816. unsigned long nr, ret;
  817. cond_resched();
  818. find_page:
  819. page = find_get_page(mapping, index);
  820. if (!page) {
  821. page_cache_sync_readahead(mapping,
  822. &ra, filp,
  823. index, last_index - index);
  824. page = find_get_page(mapping, index);
  825. if (unlikely(page == NULL))
  826. goto no_cached_page;
  827. }
  828. if (PageReadahead(page)) {
  829. page_cache_async_readahead(mapping,
  830. &ra, filp, page,
  831. index, last_index - index);
  832. }
  833. if (!PageUptodate(page))
  834. goto page_not_up_to_date;
  835. page_ok:
  836. /*
  837. * i_size must be checked after we know the page is Uptodate.
  838. *
  839. * Checking i_size after the check allows us to calculate
  840. * the correct value for "nr", which means the zero-filled
  841. * part of the page is not copied back to userspace (unless
  842. * another truncate extends the file - this is desired though).
  843. */
  844. isize = i_size_read(inode);
  845. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  846. if (unlikely(!isize || index > end_index)) {
  847. page_cache_release(page);
  848. goto out;
  849. }
  850. /* nr is the maximum number of bytes to copy from this page */
  851. nr = PAGE_CACHE_SIZE;
  852. if (index == end_index) {
  853. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  854. if (nr <= offset) {
  855. page_cache_release(page);
  856. goto out;
  857. }
  858. }
  859. nr = nr - offset;
  860. /* If users can be writing to this page using arbitrary
  861. * virtual addresses, take care about potential aliasing
  862. * before reading the page on the kernel side.
  863. */
  864. if (mapping_writably_mapped(mapping))
  865. flush_dcache_page(page);
  866. /*
  867. * When a sequential read accesses a page several times,
  868. * only mark it as accessed the first time.
  869. */
  870. if (prev_index != index || offset != prev_offset)
  871. mark_page_accessed(page);
  872. prev_index = index;
  873. /*
  874. * Ok, we have the page, and it's up-to-date, so
  875. * now we can copy it to user space...
  876. *
  877. * The actor routine returns how many bytes were actually used..
  878. * NOTE! This may not be the same as how much of a user buffer
  879. * we filled up (we may be padding etc), so we can only update
  880. * "pos" here (the actor routine has to update the user buffer
  881. * pointers and the remaining count).
  882. */
  883. ret = actor(desc, page, offset, nr);
  884. offset += ret;
  885. index += offset >> PAGE_CACHE_SHIFT;
  886. offset &= ~PAGE_CACHE_MASK;
  887. prev_offset = offset;
  888. ra.prev_offset = offset;
  889. page_cache_release(page);
  890. if (ret == nr && desc->count)
  891. continue;
  892. goto out;
  893. page_not_up_to_date:
  894. /* Get exclusive access to the page ... */
  895. lock_page(page);
  896. /* Did it get truncated before we got the lock? */
  897. if (!page->mapping) {
  898. unlock_page(page);
  899. page_cache_release(page);
  900. continue;
  901. }
  902. /* Did somebody else fill it already? */
  903. if (PageUptodate(page)) {
  904. unlock_page(page);
  905. goto page_ok;
  906. }
  907. readpage:
  908. /* Start the actual read. The read will unlock the page. */
  909. error = mapping->a_ops->readpage(filp, page);
  910. if (unlikely(error)) {
  911. if (error == AOP_TRUNCATED_PAGE) {
  912. page_cache_release(page);
  913. goto find_page;
  914. }
  915. goto readpage_error;
  916. }
  917. if (!PageUptodate(page)) {
  918. lock_page(page);
  919. if (!PageUptodate(page)) {
  920. if (page->mapping == NULL) {
  921. /*
  922. * invalidate_inode_pages got it
  923. */
  924. unlock_page(page);
  925. page_cache_release(page);
  926. goto find_page;
  927. }
  928. unlock_page(page);
  929. error = -EIO;
  930. shrink_readahead_size_eio(filp, &ra);
  931. goto readpage_error;
  932. }
  933. unlock_page(page);
  934. }
  935. goto page_ok;
  936. readpage_error:
  937. /* UHHUH! A synchronous read error occurred. Report it */
  938. desc->error = error;
  939. page_cache_release(page);
  940. goto out;
  941. no_cached_page:
  942. /*
  943. * Ok, it wasn't cached, so we need to create a new
  944. * page..
  945. */
  946. if (!cached_page) {
  947. cached_page = page_cache_alloc_cold(mapping);
  948. if (!cached_page) {
  949. desc->error = -ENOMEM;
  950. goto out;
  951. }
  952. }
  953. error = add_to_page_cache_lru(cached_page, mapping,
  954. index, GFP_KERNEL);
  955. if (error) {
  956. if (error == -EEXIST)
  957. goto find_page;
  958. desc->error = error;
  959. goto out;
  960. }
  961. page = cached_page;
  962. cached_page = NULL;
  963. goto readpage;
  964. }
  965. out:
  966. *_ra = ra;
  967. _ra->prev_index = prev_index;
  968. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  969. if (cached_page)
  970. page_cache_release(cached_page);
  971. if (filp)
  972. file_accessed(filp);
  973. }
  974. EXPORT_SYMBOL(do_generic_mapping_read);
  975. int file_read_actor(read_descriptor_t *desc, struct page *page,
  976. unsigned long offset, unsigned long size)
  977. {
  978. char *kaddr;
  979. unsigned long left, count = desc->count;
  980. if (size > count)
  981. size = count;
  982. /*
  983. * Faults on the destination of a read are common, so do it before
  984. * taking the kmap.
  985. */
  986. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  987. kaddr = kmap_atomic(page, KM_USER0);
  988. left = __copy_to_user_inatomic(desc->arg.buf,
  989. kaddr + offset, size);
  990. kunmap_atomic(kaddr, KM_USER0);
  991. if (left == 0)
  992. goto success;
  993. }
  994. /* Do it the slow way */
  995. kaddr = kmap(page);
  996. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  997. kunmap(page);
  998. if (left) {
  999. size -= left;
  1000. desc->error = -EFAULT;
  1001. }
  1002. success:
  1003. desc->count = count - size;
  1004. desc->written += size;
  1005. desc->arg.buf += size;
  1006. return size;
  1007. }
  1008. /*
  1009. * Performs necessary checks before doing a write
  1010. * @iov: io vector request
  1011. * @nr_segs: number of segments in the iovec
  1012. * @count: number of bytes to write
  1013. * @access_flags: type of access: %VERIFY_READ or %VERIFY_WRITE
  1014. *
  1015. * Adjust number of segments and amount of bytes to write (nr_segs should be
  1016. * properly initialized first). Returns appropriate error code that caller
  1017. * should return or zero in case that write should be allowed.
  1018. */
  1019. int generic_segment_checks(const struct iovec *iov,
  1020. unsigned long *nr_segs, size_t *count, int access_flags)
  1021. {
  1022. unsigned long seg;
  1023. size_t cnt = 0;
  1024. for (seg = 0; seg < *nr_segs; seg++) {
  1025. const struct iovec *iv = &iov[seg];
  1026. /*
  1027. * If any segment has a negative length, or the cumulative
  1028. * length ever wraps negative then return -EINVAL.
  1029. */
  1030. cnt += iv->iov_len;
  1031. if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
  1032. return -EINVAL;
  1033. if (access_ok(access_flags, iv->iov_base, iv->iov_len))
  1034. continue;
  1035. if (seg == 0)
  1036. return -EFAULT;
  1037. *nr_segs = seg;
  1038. cnt -= iv->iov_len; /* This segment is no good */
  1039. break;
  1040. }
  1041. *count = cnt;
  1042. return 0;
  1043. }
  1044. EXPORT_SYMBOL(generic_segment_checks);
  1045. /**
  1046. * generic_file_aio_read - generic filesystem read routine
  1047. * @iocb: kernel I/O control block
  1048. * @iov: io vector request
  1049. * @nr_segs: number of segments in the iovec
  1050. * @pos: current file position
  1051. *
  1052. * This is the "read()" routine for all filesystems
  1053. * that can use the page cache directly.
  1054. */
  1055. ssize_t
  1056. generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1057. unsigned long nr_segs, loff_t pos)
  1058. {
  1059. struct file *filp = iocb->ki_filp;
  1060. ssize_t retval;
  1061. unsigned long seg;
  1062. size_t count;
  1063. loff_t *ppos = &iocb->ki_pos;
  1064. count = 0;
  1065. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1066. if (retval)
  1067. return retval;
  1068. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1069. if (filp->f_flags & O_DIRECT) {
  1070. loff_t size;
  1071. struct address_space *mapping;
  1072. struct inode *inode;
  1073. mapping = filp->f_mapping;
  1074. inode = mapping->host;
  1075. retval = 0;
  1076. if (!count)
  1077. goto out; /* skip atime */
  1078. size = i_size_read(inode);
  1079. if (pos < size) {
  1080. retval = generic_file_direct_IO(READ, iocb,
  1081. iov, pos, nr_segs);
  1082. if (retval > 0)
  1083. *ppos = pos + retval;
  1084. }
  1085. if (likely(retval != 0)) {
  1086. file_accessed(filp);
  1087. goto out;
  1088. }
  1089. }
  1090. retval = 0;
  1091. if (count) {
  1092. for (seg = 0; seg < nr_segs; seg++) {
  1093. read_descriptor_t desc;
  1094. desc.written = 0;
  1095. desc.arg.buf = iov[seg].iov_base;
  1096. desc.count = iov[seg].iov_len;
  1097. if (desc.count == 0)
  1098. continue;
  1099. desc.error = 0;
  1100. do_generic_file_read(filp,ppos,&desc,file_read_actor);
  1101. retval += desc.written;
  1102. if (desc.error) {
  1103. retval = retval ?: desc.error;
  1104. break;
  1105. }
  1106. if (desc.count > 0)
  1107. break;
  1108. }
  1109. }
  1110. out:
  1111. return retval;
  1112. }
  1113. EXPORT_SYMBOL(generic_file_aio_read);
  1114. static ssize_t
  1115. do_readahead(struct address_space *mapping, struct file *filp,
  1116. unsigned long index, unsigned long nr)
  1117. {
  1118. if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
  1119. return -EINVAL;
  1120. force_page_cache_readahead(mapping, filp, index,
  1121. max_sane_readahead(nr));
  1122. return 0;
  1123. }
  1124. asmlinkage ssize_t sys_readahead(int fd, loff_t offset, size_t count)
  1125. {
  1126. ssize_t ret;
  1127. struct file *file;
  1128. ret = -EBADF;
  1129. file = fget(fd);
  1130. if (file) {
  1131. if (file->f_mode & FMODE_READ) {
  1132. struct address_space *mapping = file->f_mapping;
  1133. unsigned long start = offset >> PAGE_CACHE_SHIFT;
  1134. unsigned long end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  1135. unsigned long len = end - start + 1;
  1136. ret = do_readahead(mapping, file, start, len);
  1137. }
  1138. fput(file);
  1139. }
  1140. return ret;
  1141. }
  1142. #ifdef CONFIG_MMU
  1143. static int FASTCALL(page_cache_read(struct file * file, unsigned long offset));
  1144. /**
  1145. * page_cache_read - adds requested page to the page cache if not already there
  1146. * @file: file to read
  1147. * @offset: page index
  1148. *
  1149. * This adds the requested page to the page cache if it isn't already there,
  1150. * and schedules an I/O to read in its contents from disk.
  1151. */
  1152. static int fastcall page_cache_read(struct file * file, unsigned long offset)
  1153. {
  1154. struct address_space *mapping = file->f_mapping;
  1155. struct page *page;
  1156. int ret;
  1157. do {
  1158. page = page_cache_alloc_cold(mapping);
  1159. if (!page)
  1160. return -ENOMEM;
  1161. ret = add_to_page_cache_lru(page, mapping, offset, GFP_KERNEL);
  1162. if (ret == 0)
  1163. ret = mapping->a_ops->readpage(file, page);
  1164. else if (ret == -EEXIST)
  1165. ret = 0; /* losing race to add is OK */
  1166. page_cache_release(page);
  1167. } while (ret == AOP_TRUNCATED_PAGE);
  1168. return ret;
  1169. }
  1170. #define MMAP_LOTSAMISS (100)
  1171. /**
  1172. * filemap_fault - read in file data for page fault handling
  1173. * @vma: vma in which the fault was taken
  1174. * @vmf: struct vm_fault containing details of the fault
  1175. *
  1176. * filemap_fault() is invoked via the vma operations vector for a
  1177. * mapped memory region to read in file data during a page fault.
  1178. *
  1179. * The goto's are kind of ugly, but this streamlines the normal case of having
  1180. * it in the page cache, and handles the special cases reasonably without
  1181. * having a lot of duplicated code.
  1182. */
  1183. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1184. {
  1185. int error;
  1186. struct file *file = vma->vm_file;
  1187. struct address_space *mapping = file->f_mapping;
  1188. struct file_ra_state *ra = &file->f_ra;
  1189. struct inode *inode = mapping->host;
  1190. struct page *page;
  1191. unsigned long size;
  1192. int did_readaround = 0;
  1193. int ret = 0;
  1194. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1195. if (vmf->pgoff >= size)
  1196. goto outside_data_content;
  1197. /* If we don't want any read-ahead, don't bother */
  1198. if (VM_RandomReadHint(vma))
  1199. goto no_cached_page;
  1200. /*
  1201. * Do we have something in the page cache already?
  1202. */
  1203. retry_find:
  1204. page = find_lock_page(mapping, vmf->pgoff);
  1205. /*
  1206. * For sequential accesses, we use the generic readahead logic.
  1207. */
  1208. if (VM_SequentialReadHint(vma)) {
  1209. if (!page) {
  1210. page_cache_sync_readahead(mapping, ra, file,
  1211. vmf->pgoff, 1);
  1212. page = find_lock_page(mapping, vmf->pgoff);
  1213. if (!page)
  1214. goto no_cached_page;
  1215. }
  1216. if (PageReadahead(page)) {
  1217. page_cache_async_readahead(mapping, ra, file, page,
  1218. vmf->pgoff, 1);
  1219. }
  1220. }
  1221. if (!page) {
  1222. unsigned long ra_pages;
  1223. ra->mmap_miss++;
  1224. /*
  1225. * Do we miss much more than hit in this file? If so,
  1226. * stop bothering with read-ahead. It will only hurt.
  1227. */
  1228. if (ra->mmap_miss > ra->mmap_hit + MMAP_LOTSAMISS)
  1229. goto no_cached_page;
  1230. /*
  1231. * To keep the pgmajfault counter straight, we need to
  1232. * check did_readaround, as this is an inner loop.
  1233. */
  1234. if (!did_readaround) {
  1235. ret = VM_FAULT_MAJOR;
  1236. count_vm_event(PGMAJFAULT);
  1237. }
  1238. did_readaround = 1;
  1239. ra_pages = max_sane_readahead(file->f_ra.ra_pages);
  1240. if (ra_pages) {
  1241. pgoff_t start = 0;
  1242. if (vmf->pgoff > ra_pages / 2)
  1243. start = vmf->pgoff - ra_pages / 2;
  1244. do_page_cache_readahead(mapping, file, start, ra_pages);
  1245. }
  1246. page = find_lock_page(mapping, vmf->pgoff);
  1247. if (!page)
  1248. goto no_cached_page;
  1249. }
  1250. if (!did_readaround)
  1251. ra->mmap_hit++;
  1252. /*
  1253. * We have a locked page in the page cache, now we need to check
  1254. * that it's up-to-date. If not, it is going to be due to an error.
  1255. */
  1256. if (unlikely(!PageUptodate(page)))
  1257. goto page_not_uptodate;
  1258. /* Must recheck i_size under page lock */
  1259. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1260. if (unlikely(vmf->pgoff >= size)) {
  1261. unlock_page(page);
  1262. page_cache_release(page);
  1263. goto outside_data_content;
  1264. }
  1265. /*
  1266. * Found the page and have a reference on it.
  1267. */
  1268. mark_page_accessed(page);
  1269. ra->prev_index = page->index;
  1270. vmf->page = page;
  1271. return ret | VM_FAULT_LOCKED;
  1272. outside_data_content:
  1273. /*
  1274. * An external ptracer can access pages that normally aren't
  1275. * accessible..
  1276. */
  1277. if (vma->vm_mm == current->mm)
  1278. return VM_FAULT_SIGBUS;
  1279. /* Fall through to the non-read-ahead case */
  1280. no_cached_page:
  1281. /*
  1282. * We're only likely to ever get here if MADV_RANDOM is in
  1283. * effect.
  1284. */
  1285. error = page_cache_read(file, vmf->pgoff);
  1286. /*
  1287. * The page we want has now been added to the page cache.
  1288. * In the unlikely event that someone removed it in the
  1289. * meantime, we'll just come back here and read it again.
  1290. */
  1291. if (error >= 0)
  1292. goto retry_find;
  1293. /*
  1294. * An error return from page_cache_read can result if the
  1295. * system is low on memory, or a problem occurs while trying
  1296. * to schedule I/O.
  1297. */
  1298. if (error == -ENOMEM)
  1299. return VM_FAULT_OOM;
  1300. return VM_FAULT_SIGBUS;
  1301. page_not_uptodate:
  1302. /* IO error path */
  1303. if (!did_readaround) {
  1304. ret = VM_FAULT_MAJOR;
  1305. count_vm_event(PGMAJFAULT);
  1306. }
  1307. /*
  1308. * Umm, take care of errors if the page isn't up-to-date.
  1309. * Try to re-read it _once_. We do this synchronously,
  1310. * because there really aren't any performance issues here
  1311. * and we need to check for errors.
  1312. */
  1313. ClearPageError(page);
  1314. error = mapping->a_ops->readpage(file, page);
  1315. page_cache_release(page);
  1316. if (!error || error == AOP_TRUNCATED_PAGE)
  1317. goto retry_find;
  1318. /* Things didn't work out. Return zero to tell the mm layer so. */
  1319. shrink_readahead_size_eio(file, ra);
  1320. return VM_FAULT_SIGBUS;
  1321. }
  1322. EXPORT_SYMBOL(filemap_fault);
  1323. struct vm_operations_struct generic_file_vm_ops = {
  1324. .fault = filemap_fault,
  1325. };
  1326. /* This is used for a general mmap of a disk file */
  1327. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1328. {
  1329. struct address_space *mapping = file->f_mapping;
  1330. if (!mapping->a_ops->readpage)
  1331. return -ENOEXEC;
  1332. file_accessed(file);
  1333. vma->vm_ops = &generic_file_vm_ops;
  1334. vma->vm_flags |= VM_CAN_NONLINEAR;
  1335. return 0;
  1336. }
  1337. /*
  1338. * This is for filesystems which do not implement ->writepage.
  1339. */
  1340. int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
  1341. {
  1342. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
  1343. return -EINVAL;
  1344. return generic_file_mmap(file, vma);
  1345. }
  1346. #else
  1347. int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
  1348. {
  1349. return -ENOSYS;
  1350. }
  1351. int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma)
  1352. {
  1353. return -ENOSYS;
  1354. }
  1355. #endif /* CONFIG_MMU */
  1356. EXPORT_SYMBOL(generic_file_mmap);
  1357. EXPORT_SYMBOL(generic_file_readonly_mmap);
  1358. static struct page *__read_cache_page(struct address_space *mapping,
  1359. unsigned long index,
  1360. int (*filler)(void *,struct page*),
  1361. void *data)
  1362. {
  1363. struct page *page, *cached_page = NULL;
  1364. int err;
  1365. repeat:
  1366. page = find_get_page(mapping, index);
  1367. if (!page) {
  1368. if (!cached_page) {
  1369. cached_page = page_cache_alloc_cold(mapping);
  1370. if (!cached_page)
  1371. return ERR_PTR(-ENOMEM);
  1372. }
  1373. err = add_to_page_cache_lru(cached_page, mapping,
  1374. index, GFP_KERNEL);
  1375. if (err == -EEXIST)
  1376. goto repeat;
  1377. if (err < 0) {
  1378. /* Presumably ENOMEM for radix tree node */
  1379. page_cache_release(cached_page);
  1380. return ERR_PTR(err);
  1381. }
  1382. page = cached_page;
  1383. cached_page = NULL;
  1384. err = filler(data, page);
  1385. if (err < 0) {
  1386. page_cache_release(page);
  1387. page = ERR_PTR(err);
  1388. }
  1389. }
  1390. if (cached_page)
  1391. page_cache_release(cached_page);
  1392. return page;
  1393. }
  1394. /*
  1395. * Same as read_cache_page, but don't wait for page to become unlocked
  1396. * after submitting it to the filler.
  1397. */
  1398. struct page *read_cache_page_async(struct address_space *mapping,
  1399. unsigned long index,
  1400. int (*filler)(void *,struct page*),
  1401. void *data)
  1402. {
  1403. struct page *page;
  1404. int err;
  1405. retry:
  1406. page = __read_cache_page(mapping, index, filler, data);
  1407. if (IS_ERR(page))
  1408. return page;
  1409. if (PageUptodate(page))
  1410. goto out;
  1411. lock_page(page);
  1412. if (!page->mapping) {
  1413. unlock_page(page);
  1414. page_cache_release(page);
  1415. goto retry;
  1416. }
  1417. if (PageUptodate(page)) {
  1418. unlock_page(page);
  1419. goto out;
  1420. }
  1421. err = filler(data, page);
  1422. if (err < 0) {
  1423. page_cache_release(page);
  1424. return ERR_PTR(err);
  1425. }
  1426. out:
  1427. mark_page_accessed(page);
  1428. return page;
  1429. }
  1430. EXPORT_SYMBOL(read_cache_page_async);
  1431. /**
  1432. * read_cache_page - read into page cache, fill it if needed
  1433. * @mapping: the page's address_space
  1434. * @index: the page index
  1435. * @filler: function to perform the read
  1436. * @data: destination for read data
  1437. *
  1438. * Read into the page cache. If a page already exists, and PageUptodate() is
  1439. * not set, try to fill the page then wait for it to become unlocked.
  1440. *
  1441. * If the page does not get brought uptodate, return -EIO.
  1442. */
  1443. struct page *read_cache_page(struct address_space *mapping,
  1444. unsigned long index,
  1445. int (*filler)(void *,struct page*),
  1446. void *data)
  1447. {
  1448. struct page *page;
  1449. page = read_cache_page_async(mapping, index, filler, data);
  1450. if (IS_ERR(page))
  1451. goto out;
  1452. wait_on_page_locked(page);
  1453. if (!PageUptodate(page)) {
  1454. page_cache_release(page);
  1455. page = ERR_PTR(-EIO);
  1456. }
  1457. out:
  1458. return page;
  1459. }
  1460. EXPORT_SYMBOL(read_cache_page);
  1461. /*
  1462. * If the page was newly created, increment its refcount and add it to the
  1463. * caller's lru-buffering pagevec. This function is specifically for
  1464. * generic_file_write().
  1465. */
  1466. static inline struct page *
  1467. __grab_cache_page(struct address_space *mapping, unsigned long index,
  1468. struct page **cached_page, struct pagevec *lru_pvec)
  1469. {
  1470. int err;
  1471. struct page *page;
  1472. repeat:
  1473. page = find_lock_page(mapping, index);
  1474. if (!page) {
  1475. if (!*cached_page) {
  1476. *cached_page = page_cache_alloc(mapping);
  1477. if (!*cached_page)
  1478. return NULL;
  1479. }
  1480. err = add_to_page_cache(*cached_page, mapping,
  1481. index, GFP_KERNEL);
  1482. if (err == -EEXIST)
  1483. goto repeat;
  1484. if (err == 0) {
  1485. page = *cached_page;
  1486. page_cache_get(page);
  1487. if (!pagevec_add(lru_pvec, page))
  1488. __pagevec_lru_add(lru_pvec);
  1489. *cached_page = NULL;
  1490. }
  1491. }
  1492. return page;
  1493. }
  1494. /*
  1495. * The logic we want is
  1496. *
  1497. * if suid or (sgid and xgrp)
  1498. * remove privs
  1499. */
  1500. int should_remove_suid(struct dentry *dentry)
  1501. {
  1502. mode_t mode = dentry->d_inode->i_mode;
  1503. int kill = 0;
  1504. /* suid always must be killed */
  1505. if (unlikely(mode & S_ISUID))
  1506. kill = ATTR_KILL_SUID;
  1507. /*
  1508. * sgid without any exec bits is just a mandatory locking mark; leave
  1509. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1510. */
  1511. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1512. kill |= ATTR_KILL_SGID;
  1513. if (unlikely(kill && !capable(CAP_FSETID)))
  1514. return kill;
  1515. return 0;
  1516. }
  1517. EXPORT_SYMBOL(should_remove_suid);
  1518. int __remove_suid(struct dentry *dentry, int kill)
  1519. {
  1520. struct iattr newattrs;
  1521. newattrs.ia_valid = ATTR_FORCE | kill;
  1522. return notify_change(dentry, &newattrs);
  1523. }
  1524. int remove_suid(struct dentry *dentry)
  1525. {
  1526. int kill = should_remove_suid(dentry);
  1527. if (unlikely(kill))
  1528. return __remove_suid(dentry, kill);
  1529. return 0;
  1530. }
  1531. EXPORT_SYMBOL(remove_suid);
  1532. size_t
  1533. __filemap_copy_from_user_iovec_inatomic(char *vaddr,
  1534. const struct iovec *iov, size_t base, size_t bytes)
  1535. {
  1536. size_t copied = 0, left = 0;
  1537. while (bytes) {
  1538. char __user *buf = iov->iov_base + base;
  1539. int copy = min(bytes, iov->iov_len - base);
  1540. base = 0;
  1541. left = __copy_from_user_inatomic_nocache(vaddr, buf, copy);
  1542. copied += copy;
  1543. bytes -= copy;
  1544. vaddr += copy;
  1545. iov++;
  1546. if (unlikely(left))
  1547. break;
  1548. }
  1549. return copied - left;
  1550. }
  1551. /*
  1552. * Performs necessary checks before doing a write
  1553. *
  1554. * Can adjust writing position or amount of bytes to write.
  1555. * Returns appropriate error code that caller should return or
  1556. * zero in case that write should be allowed.
  1557. */
  1558. inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, int isblk)
  1559. {
  1560. struct inode *inode = file->f_mapping->host;
  1561. unsigned long limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1562. if (unlikely(*pos < 0))
  1563. return -EINVAL;
  1564. if (!isblk) {
  1565. /* FIXME: this is for backwards compatibility with 2.4 */
  1566. if (file->f_flags & O_APPEND)
  1567. *pos = i_size_read(inode);
  1568. if (limit != RLIM_INFINITY) {
  1569. if (*pos >= limit) {
  1570. send_sig(SIGXFSZ, current, 0);
  1571. return -EFBIG;
  1572. }
  1573. if (*count > limit - (typeof(limit))*pos) {
  1574. *count = limit - (typeof(limit))*pos;
  1575. }
  1576. }
  1577. }
  1578. /*
  1579. * LFS rule
  1580. */
  1581. if (unlikely(*pos + *count > MAX_NON_LFS &&
  1582. !(file->f_flags & O_LARGEFILE))) {
  1583. if (*pos >= MAX_NON_LFS) {
  1584. return -EFBIG;
  1585. }
  1586. if (*count > MAX_NON_LFS - (unsigned long)*pos) {
  1587. *count = MAX_NON_LFS - (unsigned long)*pos;
  1588. }
  1589. }
  1590. /*
  1591. * Are we about to exceed the fs block limit ?
  1592. *
  1593. * If we have written data it becomes a short write. If we have
  1594. * exceeded without writing data we send a signal and return EFBIG.
  1595. * Linus frestrict idea will clean these up nicely..
  1596. */
  1597. if (likely(!isblk)) {
  1598. if (unlikely(*pos >= inode->i_sb->s_maxbytes)) {
  1599. if (*count || *pos > inode->i_sb->s_maxbytes) {
  1600. return -EFBIG;
  1601. }
  1602. /* zero-length writes at ->s_maxbytes are OK */
  1603. }
  1604. if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
  1605. *count = inode->i_sb->s_maxbytes - *pos;
  1606. } else {
  1607. #ifdef CONFIG_BLOCK
  1608. loff_t isize;
  1609. if (bdev_read_only(I_BDEV(inode)))
  1610. return -EPERM;
  1611. isize = i_size_read(inode);
  1612. if (*pos >= isize) {
  1613. if (*count || *pos > isize)
  1614. return -ENOSPC;
  1615. }
  1616. if (*pos + *count > isize)
  1617. *count = isize - *pos;
  1618. #else
  1619. return -EPERM;
  1620. #endif
  1621. }
  1622. return 0;
  1623. }
  1624. EXPORT_SYMBOL(generic_write_checks);
  1625. ssize_t
  1626. generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  1627. unsigned long *nr_segs, loff_t pos, loff_t *ppos,
  1628. size_t count, size_t ocount)
  1629. {
  1630. struct file *file = iocb->ki_filp;
  1631. struct address_space *mapping = file->f_mapping;
  1632. struct inode *inode = mapping->host;
  1633. ssize_t written;
  1634. if (count != ocount)
  1635. *nr_segs = iov_shorten((struct iovec *)iov, *nr_segs, count);
  1636. written = generic_file_direct_IO(WRITE, iocb, iov, pos, *nr_segs);
  1637. if (written > 0) {
  1638. loff_t end = pos + written;
  1639. if (end > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
  1640. i_size_write(inode, end);
  1641. mark_inode_dirty(inode);
  1642. }
  1643. *ppos = end;
  1644. }
  1645. /*
  1646. * Sync the fs metadata but not the minor inode changes and
  1647. * of course not the data as we did direct DMA for the IO.
  1648. * i_mutex is held, which protects generic_osync_inode() from
  1649. * livelocking. AIO O_DIRECT ops attempt to sync metadata here.
  1650. */
  1651. if ((written >= 0 || written == -EIOCBQUEUED) &&
  1652. ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1653. int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
  1654. if (err < 0)
  1655. written = err;
  1656. }
  1657. return written;
  1658. }
  1659. EXPORT_SYMBOL(generic_file_direct_write);
  1660. ssize_t
  1661. generic_file_buffered_write(struct kiocb *iocb, const struct iovec *iov,
  1662. unsigned long nr_segs, loff_t pos, loff_t *ppos,
  1663. size_t count, ssize_t written)
  1664. {
  1665. struct file *file = iocb->ki_filp;
  1666. struct address_space * mapping = file->f_mapping;
  1667. const struct address_space_operations *a_ops = mapping->a_ops;
  1668. struct inode *inode = mapping->host;
  1669. long status = 0;
  1670. struct page *page;
  1671. struct page *cached_page = NULL;
  1672. size_t bytes;
  1673. struct pagevec lru_pvec;
  1674. const struct iovec *cur_iov = iov; /* current iovec */
  1675. size_t iov_base = 0; /* offset in the current iovec */
  1676. char __user *buf;
  1677. pagevec_init(&lru_pvec, 0);
  1678. /*
  1679. * handle partial DIO write. Adjust cur_iov if needed.
  1680. */
  1681. if (likely(nr_segs == 1))
  1682. buf = iov->iov_base + written;
  1683. else {
  1684. filemap_set_next_iovec(&cur_iov, &iov_base, written);
  1685. buf = cur_iov->iov_base + iov_base;
  1686. }
  1687. do {
  1688. unsigned long index;
  1689. unsigned long offset;
  1690. size_t copied;
  1691. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1692. index = pos >> PAGE_CACHE_SHIFT;
  1693. bytes = PAGE_CACHE_SIZE - offset;
  1694. /* Limit the size of the copy to the caller's write size */
  1695. bytes = min(bytes, count);
  1696. /* We only need to worry about prefaulting when writes are from
  1697. * user-space. NFSd uses vfs_writev with several non-aligned
  1698. * segments in the vector, and limiting to one segment a time is
  1699. * a noticeable performance for re-write
  1700. */
  1701. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1702. /*
  1703. * Limit the size of the copy to that of the current
  1704. * segment, because fault_in_pages_readable() doesn't
  1705. * know how to walk segments.
  1706. */
  1707. bytes = min(bytes, cur_iov->iov_len - iov_base);
  1708. /*
  1709. * Bring in the user page that we will copy from
  1710. * _first_. Otherwise there's a nasty deadlock on
  1711. * copying from the same page as we're writing to,
  1712. * without it being marked up-to-date.
  1713. */
  1714. fault_in_pages_readable(buf, bytes);
  1715. }
  1716. page = __grab_cache_page(mapping,index,&cached_page,&lru_pvec);
  1717. if (!page) {
  1718. status = -ENOMEM;
  1719. break;
  1720. }
  1721. if (unlikely(bytes == 0)) {
  1722. status = 0;
  1723. copied = 0;
  1724. goto zero_length_segment;
  1725. }
  1726. status = a_ops->prepare_write(file, page, offset, offset+bytes);
  1727. if (unlikely(status)) {
  1728. loff_t isize = i_size_read(inode);
  1729. if (status != AOP_TRUNCATED_PAGE)
  1730. unlock_page(page);
  1731. page_cache_release(page);
  1732. if (status == AOP_TRUNCATED_PAGE)
  1733. continue;
  1734. /*
  1735. * prepare_write() may have instantiated a few blocks
  1736. * outside i_size. Trim these off again.
  1737. */
  1738. if (pos + bytes > isize)
  1739. vmtruncate(inode, isize);
  1740. break;
  1741. }
  1742. if (likely(nr_segs == 1))
  1743. copied = filemap_copy_from_user(page, offset,
  1744. buf, bytes);
  1745. else
  1746. copied = filemap_copy_from_user_iovec(page, offset,
  1747. cur_iov, iov_base, bytes);
  1748. flush_dcache_page(page);
  1749. status = a_ops->commit_write(file, page, offset, offset+bytes);
  1750. if (status == AOP_TRUNCATED_PAGE) {
  1751. page_cache_release(page);
  1752. continue;
  1753. }
  1754. zero_length_segment:
  1755. if (likely(copied >= 0)) {
  1756. if (!status)
  1757. status = copied;
  1758. if (status >= 0) {
  1759. written += status;
  1760. count -= status;
  1761. pos += status;
  1762. buf += status;
  1763. if (unlikely(nr_segs > 1)) {
  1764. filemap_set_next_iovec(&cur_iov,
  1765. &iov_base, status);
  1766. if (count)
  1767. buf = cur_iov->iov_base +
  1768. iov_base;
  1769. } else {
  1770. iov_base += status;
  1771. }
  1772. }
  1773. }
  1774. if (unlikely(copied != bytes))
  1775. if (status >= 0)
  1776. status = -EFAULT;
  1777. unlock_page(page);
  1778. mark_page_accessed(page);
  1779. page_cache_release(page);
  1780. if (status < 0)
  1781. break;
  1782. balance_dirty_pages_ratelimited(mapping);
  1783. cond_resched();
  1784. } while (count);
  1785. *ppos = pos;
  1786. if (cached_page)
  1787. page_cache_release(cached_page);
  1788. /*
  1789. * For now, when the user asks for O_SYNC, we'll actually give O_DSYNC
  1790. */
  1791. if (likely(status >= 0)) {
  1792. if (unlikely((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1793. if (!a_ops->writepage || !is_sync_kiocb(iocb))
  1794. status = generic_osync_inode(inode, mapping,
  1795. OSYNC_METADATA|OSYNC_DATA);
  1796. }
  1797. }
  1798. /*
  1799. * If we get here for O_DIRECT writes then we must have fallen through
  1800. * to buffered writes (block instantiation inside i_size). So we sync
  1801. * the file data here, to try to honour O_DIRECT expectations.
  1802. */
  1803. if (unlikely(file->f_flags & O_DIRECT) && written)
  1804. status = filemap_write_and_wait(mapping);
  1805. pagevec_lru_add(&lru_pvec);
  1806. return written ? written : status;
  1807. }
  1808. EXPORT_SYMBOL(generic_file_buffered_write);
  1809. static ssize_t
  1810. __generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
  1811. unsigned long nr_segs, loff_t *ppos)
  1812. {
  1813. struct file *file = iocb->ki_filp;
  1814. struct address_space * mapping = file->f_mapping;
  1815. size_t ocount; /* original count */
  1816. size_t count; /* after file limit checks */
  1817. struct inode *inode = mapping->host;
  1818. loff_t pos;
  1819. ssize_t written;
  1820. ssize_t err;
  1821. ocount = 0;
  1822. err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1823. if (err)
  1824. return err;
  1825. count = ocount;
  1826. pos = *ppos;
  1827. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1828. /* We can write back this queue in page reclaim */
  1829. current->backing_dev_info = mapping->backing_dev_info;
  1830. written = 0;
  1831. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1832. if (err)
  1833. goto out;
  1834. if (count == 0)
  1835. goto out;
  1836. err = remove_suid(file->f_path.dentry);
  1837. if (err)
  1838. goto out;
  1839. file_update_time(file);
  1840. /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
  1841. if (unlikely(file->f_flags & O_DIRECT)) {
  1842. loff_t endbyte;
  1843. ssize_t written_buffered;
  1844. written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
  1845. ppos, count, ocount);
  1846. if (written < 0 || written == count)
  1847. goto out;
  1848. /*
  1849. * direct-io write to a hole: fall through to buffered I/O
  1850. * for completing the rest of the request.
  1851. */
  1852. pos += written;
  1853. count -= written;
  1854. written_buffered = generic_file_buffered_write(iocb, iov,
  1855. nr_segs, pos, ppos, count,
  1856. written);
  1857. /*
  1858. * If generic_file_buffered_write() retuned a synchronous error
  1859. * then we want to return the number of bytes which were
  1860. * direct-written, or the error code if that was zero. Note
  1861. * that this differs from normal direct-io semantics, which
  1862. * will return -EFOO even if some bytes were written.
  1863. */
  1864. if (written_buffered < 0) {
  1865. err = written_buffered;
  1866. goto out;
  1867. }
  1868. /*
  1869. * We need to ensure that the page cache pages are written to
  1870. * disk and invalidated to preserve the expected O_DIRECT
  1871. * semantics.
  1872. */
  1873. endbyte = pos + written_buffered - written - 1;
  1874. err = do_sync_mapping_range(file->f_mapping, pos, endbyte,
  1875. SYNC_FILE_RANGE_WAIT_BEFORE|
  1876. SYNC_FILE_RANGE_WRITE|
  1877. SYNC_FILE_RANGE_WAIT_AFTER);
  1878. if (err == 0) {
  1879. written = written_buffered;
  1880. invalidate_mapping_pages(mapping,
  1881. pos >> PAGE_CACHE_SHIFT,
  1882. endbyte >> PAGE_CACHE_SHIFT);
  1883. } else {
  1884. /*
  1885. * We don't know how much we wrote, so just return
  1886. * the number of bytes which were direct-written
  1887. */
  1888. }
  1889. } else {
  1890. written = generic_file_buffered_write(iocb, iov, nr_segs,
  1891. pos, ppos, count, written);
  1892. }
  1893. out:
  1894. current->backing_dev_info = NULL;
  1895. return written ? written : err;
  1896. }
  1897. ssize_t generic_file_aio_write_nolock(struct kiocb *iocb,
  1898. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1899. {
  1900. struct file *file = iocb->ki_filp;
  1901. struct address_space *mapping = file->f_mapping;
  1902. struct inode *inode = mapping->host;
  1903. ssize_t ret;
  1904. BUG_ON(iocb->ki_pos != pos);
  1905. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1906. &iocb->ki_pos);
  1907. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1908. ssize_t err;
  1909. err = sync_page_range_nolock(inode, mapping, pos, ret);
  1910. if (err < 0)
  1911. ret = err;
  1912. }
  1913. return ret;
  1914. }
  1915. EXPORT_SYMBOL(generic_file_aio_write_nolock);
  1916. ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1917. unsigned long nr_segs, loff_t pos)
  1918. {
  1919. struct file *file = iocb->ki_filp;
  1920. struct address_space *mapping = file->f_mapping;
  1921. struct inode *inode = mapping->host;
  1922. ssize_t ret;
  1923. BUG_ON(iocb->ki_pos != pos);
  1924. mutex_lock(&inode->i_mutex);
  1925. ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1926. &iocb->ki_pos);
  1927. mutex_unlock(&inode->i_mutex);
  1928. if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1929. ssize_t err;
  1930. err = sync_page_range(inode, mapping, pos, ret);
  1931. if (err < 0)
  1932. ret = err;
  1933. }
  1934. return ret;
  1935. }
  1936. EXPORT_SYMBOL(generic_file_aio_write);
  1937. /*
  1938. * Called under i_mutex for writes to S_ISREG files. Returns -EIO if something
  1939. * went wrong during pagecache shootdown.
  1940. */
  1941. static ssize_t
  1942. generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  1943. loff_t offset, unsigned long nr_segs)
  1944. {
  1945. struct file *file = iocb->ki_filp;
  1946. struct address_space *mapping = file->f_mapping;
  1947. ssize_t retval;
  1948. size_t write_len;
  1949. pgoff_t end = 0; /* silence gcc */
  1950. /*
  1951. * If it's a write, unmap all mmappings of the file up-front. This
  1952. * will cause any pte dirty bits to be propagated into the pageframes
  1953. * for the subsequent filemap_write_and_wait().
  1954. */
  1955. if (rw == WRITE) {
  1956. write_len = iov_length(iov, nr_segs);
  1957. end = (offset + write_len - 1) >> PAGE_CACHE_SHIFT;
  1958. if (mapping_mapped(mapping))
  1959. unmap_mapping_range(mapping, offset, write_len, 0);
  1960. }
  1961. retval = filemap_write_and_wait(mapping);
  1962. if (retval)
  1963. goto out;
  1964. /*
  1965. * After a write we want buffered reads to be sure to go to disk to get
  1966. * the new data. We invalidate clean cached page from the region we're
  1967. * about to write. We do this *before* the write so that we can return
  1968. * -EIO without clobbering -EIOCBQUEUED from ->direct_IO().
  1969. */
  1970. if (rw == WRITE && mapping->nrpages) {
  1971. retval = invalidate_inode_pages2_range(mapping,
  1972. offset >> PAGE_CACHE_SHIFT, end);
  1973. if (retval)
  1974. goto out;
  1975. }
  1976. retval = mapping->a_ops->direct_IO(rw, iocb, iov, offset, nr_segs);
  1977. if (retval)
  1978. goto out;
  1979. /*
  1980. * Finally, try again to invalidate clean pages which might have been
  1981. * faulted in by get_user_pages() if the source of the write was an
  1982. * mmap()ed region of the file we're writing. That's a pretty crazy
  1983. * thing to do, so we don't support it 100%. If this invalidation
  1984. * fails and we have -EIOCBQUEUED we ignore the failure.
  1985. */
  1986. if (rw == WRITE && mapping->nrpages) {
  1987. int err = invalidate_inode_pages2_range(mapping,
  1988. offset >> PAGE_CACHE_SHIFT, end);
  1989. if (err && retval >= 0)
  1990. retval = err;
  1991. }
  1992. out:
  1993. return retval;
  1994. }
  1995. /**
  1996. * try_to_release_page() - release old fs-specific metadata on a page
  1997. *
  1998. * @page: the page which the kernel is trying to free
  1999. * @gfp_mask: memory allocation flags (and I/O mode)
  2000. *
  2001. * The address_space is to try to release any data against the page
  2002. * (presumably at page->private). If the release was successful, return `1'.
  2003. * Otherwise return zero.
  2004. *
  2005. * The @gfp_mask argument specifies whether I/O may be performed to release
  2006. * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
  2007. *
  2008. * NOTE: @gfp_mask may go away, and this function may become non-blocking.
  2009. */
  2010. int try_to_release_page(struct page *page, gfp_t gfp_mask)
  2011. {
  2012. struct address_space * const mapping = page->mapping;
  2013. BUG_ON(!PageLocked(page));
  2014. if (PageWriteback(page))
  2015. return 0;
  2016. if (mapping && mapping->a_ops->releasepage)
  2017. return mapping->a_ops->releasepage(page, gfp_mask);
  2018. return try_to_free_buffers(page);
  2019. }
  2020. EXPORT_SYMBOL(try_to_release_page);