filemap.c 59 KB

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