filemap.c 57 KB

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