filemap.c 68 KB

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