filemap.c 58 KB

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