filemap.c 58 KB

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