filemap.c 65 KB

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