filemap.c 57 KB

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