buffer.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592
  1. /*
  2. * linux/fs/buffer.c
  3. *
  4. * Copyright (C) 1991, 1992, 2002 Linus Torvalds
  5. */
  6. /*
  7. * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
  8. *
  9. * Removed a lot of unnecessary code and simplified things now that
  10. * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
  11. *
  12. * Speed up hash, lru, and free list operations. Use gfp() for allocating
  13. * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
  14. *
  15. * Added 32k buffer block sizes - these are required older ARM systems. - RMK
  16. *
  17. * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/fs.h>
  23. #include <linux/iomap.h>
  24. #include <linux/mm.h>
  25. #include <linux/percpu.h>
  26. #include <linux/slab.h>
  27. #include <linux/capability.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/file.h>
  30. #include <linux/quotaops.h>
  31. #include <linux/highmem.h>
  32. #include <linux/export.h>
  33. #include <linux/backing-dev.h>
  34. #include <linux/writeback.h>
  35. #include <linux/hash.h>
  36. #include <linux/suspend.h>
  37. #include <linux/buffer_head.h>
  38. #include <linux/task_io_accounting_ops.h>
  39. #include <linux/bio.h>
  40. #include <linux/notifier.h>
  41. #include <linux/cpu.h>
  42. #include <linux/bitops.h>
  43. #include <linux/mpage.h>
  44. #include <linux/bit_spinlock.h>
  45. #include <linux/pagevec.h>
  46. #include <trace/events/block.h>
  47. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
  48. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  49. enum rw_hint hint, struct writeback_control *wbc);
  50. #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
  51. void init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
  52. {
  53. bh->b_end_io = handler;
  54. bh->b_private = private;
  55. }
  56. EXPORT_SYMBOL(init_buffer);
  57. inline void touch_buffer(struct buffer_head *bh)
  58. {
  59. trace_block_touch_buffer(bh);
  60. mark_page_accessed(bh->b_page);
  61. }
  62. EXPORT_SYMBOL(touch_buffer);
  63. void __lock_buffer(struct buffer_head *bh)
  64. {
  65. wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
  66. }
  67. EXPORT_SYMBOL(__lock_buffer);
  68. void unlock_buffer(struct buffer_head *bh)
  69. {
  70. clear_bit_unlock(BH_Lock, &bh->b_state);
  71. smp_mb__after_atomic();
  72. wake_up_bit(&bh->b_state, BH_Lock);
  73. }
  74. EXPORT_SYMBOL(unlock_buffer);
  75. /*
  76. * Returns if the page has dirty or writeback buffers. If all the buffers
  77. * are unlocked and clean then the PageDirty information is stale. If
  78. * any of the pages are locked, it is assumed they are locked for IO.
  79. */
  80. void buffer_check_dirty_writeback(struct page *page,
  81. bool *dirty, bool *writeback)
  82. {
  83. struct buffer_head *head, *bh;
  84. *dirty = false;
  85. *writeback = false;
  86. BUG_ON(!PageLocked(page));
  87. if (!page_has_buffers(page))
  88. return;
  89. if (PageWriteback(page))
  90. *writeback = true;
  91. head = page_buffers(page);
  92. bh = head;
  93. do {
  94. if (buffer_locked(bh))
  95. *writeback = true;
  96. if (buffer_dirty(bh))
  97. *dirty = true;
  98. bh = bh->b_this_page;
  99. } while (bh != head);
  100. }
  101. EXPORT_SYMBOL(buffer_check_dirty_writeback);
  102. /*
  103. * Block until a buffer comes unlocked. This doesn't stop it
  104. * from becoming locked again - you have to lock it yourself
  105. * if you want to preserve its state.
  106. */
  107. void __wait_on_buffer(struct buffer_head * bh)
  108. {
  109. wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
  110. }
  111. EXPORT_SYMBOL(__wait_on_buffer);
  112. static void
  113. __clear_page_buffers(struct page *page)
  114. {
  115. ClearPagePrivate(page);
  116. set_page_private(page, 0);
  117. put_page(page);
  118. }
  119. static void buffer_io_error(struct buffer_head *bh, char *msg)
  120. {
  121. if (!test_bit(BH_Quiet, &bh->b_state))
  122. printk_ratelimited(KERN_ERR
  123. "Buffer I/O error on dev %pg, logical block %llu%s\n",
  124. bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
  125. }
  126. /*
  127. * End-of-IO handler helper function which does not touch the bh after
  128. * unlocking it.
  129. * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
  130. * a race there is benign: unlock_buffer() only use the bh's address for
  131. * hashing after unlocking the buffer, so it doesn't actually touch the bh
  132. * itself.
  133. */
  134. static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
  135. {
  136. if (uptodate) {
  137. set_buffer_uptodate(bh);
  138. } else {
  139. /* This happens, due to failed read-ahead attempts. */
  140. clear_buffer_uptodate(bh);
  141. }
  142. unlock_buffer(bh);
  143. }
  144. /*
  145. * Default synchronous end-of-IO handler.. Just mark it up-to-date and
  146. * unlock the buffer. This is what ll_rw_block uses too.
  147. */
  148. void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
  149. {
  150. __end_buffer_read_notouch(bh, uptodate);
  151. put_bh(bh);
  152. }
  153. EXPORT_SYMBOL(end_buffer_read_sync);
  154. void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  155. {
  156. if (uptodate) {
  157. set_buffer_uptodate(bh);
  158. } else {
  159. buffer_io_error(bh, ", lost sync page write");
  160. mark_buffer_write_io_error(bh);
  161. clear_buffer_uptodate(bh);
  162. }
  163. unlock_buffer(bh);
  164. put_bh(bh);
  165. }
  166. EXPORT_SYMBOL(end_buffer_write_sync);
  167. /*
  168. * Various filesystems appear to want __find_get_block to be non-blocking.
  169. * But it's the page lock which protects the buffers. To get around this,
  170. * we get exclusion from try_to_free_buffers with the blockdev mapping's
  171. * private_lock.
  172. *
  173. * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
  174. * may be quite high. This code could TryLock the page, and if that
  175. * succeeds, there is no need to take private_lock. (But if
  176. * private_lock is contended then so is mapping->tree_lock).
  177. */
  178. static struct buffer_head *
  179. __find_get_block_slow(struct block_device *bdev, sector_t block)
  180. {
  181. struct inode *bd_inode = bdev->bd_inode;
  182. struct address_space *bd_mapping = bd_inode->i_mapping;
  183. struct buffer_head *ret = NULL;
  184. pgoff_t index;
  185. struct buffer_head *bh;
  186. struct buffer_head *head;
  187. struct page *page;
  188. int all_mapped = 1;
  189. index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
  190. page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
  191. if (!page)
  192. goto out;
  193. spin_lock(&bd_mapping->private_lock);
  194. if (!page_has_buffers(page))
  195. goto out_unlock;
  196. head = page_buffers(page);
  197. bh = head;
  198. do {
  199. if (!buffer_mapped(bh))
  200. all_mapped = 0;
  201. else if (bh->b_blocknr == block) {
  202. ret = bh;
  203. get_bh(bh);
  204. goto out_unlock;
  205. }
  206. bh = bh->b_this_page;
  207. } while (bh != head);
  208. /* we might be here because some of the buffers on this page are
  209. * not mapped. This is due to various races between
  210. * file io on the block device and getblk. It gets dealt with
  211. * elsewhere, don't buffer_error if we had some unmapped buffers
  212. */
  213. if (all_mapped) {
  214. printk("__find_get_block_slow() failed. "
  215. "block=%llu, b_blocknr=%llu\n",
  216. (unsigned long long)block,
  217. (unsigned long long)bh->b_blocknr);
  218. printk("b_state=0x%08lx, b_size=%zu\n",
  219. bh->b_state, bh->b_size);
  220. printk("device %pg blocksize: %d\n", bdev,
  221. 1 << bd_inode->i_blkbits);
  222. }
  223. out_unlock:
  224. spin_unlock(&bd_mapping->private_lock);
  225. put_page(page);
  226. out:
  227. return ret;
  228. }
  229. /*
  230. * I/O completion handler for block_read_full_page() - pages
  231. * which come unlocked at the end of I/O.
  232. */
  233. static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  234. {
  235. unsigned long flags;
  236. struct buffer_head *first;
  237. struct buffer_head *tmp;
  238. struct page *page;
  239. int page_uptodate = 1;
  240. BUG_ON(!buffer_async_read(bh));
  241. page = bh->b_page;
  242. if (uptodate) {
  243. set_buffer_uptodate(bh);
  244. } else {
  245. clear_buffer_uptodate(bh);
  246. buffer_io_error(bh, ", async page read");
  247. SetPageError(page);
  248. }
  249. /*
  250. * Be _very_ careful from here on. Bad things can happen if
  251. * two buffer heads end IO at almost the same time and both
  252. * decide that the page is now completely done.
  253. */
  254. first = page_buffers(page);
  255. local_irq_save(flags);
  256. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  257. clear_buffer_async_read(bh);
  258. unlock_buffer(bh);
  259. tmp = bh;
  260. do {
  261. if (!buffer_uptodate(tmp))
  262. page_uptodate = 0;
  263. if (buffer_async_read(tmp)) {
  264. BUG_ON(!buffer_locked(tmp));
  265. goto still_busy;
  266. }
  267. tmp = tmp->b_this_page;
  268. } while (tmp != bh);
  269. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  270. local_irq_restore(flags);
  271. /*
  272. * If none of the buffers had errors and they are all
  273. * uptodate then we can set the page uptodate.
  274. */
  275. if (page_uptodate && !PageError(page))
  276. SetPageUptodate(page);
  277. unlock_page(page);
  278. return;
  279. still_busy:
  280. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  281. local_irq_restore(flags);
  282. return;
  283. }
  284. /*
  285. * Completion handler for block_write_full_page() - pages which are unlocked
  286. * during I/O, and which have PageWriteback cleared upon I/O completion.
  287. */
  288. void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  289. {
  290. unsigned long flags;
  291. struct buffer_head *first;
  292. struct buffer_head *tmp;
  293. struct page *page;
  294. BUG_ON(!buffer_async_write(bh));
  295. page = bh->b_page;
  296. if (uptodate) {
  297. set_buffer_uptodate(bh);
  298. } else {
  299. buffer_io_error(bh, ", lost async page write");
  300. mark_buffer_write_io_error(bh);
  301. clear_buffer_uptodate(bh);
  302. SetPageError(page);
  303. }
  304. first = page_buffers(page);
  305. local_irq_save(flags);
  306. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  307. clear_buffer_async_write(bh);
  308. unlock_buffer(bh);
  309. tmp = bh->b_this_page;
  310. while (tmp != bh) {
  311. if (buffer_async_write(tmp)) {
  312. BUG_ON(!buffer_locked(tmp));
  313. goto still_busy;
  314. }
  315. tmp = tmp->b_this_page;
  316. }
  317. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  318. local_irq_restore(flags);
  319. end_page_writeback(page);
  320. return;
  321. still_busy:
  322. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  323. local_irq_restore(flags);
  324. return;
  325. }
  326. EXPORT_SYMBOL(end_buffer_async_write);
  327. /*
  328. * If a page's buffers are under async readin (end_buffer_async_read
  329. * completion) then there is a possibility that another thread of
  330. * control could lock one of the buffers after it has completed
  331. * but while some of the other buffers have not completed. This
  332. * locked buffer would confuse end_buffer_async_read() into not unlocking
  333. * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
  334. * that this buffer is not under async I/O.
  335. *
  336. * The page comes unlocked when it has no locked buffer_async buffers
  337. * left.
  338. *
  339. * PageLocked prevents anyone starting new async I/O reads any of
  340. * the buffers.
  341. *
  342. * PageWriteback is used to prevent simultaneous writeout of the same
  343. * page.
  344. *
  345. * PageLocked prevents anyone from starting writeback of a page which is
  346. * under read I/O (PageWriteback is only ever set against a locked page).
  347. */
  348. static void mark_buffer_async_read(struct buffer_head *bh)
  349. {
  350. bh->b_end_io = end_buffer_async_read;
  351. set_buffer_async_read(bh);
  352. }
  353. static void mark_buffer_async_write_endio(struct buffer_head *bh,
  354. bh_end_io_t *handler)
  355. {
  356. bh->b_end_io = handler;
  357. set_buffer_async_write(bh);
  358. }
  359. void mark_buffer_async_write(struct buffer_head *bh)
  360. {
  361. mark_buffer_async_write_endio(bh, end_buffer_async_write);
  362. }
  363. EXPORT_SYMBOL(mark_buffer_async_write);
  364. /*
  365. * fs/buffer.c contains helper functions for buffer-backed address space's
  366. * fsync functions. A common requirement for buffer-based filesystems is
  367. * that certain data from the backing blockdev needs to be written out for
  368. * a successful fsync(). For example, ext2 indirect blocks need to be
  369. * written back and waited upon before fsync() returns.
  370. *
  371. * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
  372. * inode_has_buffers() and invalidate_inode_buffers() are provided for the
  373. * management of a list of dependent buffers at ->i_mapping->private_list.
  374. *
  375. * Locking is a little subtle: try_to_free_buffers() will remove buffers
  376. * from their controlling inode's queue when they are being freed. But
  377. * try_to_free_buffers() will be operating against the *blockdev* mapping
  378. * at the time, not against the S_ISREG file which depends on those buffers.
  379. * So the locking for private_list is via the private_lock in the address_space
  380. * which backs the buffers. Which is different from the address_space
  381. * against which the buffers are listed. So for a particular address_space,
  382. * mapping->private_lock does *not* protect mapping->private_list! In fact,
  383. * mapping->private_list will always be protected by the backing blockdev's
  384. * ->private_lock.
  385. *
  386. * Which introduces a requirement: all buffers on an address_space's
  387. * ->private_list must be from the same address_space: the blockdev's.
  388. *
  389. * address_spaces which do not place buffers at ->private_list via these
  390. * utility functions are free to use private_lock and private_list for
  391. * whatever they want. The only requirement is that list_empty(private_list)
  392. * be true at clear_inode() time.
  393. *
  394. * FIXME: clear_inode should not call invalidate_inode_buffers(). The
  395. * filesystems should do that. invalidate_inode_buffers() should just go
  396. * BUG_ON(!list_empty).
  397. *
  398. * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
  399. * take an address_space, not an inode. And it should be called
  400. * mark_buffer_dirty_fsync() to clearly define why those buffers are being
  401. * queued up.
  402. *
  403. * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
  404. * list if it is already on a list. Because if the buffer is on a list,
  405. * it *must* already be on the right one. If not, the filesystem is being
  406. * silly. This will save a ton of locking. But first we have to ensure
  407. * that buffers are taken *off* the old inode's list when they are freed
  408. * (presumably in truncate). That requires careful auditing of all
  409. * filesystems (do it inside bforget()). It could also be done by bringing
  410. * b_inode back.
  411. */
  412. /*
  413. * The buffer's backing address_space's private_lock must be held
  414. */
  415. static void __remove_assoc_queue(struct buffer_head *bh)
  416. {
  417. list_del_init(&bh->b_assoc_buffers);
  418. WARN_ON(!bh->b_assoc_map);
  419. bh->b_assoc_map = NULL;
  420. }
  421. int inode_has_buffers(struct inode *inode)
  422. {
  423. return !list_empty(&inode->i_data.private_list);
  424. }
  425. /*
  426. * osync is designed to support O_SYNC io. It waits synchronously for
  427. * all already-submitted IO to complete, but does not queue any new
  428. * writes to the disk.
  429. *
  430. * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
  431. * you dirty the buffers, and then use osync_inode_buffers to wait for
  432. * completion. Any other dirty buffers which are not yet queued for
  433. * write will not be flushed to disk by the osync.
  434. */
  435. static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
  436. {
  437. struct buffer_head *bh;
  438. struct list_head *p;
  439. int err = 0;
  440. spin_lock(lock);
  441. repeat:
  442. list_for_each_prev(p, list) {
  443. bh = BH_ENTRY(p);
  444. if (buffer_locked(bh)) {
  445. get_bh(bh);
  446. spin_unlock(lock);
  447. wait_on_buffer(bh);
  448. if (!buffer_uptodate(bh))
  449. err = -EIO;
  450. brelse(bh);
  451. spin_lock(lock);
  452. goto repeat;
  453. }
  454. }
  455. spin_unlock(lock);
  456. return err;
  457. }
  458. static void do_thaw_one(struct super_block *sb, void *unused)
  459. {
  460. while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
  461. printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev);
  462. }
  463. static void do_thaw_all(struct work_struct *work)
  464. {
  465. iterate_supers(do_thaw_one, NULL);
  466. kfree(work);
  467. printk(KERN_WARNING "Emergency Thaw complete\n");
  468. }
  469. /**
  470. * emergency_thaw_all -- forcibly thaw every frozen filesystem
  471. *
  472. * Used for emergency unfreeze of all filesystems via SysRq
  473. */
  474. void emergency_thaw_all(void)
  475. {
  476. struct work_struct *work;
  477. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  478. if (work) {
  479. INIT_WORK(work, do_thaw_all);
  480. schedule_work(work);
  481. }
  482. }
  483. /**
  484. * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
  485. * @mapping: the mapping which wants those buffers written
  486. *
  487. * Starts I/O against the buffers at mapping->private_list, and waits upon
  488. * that I/O.
  489. *
  490. * Basically, this is a convenience function for fsync().
  491. * @mapping is a file or directory which needs those buffers to be written for
  492. * a successful fsync().
  493. */
  494. int sync_mapping_buffers(struct address_space *mapping)
  495. {
  496. struct address_space *buffer_mapping = mapping->private_data;
  497. if (buffer_mapping == NULL || list_empty(&mapping->private_list))
  498. return 0;
  499. return fsync_buffers_list(&buffer_mapping->private_lock,
  500. &mapping->private_list);
  501. }
  502. EXPORT_SYMBOL(sync_mapping_buffers);
  503. /*
  504. * Called when we've recently written block `bblock', and it is known that
  505. * `bblock' was for a buffer_boundary() buffer. This means that the block at
  506. * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
  507. * dirty, schedule it for IO. So that indirects merge nicely with their data.
  508. */
  509. void write_boundary_block(struct block_device *bdev,
  510. sector_t bblock, unsigned blocksize)
  511. {
  512. struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
  513. if (bh) {
  514. if (buffer_dirty(bh))
  515. ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
  516. put_bh(bh);
  517. }
  518. }
  519. void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
  520. {
  521. struct address_space *mapping = inode->i_mapping;
  522. struct address_space *buffer_mapping = bh->b_page->mapping;
  523. mark_buffer_dirty(bh);
  524. if (!mapping->private_data) {
  525. mapping->private_data = buffer_mapping;
  526. } else {
  527. BUG_ON(mapping->private_data != buffer_mapping);
  528. }
  529. if (!bh->b_assoc_map) {
  530. spin_lock(&buffer_mapping->private_lock);
  531. list_move_tail(&bh->b_assoc_buffers,
  532. &mapping->private_list);
  533. bh->b_assoc_map = mapping;
  534. spin_unlock(&buffer_mapping->private_lock);
  535. }
  536. }
  537. EXPORT_SYMBOL(mark_buffer_dirty_inode);
  538. /*
  539. * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
  540. * dirty.
  541. *
  542. * If warn is true, then emit a warning if the page is not uptodate and has
  543. * not been truncated.
  544. *
  545. * The caller must hold lock_page_memcg().
  546. */
  547. static void __set_page_dirty(struct page *page, struct address_space *mapping,
  548. int warn)
  549. {
  550. unsigned long flags;
  551. spin_lock_irqsave(&mapping->tree_lock, flags);
  552. if (page->mapping) { /* Race with truncate? */
  553. WARN_ON_ONCE(warn && !PageUptodate(page));
  554. account_page_dirtied(page, mapping);
  555. radix_tree_tag_set(&mapping->page_tree,
  556. page_index(page), PAGECACHE_TAG_DIRTY);
  557. }
  558. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  559. }
  560. /*
  561. * Add a page to the dirty page list.
  562. *
  563. * It is a sad fact of life that this function is called from several places
  564. * deeply under spinlocking. It may not sleep.
  565. *
  566. * If the page has buffers, the uptodate buffers are set dirty, to preserve
  567. * dirty-state coherency between the page and the buffers. It the page does
  568. * not have buffers then when they are later attached they will all be set
  569. * dirty.
  570. *
  571. * The buffers are dirtied before the page is dirtied. There's a small race
  572. * window in which a writepage caller may see the page cleanness but not the
  573. * buffer dirtiness. That's fine. If this code were to set the page dirty
  574. * before the buffers, a concurrent writepage caller could clear the page dirty
  575. * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
  576. * page on the dirty page list.
  577. *
  578. * We use private_lock to lock against try_to_free_buffers while using the
  579. * page's buffer list. Also use this to protect against clean buffers being
  580. * added to the page after it was set dirty.
  581. *
  582. * FIXME: may need to call ->reservepage here as well. That's rather up to the
  583. * address_space though.
  584. */
  585. int __set_page_dirty_buffers(struct page *page)
  586. {
  587. int newly_dirty;
  588. struct address_space *mapping = page_mapping(page);
  589. if (unlikely(!mapping))
  590. return !TestSetPageDirty(page);
  591. spin_lock(&mapping->private_lock);
  592. if (page_has_buffers(page)) {
  593. struct buffer_head *head = page_buffers(page);
  594. struct buffer_head *bh = head;
  595. do {
  596. set_buffer_dirty(bh);
  597. bh = bh->b_this_page;
  598. } while (bh != head);
  599. }
  600. /*
  601. * Lock out page->mem_cgroup migration to keep PageDirty
  602. * synchronized with per-memcg dirty page counters.
  603. */
  604. lock_page_memcg(page);
  605. newly_dirty = !TestSetPageDirty(page);
  606. spin_unlock(&mapping->private_lock);
  607. if (newly_dirty)
  608. __set_page_dirty(page, mapping, 1);
  609. unlock_page_memcg(page);
  610. if (newly_dirty)
  611. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  612. return newly_dirty;
  613. }
  614. EXPORT_SYMBOL(__set_page_dirty_buffers);
  615. /*
  616. * Write out and wait upon a list of buffers.
  617. *
  618. * We have conflicting pressures: we want to make sure that all
  619. * initially dirty buffers get waited on, but that any subsequently
  620. * dirtied buffers don't. After all, we don't want fsync to last
  621. * forever if somebody is actively writing to the file.
  622. *
  623. * Do this in two main stages: first we copy dirty buffers to a
  624. * temporary inode list, queueing the writes as we go. Then we clean
  625. * up, waiting for those writes to complete.
  626. *
  627. * During this second stage, any subsequent updates to the file may end
  628. * up refiling the buffer on the original inode's dirty list again, so
  629. * there is a chance we will end up with a buffer queued for write but
  630. * not yet completed on that list. So, as a final cleanup we go through
  631. * the osync code to catch these locked, dirty buffers without requeuing
  632. * any newly dirty buffers for write.
  633. */
  634. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
  635. {
  636. struct buffer_head *bh;
  637. struct list_head tmp;
  638. struct address_space *mapping;
  639. int err = 0, err2;
  640. struct blk_plug plug;
  641. INIT_LIST_HEAD(&tmp);
  642. blk_start_plug(&plug);
  643. spin_lock(lock);
  644. while (!list_empty(list)) {
  645. bh = BH_ENTRY(list->next);
  646. mapping = bh->b_assoc_map;
  647. __remove_assoc_queue(bh);
  648. /* Avoid race with mark_buffer_dirty_inode() which does
  649. * a lockless check and we rely on seeing the dirty bit */
  650. smp_mb();
  651. if (buffer_dirty(bh) || buffer_locked(bh)) {
  652. list_add(&bh->b_assoc_buffers, &tmp);
  653. bh->b_assoc_map = mapping;
  654. if (buffer_dirty(bh)) {
  655. get_bh(bh);
  656. spin_unlock(lock);
  657. /*
  658. * Ensure any pending I/O completes so that
  659. * write_dirty_buffer() actually writes the
  660. * current contents - it is a noop if I/O is
  661. * still in flight on potentially older
  662. * contents.
  663. */
  664. write_dirty_buffer(bh, REQ_SYNC);
  665. /*
  666. * Kick off IO for the previous mapping. Note
  667. * that we will not run the very last mapping,
  668. * wait_on_buffer() will do that for us
  669. * through sync_buffer().
  670. */
  671. brelse(bh);
  672. spin_lock(lock);
  673. }
  674. }
  675. }
  676. spin_unlock(lock);
  677. blk_finish_plug(&plug);
  678. spin_lock(lock);
  679. while (!list_empty(&tmp)) {
  680. bh = BH_ENTRY(tmp.prev);
  681. get_bh(bh);
  682. mapping = bh->b_assoc_map;
  683. __remove_assoc_queue(bh);
  684. /* Avoid race with mark_buffer_dirty_inode() which does
  685. * a lockless check and we rely on seeing the dirty bit */
  686. smp_mb();
  687. if (buffer_dirty(bh)) {
  688. list_add(&bh->b_assoc_buffers,
  689. &mapping->private_list);
  690. bh->b_assoc_map = mapping;
  691. }
  692. spin_unlock(lock);
  693. wait_on_buffer(bh);
  694. if (!buffer_uptodate(bh))
  695. err = -EIO;
  696. brelse(bh);
  697. spin_lock(lock);
  698. }
  699. spin_unlock(lock);
  700. err2 = osync_buffers_list(lock, list);
  701. if (err)
  702. return err;
  703. else
  704. return err2;
  705. }
  706. /*
  707. * Invalidate any and all dirty buffers on a given inode. We are
  708. * probably unmounting the fs, but that doesn't mean we have already
  709. * done a sync(). Just drop the buffers from the inode list.
  710. *
  711. * NOTE: we take the inode's blockdev's mapping's private_lock. Which
  712. * assumes that all the buffers are against the blockdev. Not true
  713. * for reiserfs.
  714. */
  715. void invalidate_inode_buffers(struct inode *inode)
  716. {
  717. if (inode_has_buffers(inode)) {
  718. struct address_space *mapping = &inode->i_data;
  719. struct list_head *list = &mapping->private_list;
  720. struct address_space *buffer_mapping = mapping->private_data;
  721. spin_lock(&buffer_mapping->private_lock);
  722. while (!list_empty(list))
  723. __remove_assoc_queue(BH_ENTRY(list->next));
  724. spin_unlock(&buffer_mapping->private_lock);
  725. }
  726. }
  727. EXPORT_SYMBOL(invalidate_inode_buffers);
  728. /*
  729. * Remove any clean buffers from the inode's buffer list. This is called
  730. * when we're trying to free the inode itself. Those buffers can pin it.
  731. *
  732. * Returns true if all buffers were removed.
  733. */
  734. int remove_inode_buffers(struct inode *inode)
  735. {
  736. int ret = 1;
  737. if (inode_has_buffers(inode)) {
  738. struct address_space *mapping = &inode->i_data;
  739. struct list_head *list = &mapping->private_list;
  740. struct address_space *buffer_mapping = mapping->private_data;
  741. spin_lock(&buffer_mapping->private_lock);
  742. while (!list_empty(list)) {
  743. struct buffer_head *bh = BH_ENTRY(list->next);
  744. if (buffer_dirty(bh)) {
  745. ret = 0;
  746. break;
  747. }
  748. __remove_assoc_queue(bh);
  749. }
  750. spin_unlock(&buffer_mapping->private_lock);
  751. }
  752. return ret;
  753. }
  754. /*
  755. * Create the appropriate buffers when given a page for data area and
  756. * the size of each buffer.. Use the bh->b_this_page linked list to
  757. * follow the buffers created. Return NULL if unable to create more
  758. * buffers.
  759. *
  760. * The retry flag is used to differentiate async IO (paging, swapping)
  761. * which may not fail from ordinary buffer allocations.
  762. */
  763. struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
  764. bool retry)
  765. {
  766. struct buffer_head *bh, *head;
  767. gfp_t gfp = GFP_NOFS;
  768. long offset;
  769. if (retry)
  770. gfp |= __GFP_NOFAIL;
  771. head = NULL;
  772. offset = PAGE_SIZE;
  773. while ((offset -= size) >= 0) {
  774. bh = alloc_buffer_head(gfp);
  775. if (!bh)
  776. goto no_grow;
  777. bh->b_this_page = head;
  778. bh->b_blocknr = -1;
  779. head = bh;
  780. bh->b_size = size;
  781. /* Link the buffer to its page */
  782. set_bh_page(bh, page, offset);
  783. }
  784. return head;
  785. /*
  786. * In case anything failed, we just free everything we got.
  787. */
  788. no_grow:
  789. if (head) {
  790. do {
  791. bh = head;
  792. head = head->b_this_page;
  793. free_buffer_head(bh);
  794. } while (head);
  795. }
  796. return NULL;
  797. }
  798. EXPORT_SYMBOL_GPL(alloc_page_buffers);
  799. static inline void
  800. link_dev_buffers(struct page *page, struct buffer_head *head)
  801. {
  802. struct buffer_head *bh, *tail;
  803. bh = head;
  804. do {
  805. tail = bh;
  806. bh = bh->b_this_page;
  807. } while (bh);
  808. tail->b_this_page = head;
  809. attach_page_buffers(page, head);
  810. }
  811. static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
  812. {
  813. sector_t retval = ~((sector_t)0);
  814. loff_t sz = i_size_read(bdev->bd_inode);
  815. if (sz) {
  816. unsigned int sizebits = blksize_bits(size);
  817. retval = (sz >> sizebits);
  818. }
  819. return retval;
  820. }
  821. /*
  822. * Initialise the state of a blockdev page's buffers.
  823. */
  824. static sector_t
  825. init_page_buffers(struct page *page, struct block_device *bdev,
  826. sector_t block, int size)
  827. {
  828. struct buffer_head *head = page_buffers(page);
  829. struct buffer_head *bh = head;
  830. int uptodate = PageUptodate(page);
  831. sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
  832. do {
  833. if (!buffer_mapped(bh)) {
  834. init_buffer(bh, NULL, NULL);
  835. bh->b_bdev = bdev;
  836. bh->b_blocknr = block;
  837. if (uptodate)
  838. set_buffer_uptodate(bh);
  839. if (block < end_block)
  840. set_buffer_mapped(bh);
  841. }
  842. block++;
  843. bh = bh->b_this_page;
  844. } while (bh != head);
  845. /*
  846. * Caller needs to validate requested block against end of device.
  847. */
  848. return end_block;
  849. }
  850. /*
  851. * Create the page-cache page that contains the requested block.
  852. *
  853. * This is used purely for blockdev mappings.
  854. */
  855. static int
  856. grow_dev_page(struct block_device *bdev, sector_t block,
  857. pgoff_t index, int size, int sizebits, gfp_t gfp)
  858. {
  859. struct inode *inode = bdev->bd_inode;
  860. struct page *page;
  861. struct buffer_head *bh;
  862. sector_t end_block;
  863. int ret = 0; /* Will call free_more_memory() */
  864. gfp_t gfp_mask;
  865. gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
  866. /*
  867. * XXX: __getblk_slow() can not really deal with failure and
  868. * will endlessly loop on improvised global reclaim. Prefer
  869. * looping in the allocator rather than here, at least that
  870. * code knows what it's doing.
  871. */
  872. gfp_mask |= __GFP_NOFAIL;
  873. page = find_or_create_page(inode->i_mapping, index, gfp_mask);
  874. BUG_ON(!PageLocked(page));
  875. if (page_has_buffers(page)) {
  876. bh = page_buffers(page);
  877. if (bh->b_size == size) {
  878. end_block = init_page_buffers(page, bdev,
  879. (sector_t)index << sizebits,
  880. size);
  881. goto done;
  882. }
  883. if (!try_to_free_buffers(page))
  884. goto failed;
  885. }
  886. /*
  887. * Allocate some buffers for this page
  888. */
  889. bh = alloc_page_buffers(page, size, true);
  890. /*
  891. * Link the page to the buffers and initialise them. Take the
  892. * lock to be atomic wrt __find_get_block(), which does not
  893. * run under the page lock.
  894. */
  895. spin_lock(&inode->i_mapping->private_lock);
  896. link_dev_buffers(page, bh);
  897. end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
  898. size);
  899. spin_unlock(&inode->i_mapping->private_lock);
  900. done:
  901. ret = (block < end_block) ? 1 : -ENXIO;
  902. failed:
  903. unlock_page(page);
  904. put_page(page);
  905. return ret;
  906. }
  907. /*
  908. * Create buffers for the specified block device block's page. If
  909. * that page was dirty, the buffers are set dirty also.
  910. */
  911. static int
  912. grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
  913. {
  914. pgoff_t index;
  915. int sizebits;
  916. sizebits = -1;
  917. do {
  918. sizebits++;
  919. } while ((size << sizebits) < PAGE_SIZE);
  920. index = block >> sizebits;
  921. /*
  922. * Check for a block which wants to lie outside our maximum possible
  923. * pagecache index. (this comparison is done using sector_t types).
  924. */
  925. if (unlikely(index != block >> sizebits)) {
  926. printk(KERN_ERR "%s: requested out-of-range block %llu for "
  927. "device %pg\n",
  928. __func__, (unsigned long long)block,
  929. bdev);
  930. return -EIO;
  931. }
  932. /* Create a page with the proper size buffers.. */
  933. return grow_dev_page(bdev, block, index, size, sizebits, gfp);
  934. }
  935. static struct buffer_head *
  936. __getblk_slow(struct block_device *bdev, sector_t block,
  937. unsigned size, gfp_t gfp)
  938. {
  939. /* Size must be multiple of hard sectorsize */
  940. if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
  941. (size < 512 || size > PAGE_SIZE))) {
  942. printk(KERN_ERR "getblk(): invalid block size %d requested\n",
  943. size);
  944. printk(KERN_ERR "logical block size: %d\n",
  945. bdev_logical_block_size(bdev));
  946. dump_stack();
  947. return NULL;
  948. }
  949. for (;;) {
  950. struct buffer_head *bh;
  951. int ret;
  952. bh = __find_get_block(bdev, block, size);
  953. if (bh)
  954. return bh;
  955. ret = grow_buffers(bdev, block, size, gfp);
  956. if (ret < 0)
  957. return NULL;
  958. }
  959. }
  960. /*
  961. * The relationship between dirty buffers and dirty pages:
  962. *
  963. * Whenever a page has any dirty buffers, the page's dirty bit is set, and
  964. * the page is tagged dirty in its radix tree.
  965. *
  966. * At all times, the dirtiness of the buffers represents the dirtiness of
  967. * subsections of the page. If the page has buffers, the page dirty bit is
  968. * merely a hint about the true dirty state.
  969. *
  970. * When a page is set dirty in its entirety, all its buffers are marked dirty
  971. * (if the page has buffers).
  972. *
  973. * When a buffer is marked dirty, its page is dirtied, but the page's other
  974. * buffers are not.
  975. *
  976. * Also. When blockdev buffers are explicitly read with bread(), they
  977. * individually become uptodate. But their backing page remains not
  978. * uptodate - even if all of its buffers are uptodate. A subsequent
  979. * block_read_full_page() against that page will discover all the uptodate
  980. * buffers, will set the page uptodate and will perform no I/O.
  981. */
  982. /**
  983. * mark_buffer_dirty - mark a buffer_head as needing writeout
  984. * @bh: the buffer_head to mark dirty
  985. *
  986. * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
  987. * backing page dirty, then tag the page as dirty in its address_space's radix
  988. * tree and then attach the address_space's inode to its superblock's dirty
  989. * inode list.
  990. *
  991. * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
  992. * mapping->tree_lock and mapping->host->i_lock.
  993. */
  994. void mark_buffer_dirty(struct buffer_head *bh)
  995. {
  996. WARN_ON_ONCE(!buffer_uptodate(bh));
  997. trace_block_dirty_buffer(bh);
  998. /*
  999. * Very *carefully* optimize the it-is-already-dirty case.
  1000. *
  1001. * Don't let the final "is it dirty" escape to before we
  1002. * perhaps modified the buffer.
  1003. */
  1004. if (buffer_dirty(bh)) {
  1005. smp_mb();
  1006. if (buffer_dirty(bh))
  1007. return;
  1008. }
  1009. if (!test_set_buffer_dirty(bh)) {
  1010. struct page *page = bh->b_page;
  1011. struct address_space *mapping = NULL;
  1012. lock_page_memcg(page);
  1013. if (!TestSetPageDirty(page)) {
  1014. mapping = page_mapping(page);
  1015. if (mapping)
  1016. __set_page_dirty(page, mapping, 0);
  1017. }
  1018. unlock_page_memcg(page);
  1019. if (mapping)
  1020. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1021. }
  1022. }
  1023. EXPORT_SYMBOL(mark_buffer_dirty);
  1024. void mark_buffer_write_io_error(struct buffer_head *bh)
  1025. {
  1026. set_buffer_write_io_error(bh);
  1027. /* FIXME: do we need to set this in both places? */
  1028. if (bh->b_page && bh->b_page->mapping)
  1029. mapping_set_error(bh->b_page->mapping, -EIO);
  1030. if (bh->b_assoc_map)
  1031. mapping_set_error(bh->b_assoc_map, -EIO);
  1032. }
  1033. EXPORT_SYMBOL(mark_buffer_write_io_error);
  1034. /*
  1035. * Decrement a buffer_head's reference count. If all buffers against a page
  1036. * have zero reference count, are clean and unlocked, and if the page is clean
  1037. * and unlocked then try_to_free_buffers() may strip the buffers from the page
  1038. * in preparation for freeing it (sometimes, rarely, buffers are removed from
  1039. * a page but it ends up not being freed, and buffers may later be reattached).
  1040. */
  1041. void __brelse(struct buffer_head * buf)
  1042. {
  1043. if (atomic_read(&buf->b_count)) {
  1044. put_bh(buf);
  1045. return;
  1046. }
  1047. WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
  1048. }
  1049. EXPORT_SYMBOL(__brelse);
  1050. /*
  1051. * bforget() is like brelse(), except it discards any
  1052. * potentially dirty data.
  1053. */
  1054. void __bforget(struct buffer_head *bh)
  1055. {
  1056. clear_buffer_dirty(bh);
  1057. if (bh->b_assoc_map) {
  1058. struct address_space *buffer_mapping = bh->b_page->mapping;
  1059. spin_lock(&buffer_mapping->private_lock);
  1060. list_del_init(&bh->b_assoc_buffers);
  1061. bh->b_assoc_map = NULL;
  1062. spin_unlock(&buffer_mapping->private_lock);
  1063. }
  1064. __brelse(bh);
  1065. }
  1066. EXPORT_SYMBOL(__bforget);
  1067. static struct buffer_head *__bread_slow(struct buffer_head *bh)
  1068. {
  1069. lock_buffer(bh);
  1070. if (buffer_uptodate(bh)) {
  1071. unlock_buffer(bh);
  1072. return bh;
  1073. } else {
  1074. get_bh(bh);
  1075. bh->b_end_io = end_buffer_read_sync;
  1076. submit_bh(REQ_OP_READ, 0, bh);
  1077. wait_on_buffer(bh);
  1078. if (buffer_uptodate(bh))
  1079. return bh;
  1080. }
  1081. brelse(bh);
  1082. return NULL;
  1083. }
  1084. /*
  1085. * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
  1086. * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
  1087. * refcount elevated by one when they're in an LRU. A buffer can only appear
  1088. * once in a particular CPU's LRU. A single buffer can be present in multiple
  1089. * CPU's LRUs at the same time.
  1090. *
  1091. * This is a transparent caching front-end to sb_bread(), sb_getblk() and
  1092. * sb_find_get_block().
  1093. *
  1094. * The LRUs themselves only need locking against invalidate_bh_lrus. We use
  1095. * a local interrupt disable for that.
  1096. */
  1097. #define BH_LRU_SIZE 16
  1098. struct bh_lru {
  1099. struct buffer_head *bhs[BH_LRU_SIZE];
  1100. };
  1101. static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
  1102. #ifdef CONFIG_SMP
  1103. #define bh_lru_lock() local_irq_disable()
  1104. #define bh_lru_unlock() local_irq_enable()
  1105. #else
  1106. #define bh_lru_lock() preempt_disable()
  1107. #define bh_lru_unlock() preempt_enable()
  1108. #endif
  1109. static inline void check_irqs_on(void)
  1110. {
  1111. #ifdef irqs_disabled
  1112. BUG_ON(irqs_disabled());
  1113. #endif
  1114. }
  1115. /*
  1116. * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is
  1117. * inserted at the front, and the buffer_head at the back if any is evicted.
  1118. * Or, if already in the LRU it is moved to the front.
  1119. */
  1120. static void bh_lru_install(struct buffer_head *bh)
  1121. {
  1122. struct buffer_head *evictee = bh;
  1123. struct bh_lru *b;
  1124. int i;
  1125. check_irqs_on();
  1126. bh_lru_lock();
  1127. b = this_cpu_ptr(&bh_lrus);
  1128. for (i = 0; i < BH_LRU_SIZE; i++) {
  1129. swap(evictee, b->bhs[i]);
  1130. if (evictee == bh) {
  1131. bh_lru_unlock();
  1132. return;
  1133. }
  1134. }
  1135. get_bh(bh);
  1136. bh_lru_unlock();
  1137. brelse(evictee);
  1138. }
  1139. /*
  1140. * Look up the bh in this cpu's LRU. If it's there, move it to the head.
  1141. */
  1142. static struct buffer_head *
  1143. lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
  1144. {
  1145. struct buffer_head *ret = NULL;
  1146. unsigned int i;
  1147. check_irqs_on();
  1148. bh_lru_lock();
  1149. for (i = 0; i < BH_LRU_SIZE; i++) {
  1150. struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
  1151. if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
  1152. bh->b_size == size) {
  1153. if (i) {
  1154. while (i) {
  1155. __this_cpu_write(bh_lrus.bhs[i],
  1156. __this_cpu_read(bh_lrus.bhs[i - 1]));
  1157. i--;
  1158. }
  1159. __this_cpu_write(bh_lrus.bhs[0], bh);
  1160. }
  1161. get_bh(bh);
  1162. ret = bh;
  1163. break;
  1164. }
  1165. }
  1166. bh_lru_unlock();
  1167. return ret;
  1168. }
  1169. /*
  1170. * Perform a pagecache lookup for the matching buffer. If it's there, refresh
  1171. * it in the LRU and mark it as accessed. If it is not present then return
  1172. * NULL
  1173. */
  1174. struct buffer_head *
  1175. __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
  1176. {
  1177. struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
  1178. if (bh == NULL) {
  1179. /* __find_get_block_slow will mark the page accessed */
  1180. bh = __find_get_block_slow(bdev, block);
  1181. if (bh)
  1182. bh_lru_install(bh);
  1183. } else
  1184. touch_buffer(bh);
  1185. return bh;
  1186. }
  1187. EXPORT_SYMBOL(__find_get_block);
  1188. /*
  1189. * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
  1190. * which corresponds to the passed block_device, block and size. The
  1191. * returned buffer has its reference count incremented.
  1192. *
  1193. * __getblk_gfp() will lock up the machine if grow_dev_page's
  1194. * try_to_free_buffers() attempt is failing. FIXME, perhaps?
  1195. */
  1196. struct buffer_head *
  1197. __getblk_gfp(struct block_device *bdev, sector_t block,
  1198. unsigned size, gfp_t gfp)
  1199. {
  1200. struct buffer_head *bh = __find_get_block(bdev, block, size);
  1201. might_sleep();
  1202. if (bh == NULL)
  1203. bh = __getblk_slow(bdev, block, size, gfp);
  1204. return bh;
  1205. }
  1206. EXPORT_SYMBOL(__getblk_gfp);
  1207. /*
  1208. * Do async read-ahead on a buffer..
  1209. */
  1210. void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
  1211. {
  1212. struct buffer_head *bh = __getblk(bdev, block, size);
  1213. if (likely(bh)) {
  1214. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
  1215. brelse(bh);
  1216. }
  1217. }
  1218. EXPORT_SYMBOL(__breadahead);
  1219. /**
  1220. * __bread_gfp() - reads a specified block and returns the bh
  1221. * @bdev: the block_device to read from
  1222. * @block: number of block
  1223. * @size: size (in bytes) to read
  1224. * @gfp: page allocation flag
  1225. *
  1226. * Reads a specified block, and returns buffer head that contains it.
  1227. * The page cache can be allocated from non-movable area
  1228. * not to prevent page migration if you set gfp to zero.
  1229. * It returns NULL if the block was unreadable.
  1230. */
  1231. struct buffer_head *
  1232. __bread_gfp(struct block_device *bdev, sector_t block,
  1233. unsigned size, gfp_t gfp)
  1234. {
  1235. struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
  1236. if (likely(bh) && !buffer_uptodate(bh))
  1237. bh = __bread_slow(bh);
  1238. return bh;
  1239. }
  1240. EXPORT_SYMBOL(__bread_gfp);
  1241. /*
  1242. * invalidate_bh_lrus() is called rarely - but not only at unmount.
  1243. * This doesn't race because it runs in each cpu either in irq
  1244. * or with preempt disabled.
  1245. */
  1246. static void invalidate_bh_lru(void *arg)
  1247. {
  1248. struct bh_lru *b = &get_cpu_var(bh_lrus);
  1249. int i;
  1250. for (i = 0; i < BH_LRU_SIZE; i++) {
  1251. brelse(b->bhs[i]);
  1252. b->bhs[i] = NULL;
  1253. }
  1254. put_cpu_var(bh_lrus);
  1255. }
  1256. static bool has_bh_in_lru(int cpu, void *dummy)
  1257. {
  1258. struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
  1259. int i;
  1260. for (i = 0; i < BH_LRU_SIZE; i++) {
  1261. if (b->bhs[i])
  1262. return 1;
  1263. }
  1264. return 0;
  1265. }
  1266. void invalidate_bh_lrus(void)
  1267. {
  1268. on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
  1269. }
  1270. EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
  1271. void set_bh_page(struct buffer_head *bh,
  1272. struct page *page, unsigned long offset)
  1273. {
  1274. bh->b_page = page;
  1275. BUG_ON(offset >= PAGE_SIZE);
  1276. if (PageHighMem(page))
  1277. /*
  1278. * This catches illegal uses and preserves the offset:
  1279. */
  1280. bh->b_data = (char *)(0 + offset);
  1281. else
  1282. bh->b_data = page_address(page) + offset;
  1283. }
  1284. EXPORT_SYMBOL(set_bh_page);
  1285. /*
  1286. * Called when truncating a buffer on a page completely.
  1287. */
  1288. /* Bits that are cleared during an invalidate */
  1289. #define BUFFER_FLAGS_DISCARD \
  1290. (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
  1291. 1 << BH_Delay | 1 << BH_Unwritten)
  1292. static void discard_buffer(struct buffer_head * bh)
  1293. {
  1294. unsigned long b_state, b_state_old;
  1295. lock_buffer(bh);
  1296. clear_buffer_dirty(bh);
  1297. bh->b_bdev = NULL;
  1298. b_state = bh->b_state;
  1299. for (;;) {
  1300. b_state_old = cmpxchg(&bh->b_state, b_state,
  1301. (b_state & ~BUFFER_FLAGS_DISCARD));
  1302. if (b_state_old == b_state)
  1303. break;
  1304. b_state = b_state_old;
  1305. }
  1306. unlock_buffer(bh);
  1307. }
  1308. /**
  1309. * block_invalidatepage - invalidate part or all of a buffer-backed page
  1310. *
  1311. * @page: the page which is affected
  1312. * @offset: start of the range to invalidate
  1313. * @length: length of the range to invalidate
  1314. *
  1315. * block_invalidatepage() is called when all or part of the page has become
  1316. * invalidated by a truncate operation.
  1317. *
  1318. * block_invalidatepage() does not have to release all buffers, but it must
  1319. * ensure that no dirty buffer is left outside @offset and that no I/O
  1320. * is underway against any of the blocks which are outside the truncation
  1321. * point. Because the caller is about to free (and possibly reuse) those
  1322. * blocks on-disk.
  1323. */
  1324. void block_invalidatepage(struct page *page, unsigned int offset,
  1325. unsigned int length)
  1326. {
  1327. struct buffer_head *head, *bh, *next;
  1328. unsigned int curr_off = 0;
  1329. unsigned int stop = length + offset;
  1330. BUG_ON(!PageLocked(page));
  1331. if (!page_has_buffers(page))
  1332. goto out;
  1333. /*
  1334. * Check for overflow
  1335. */
  1336. BUG_ON(stop > PAGE_SIZE || stop < length);
  1337. head = page_buffers(page);
  1338. bh = head;
  1339. do {
  1340. unsigned int next_off = curr_off + bh->b_size;
  1341. next = bh->b_this_page;
  1342. /*
  1343. * Are we still fully in range ?
  1344. */
  1345. if (next_off > stop)
  1346. goto out;
  1347. /*
  1348. * is this block fully invalidated?
  1349. */
  1350. if (offset <= curr_off)
  1351. discard_buffer(bh);
  1352. curr_off = next_off;
  1353. bh = next;
  1354. } while (bh != head);
  1355. /*
  1356. * We release buffers only if the entire page is being invalidated.
  1357. * The get_block cached value has been unconditionally invalidated,
  1358. * so real IO is not possible anymore.
  1359. */
  1360. if (offset == 0)
  1361. try_to_release_page(page, 0);
  1362. out:
  1363. return;
  1364. }
  1365. EXPORT_SYMBOL(block_invalidatepage);
  1366. /*
  1367. * We attach and possibly dirty the buffers atomically wrt
  1368. * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
  1369. * is already excluded via the page lock.
  1370. */
  1371. void create_empty_buffers(struct page *page,
  1372. unsigned long blocksize, unsigned long b_state)
  1373. {
  1374. struct buffer_head *bh, *head, *tail;
  1375. head = alloc_page_buffers(page, blocksize, true);
  1376. bh = head;
  1377. do {
  1378. bh->b_state |= b_state;
  1379. tail = bh;
  1380. bh = bh->b_this_page;
  1381. } while (bh);
  1382. tail->b_this_page = head;
  1383. spin_lock(&page->mapping->private_lock);
  1384. if (PageUptodate(page) || PageDirty(page)) {
  1385. bh = head;
  1386. do {
  1387. if (PageDirty(page))
  1388. set_buffer_dirty(bh);
  1389. if (PageUptodate(page))
  1390. set_buffer_uptodate(bh);
  1391. bh = bh->b_this_page;
  1392. } while (bh != head);
  1393. }
  1394. attach_page_buffers(page, head);
  1395. spin_unlock(&page->mapping->private_lock);
  1396. }
  1397. EXPORT_SYMBOL(create_empty_buffers);
  1398. /**
  1399. * clean_bdev_aliases: clean a range of buffers in block device
  1400. * @bdev: Block device to clean buffers in
  1401. * @block: Start of a range of blocks to clean
  1402. * @len: Number of blocks to clean
  1403. *
  1404. * We are taking a range of blocks for data and we don't want writeback of any
  1405. * buffer-cache aliases starting from return from this function and until the
  1406. * moment when something will explicitly mark the buffer dirty (hopefully that
  1407. * will not happen until we will free that block ;-) We don't even need to mark
  1408. * it not-uptodate - nobody can expect anything from a newly allocated buffer
  1409. * anyway. We used to use unmap_buffer() for such invalidation, but that was
  1410. * wrong. We definitely don't want to mark the alias unmapped, for example - it
  1411. * would confuse anyone who might pick it with bread() afterwards...
  1412. *
  1413. * Also.. Note that bforget() doesn't lock the buffer. So there can be
  1414. * writeout I/O going on against recently-freed buffers. We don't wait on that
  1415. * I/O in bforget() - it's more efficient to wait on the I/O only if we really
  1416. * need to. That happens here.
  1417. */
  1418. void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
  1419. {
  1420. struct inode *bd_inode = bdev->bd_inode;
  1421. struct address_space *bd_mapping = bd_inode->i_mapping;
  1422. struct pagevec pvec;
  1423. pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1424. pgoff_t end;
  1425. int i, count;
  1426. struct buffer_head *bh;
  1427. struct buffer_head *head;
  1428. end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1429. pagevec_init(&pvec);
  1430. while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) {
  1431. count = pagevec_count(&pvec);
  1432. for (i = 0; i < count; i++) {
  1433. struct page *page = pvec.pages[i];
  1434. if (!page_has_buffers(page))
  1435. continue;
  1436. /*
  1437. * We use page lock instead of bd_mapping->private_lock
  1438. * to pin buffers here since we can afford to sleep and
  1439. * it scales better than a global spinlock lock.
  1440. */
  1441. lock_page(page);
  1442. /* Recheck when the page is locked which pins bhs */
  1443. if (!page_has_buffers(page))
  1444. goto unlock_page;
  1445. head = page_buffers(page);
  1446. bh = head;
  1447. do {
  1448. if (!buffer_mapped(bh) || (bh->b_blocknr < block))
  1449. goto next;
  1450. if (bh->b_blocknr >= block + len)
  1451. break;
  1452. clear_buffer_dirty(bh);
  1453. wait_on_buffer(bh);
  1454. clear_buffer_req(bh);
  1455. next:
  1456. bh = bh->b_this_page;
  1457. } while (bh != head);
  1458. unlock_page:
  1459. unlock_page(page);
  1460. }
  1461. pagevec_release(&pvec);
  1462. cond_resched();
  1463. /* End of range already reached? */
  1464. if (index > end || !index)
  1465. break;
  1466. }
  1467. }
  1468. EXPORT_SYMBOL(clean_bdev_aliases);
  1469. /*
  1470. * Size is a power-of-two in the range 512..PAGE_SIZE,
  1471. * and the case we care about most is PAGE_SIZE.
  1472. *
  1473. * So this *could* possibly be written with those
  1474. * constraints in mind (relevant mostly if some
  1475. * architecture has a slow bit-scan instruction)
  1476. */
  1477. static inline int block_size_bits(unsigned int blocksize)
  1478. {
  1479. return ilog2(blocksize);
  1480. }
  1481. static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
  1482. {
  1483. BUG_ON(!PageLocked(page));
  1484. if (!page_has_buffers(page))
  1485. create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits),
  1486. b_state);
  1487. return page_buffers(page);
  1488. }
  1489. /*
  1490. * NOTE! All mapped/uptodate combinations are valid:
  1491. *
  1492. * Mapped Uptodate Meaning
  1493. *
  1494. * No No "unknown" - must do get_block()
  1495. * No Yes "hole" - zero-filled
  1496. * Yes No "allocated" - allocated on disk, not read in
  1497. * Yes Yes "valid" - allocated and up-to-date in memory.
  1498. *
  1499. * "Dirty" is valid only with the last case (mapped+uptodate).
  1500. */
  1501. /*
  1502. * While block_write_full_page is writing back the dirty buffers under
  1503. * the page lock, whoever dirtied the buffers may decide to clean them
  1504. * again at any time. We handle that by only looking at the buffer
  1505. * state inside lock_buffer().
  1506. *
  1507. * If block_write_full_page() is called for regular writeback
  1508. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1509. * locked buffer. This only can happen if someone has written the buffer
  1510. * directly, with submit_bh(). At the address_space level PageWriteback
  1511. * prevents this contention from occurring.
  1512. *
  1513. * If block_write_full_page() is called with wbc->sync_mode ==
  1514. * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
  1515. * causes the writes to be flagged as synchronous writes.
  1516. */
  1517. int __block_write_full_page(struct inode *inode, struct page *page,
  1518. get_block_t *get_block, struct writeback_control *wbc,
  1519. bh_end_io_t *handler)
  1520. {
  1521. int err;
  1522. sector_t block;
  1523. sector_t last_block;
  1524. struct buffer_head *bh, *head;
  1525. unsigned int blocksize, bbits;
  1526. int nr_underway = 0;
  1527. int write_flags = wbc_to_write_flags(wbc);
  1528. head = create_page_buffers(page, inode,
  1529. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1530. /*
  1531. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1532. * here, and the (potentially unmapped) buffers may become dirty at
  1533. * any time. If a buffer becomes dirty here after we've inspected it
  1534. * then we just miss that fact, and the page stays dirty.
  1535. *
  1536. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1537. * handle that here by just cleaning them.
  1538. */
  1539. bh = head;
  1540. blocksize = bh->b_size;
  1541. bbits = block_size_bits(blocksize);
  1542. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1543. last_block = (i_size_read(inode) - 1) >> bbits;
  1544. /*
  1545. * Get all the dirty buffers mapped to disk addresses and
  1546. * handle any aliases from the underlying blockdev's mapping.
  1547. */
  1548. do {
  1549. if (block > last_block) {
  1550. /*
  1551. * mapped buffers outside i_size will occur, because
  1552. * this page can be outside i_size when there is a
  1553. * truncate in progress.
  1554. */
  1555. /*
  1556. * The buffer was zeroed by block_write_full_page()
  1557. */
  1558. clear_buffer_dirty(bh);
  1559. set_buffer_uptodate(bh);
  1560. } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
  1561. buffer_dirty(bh)) {
  1562. WARN_ON(bh->b_size != blocksize);
  1563. err = get_block(inode, block, bh, 1);
  1564. if (err)
  1565. goto recover;
  1566. clear_buffer_delay(bh);
  1567. if (buffer_new(bh)) {
  1568. /* blockdev mappings never come here */
  1569. clear_buffer_new(bh);
  1570. clean_bdev_bh_alias(bh);
  1571. }
  1572. }
  1573. bh = bh->b_this_page;
  1574. block++;
  1575. } while (bh != head);
  1576. do {
  1577. if (!buffer_mapped(bh))
  1578. continue;
  1579. /*
  1580. * If it's a fully non-blocking write attempt and we cannot
  1581. * lock the buffer then redirty the page. Note that this can
  1582. * potentially cause a busy-wait loop from writeback threads
  1583. * and kswapd activity, but those code paths have their own
  1584. * higher-level throttling.
  1585. */
  1586. if (wbc->sync_mode != WB_SYNC_NONE) {
  1587. lock_buffer(bh);
  1588. } else if (!trylock_buffer(bh)) {
  1589. redirty_page_for_writepage(wbc, page);
  1590. continue;
  1591. }
  1592. if (test_clear_buffer_dirty(bh)) {
  1593. mark_buffer_async_write_endio(bh, handler);
  1594. } else {
  1595. unlock_buffer(bh);
  1596. }
  1597. } while ((bh = bh->b_this_page) != head);
  1598. /*
  1599. * The page and its buffers are protected by PageWriteback(), so we can
  1600. * drop the bh refcounts early.
  1601. */
  1602. BUG_ON(PageWriteback(page));
  1603. set_page_writeback(page);
  1604. do {
  1605. struct buffer_head *next = bh->b_this_page;
  1606. if (buffer_async_write(bh)) {
  1607. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1608. inode->i_write_hint, wbc);
  1609. nr_underway++;
  1610. }
  1611. bh = next;
  1612. } while (bh != head);
  1613. unlock_page(page);
  1614. err = 0;
  1615. done:
  1616. if (nr_underway == 0) {
  1617. /*
  1618. * The page was marked dirty, but the buffers were
  1619. * clean. Someone wrote them back by hand with
  1620. * ll_rw_block/submit_bh. A rare case.
  1621. */
  1622. end_page_writeback(page);
  1623. /*
  1624. * The page and buffer_heads can be released at any time from
  1625. * here on.
  1626. */
  1627. }
  1628. return err;
  1629. recover:
  1630. /*
  1631. * ENOSPC, or some other error. We may already have added some
  1632. * blocks to the file, so we need to write these out to avoid
  1633. * exposing stale data.
  1634. * The page is currently locked and not marked for writeback
  1635. */
  1636. bh = head;
  1637. /* Recovery: lock and submit the mapped buffers */
  1638. do {
  1639. if (buffer_mapped(bh) && buffer_dirty(bh) &&
  1640. !buffer_delay(bh)) {
  1641. lock_buffer(bh);
  1642. mark_buffer_async_write_endio(bh, handler);
  1643. } else {
  1644. /*
  1645. * The buffer may have been set dirty during
  1646. * attachment to a dirty page.
  1647. */
  1648. clear_buffer_dirty(bh);
  1649. }
  1650. } while ((bh = bh->b_this_page) != head);
  1651. SetPageError(page);
  1652. BUG_ON(PageWriteback(page));
  1653. mapping_set_error(page->mapping, err);
  1654. set_page_writeback(page);
  1655. do {
  1656. struct buffer_head *next = bh->b_this_page;
  1657. if (buffer_async_write(bh)) {
  1658. clear_buffer_dirty(bh);
  1659. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1660. inode->i_write_hint, wbc);
  1661. nr_underway++;
  1662. }
  1663. bh = next;
  1664. } while (bh != head);
  1665. unlock_page(page);
  1666. goto done;
  1667. }
  1668. EXPORT_SYMBOL(__block_write_full_page);
  1669. /*
  1670. * If a page has any new buffers, zero them out here, and mark them uptodate
  1671. * and dirty so they'll be written out (in order to prevent uninitialised
  1672. * block data from leaking). And clear the new bit.
  1673. */
  1674. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1675. {
  1676. unsigned int block_start, block_end;
  1677. struct buffer_head *head, *bh;
  1678. BUG_ON(!PageLocked(page));
  1679. if (!page_has_buffers(page))
  1680. return;
  1681. bh = head = page_buffers(page);
  1682. block_start = 0;
  1683. do {
  1684. block_end = block_start + bh->b_size;
  1685. if (buffer_new(bh)) {
  1686. if (block_end > from && block_start < to) {
  1687. if (!PageUptodate(page)) {
  1688. unsigned start, size;
  1689. start = max(from, block_start);
  1690. size = min(to, block_end) - start;
  1691. zero_user(page, start, size);
  1692. set_buffer_uptodate(bh);
  1693. }
  1694. clear_buffer_new(bh);
  1695. mark_buffer_dirty(bh);
  1696. }
  1697. }
  1698. block_start = block_end;
  1699. bh = bh->b_this_page;
  1700. } while (bh != head);
  1701. }
  1702. EXPORT_SYMBOL(page_zero_new_buffers);
  1703. static void
  1704. iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
  1705. struct iomap *iomap)
  1706. {
  1707. loff_t offset = block << inode->i_blkbits;
  1708. bh->b_bdev = iomap->bdev;
  1709. /*
  1710. * Block points to offset in file we need to map, iomap contains
  1711. * the offset at which the map starts. If the map ends before the
  1712. * current block, then do not map the buffer and let the caller
  1713. * handle it.
  1714. */
  1715. BUG_ON(offset >= iomap->offset + iomap->length);
  1716. switch (iomap->type) {
  1717. case IOMAP_HOLE:
  1718. /*
  1719. * If the buffer is not up to date or beyond the current EOF,
  1720. * we need to mark it as new to ensure sub-block zeroing is
  1721. * executed if necessary.
  1722. */
  1723. if (!buffer_uptodate(bh) ||
  1724. (offset >= i_size_read(inode)))
  1725. set_buffer_new(bh);
  1726. break;
  1727. case IOMAP_DELALLOC:
  1728. if (!buffer_uptodate(bh) ||
  1729. (offset >= i_size_read(inode)))
  1730. set_buffer_new(bh);
  1731. set_buffer_uptodate(bh);
  1732. set_buffer_mapped(bh);
  1733. set_buffer_delay(bh);
  1734. break;
  1735. case IOMAP_UNWRITTEN:
  1736. /*
  1737. * For unwritten regions, we always need to ensure that
  1738. * sub-block writes cause the regions in the block we are not
  1739. * writing to are zeroed. Set the buffer as new to ensure this.
  1740. */
  1741. set_buffer_new(bh);
  1742. set_buffer_unwritten(bh);
  1743. /* FALLTHRU */
  1744. case IOMAP_MAPPED:
  1745. if (offset >= i_size_read(inode))
  1746. set_buffer_new(bh);
  1747. bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
  1748. inode->i_blkbits;
  1749. set_buffer_mapped(bh);
  1750. break;
  1751. }
  1752. }
  1753. int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
  1754. get_block_t *get_block, struct iomap *iomap)
  1755. {
  1756. unsigned from = pos & (PAGE_SIZE - 1);
  1757. unsigned to = from + len;
  1758. struct inode *inode = page->mapping->host;
  1759. unsigned block_start, block_end;
  1760. sector_t block;
  1761. int err = 0;
  1762. unsigned blocksize, bbits;
  1763. struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
  1764. BUG_ON(!PageLocked(page));
  1765. BUG_ON(from > PAGE_SIZE);
  1766. BUG_ON(to > PAGE_SIZE);
  1767. BUG_ON(from > to);
  1768. head = create_page_buffers(page, inode, 0);
  1769. blocksize = head->b_size;
  1770. bbits = block_size_bits(blocksize);
  1771. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1772. for(bh = head, block_start = 0; bh != head || !block_start;
  1773. block++, block_start=block_end, bh = bh->b_this_page) {
  1774. block_end = block_start + blocksize;
  1775. if (block_end <= from || block_start >= to) {
  1776. if (PageUptodate(page)) {
  1777. if (!buffer_uptodate(bh))
  1778. set_buffer_uptodate(bh);
  1779. }
  1780. continue;
  1781. }
  1782. if (buffer_new(bh))
  1783. clear_buffer_new(bh);
  1784. if (!buffer_mapped(bh)) {
  1785. WARN_ON(bh->b_size != blocksize);
  1786. if (get_block) {
  1787. err = get_block(inode, block, bh, 1);
  1788. if (err)
  1789. break;
  1790. } else {
  1791. iomap_to_bh(inode, block, bh, iomap);
  1792. }
  1793. if (buffer_new(bh)) {
  1794. clean_bdev_bh_alias(bh);
  1795. if (PageUptodate(page)) {
  1796. clear_buffer_new(bh);
  1797. set_buffer_uptodate(bh);
  1798. mark_buffer_dirty(bh);
  1799. continue;
  1800. }
  1801. if (block_end > to || block_start < from)
  1802. zero_user_segments(page,
  1803. to, block_end,
  1804. block_start, from);
  1805. continue;
  1806. }
  1807. }
  1808. if (PageUptodate(page)) {
  1809. if (!buffer_uptodate(bh))
  1810. set_buffer_uptodate(bh);
  1811. continue;
  1812. }
  1813. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1814. !buffer_unwritten(bh) &&
  1815. (block_start < from || block_end > to)) {
  1816. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1817. *wait_bh++=bh;
  1818. }
  1819. }
  1820. /*
  1821. * If we issued read requests - let them complete.
  1822. */
  1823. while(wait_bh > wait) {
  1824. wait_on_buffer(*--wait_bh);
  1825. if (!buffer_uptodate(*wait_bh))
  1826. err = -EIO;
  1827. }
  1828. if (unlikely(err))
  1829. page_zero_new_buffers(page, from, to);
  1830. return err;
  1831. }
  1832. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  1833. get_block_t *get_block)
  1834. {
  1835. return __block_write_begin_int(page, pos, len, get_block, NULL);
  1836. }
  1837. EXPORT_SYMBOL(__block_write_begin);
  1838. static int __block_commit_write(struct inode *inode, struct page *page,
  1839. unsigned from, unsigned to)
  1840. {
  1841. unsigned block_start, block_end;
  1842. int partial = 0;
  1843. unsigned blocksize;
  1844. struct buffer_head *bh, *head;
  1845. bh = head = page_buffers(page);
  1846. blocksize = bh->b_size;
  1847. block_start = 0;
  1848. do {
  1849. block_end = block_start + blocksize;
  1850. if (block_end <= from || block_start >= to) {
  1851. if (!buffer_uptodate(bh))
  1852. partial = 1;
  1853. } else {
  1854. set_buffer_uptodate(bh);
  1855. mark_buffer_dirty(bh);
  1856. }
  1857. clear_buffer_new(bh);
  1858. block_start = block_end;
  1859. bh = bh->b_this_page;
  1860. } while (bh != head);
  1861. /*
  1862. * If this is a partial write which happened to make all buffers
  1863. * uptodate then we can optimize away a bogus readpage() for
  1864. * the next read(). Here we 'discover' whether the page went
  1865. * uptodate as a result of this (potentially partial) write.
  1866. */
  1867. if (!partial)
  1868. SetPageUptodate(page);
  1869. return 0;
  1870. }
  1871. /*
  1872. * block_write_begin takes care of the basic task of block allocation and
  1873. * bringing partial write blocks uptodate first.
  1874. *
  1875. * The filesystem needs to handle block truncation upon failure.
  1876. */
  1877. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  1878. unsigned flags, struct page **pagep, get_block_t *get_block)
  1879. {
  1880. pgoff_t index = pos >> PAGE_SHIFT;
  1881. struct page *page;
  1882. int status;
  1883. page = grab_cache_page_write_begin(mapping, index, flags);
  1884. if (!page)
  1885. return -ENOMEM;
  1886. status = __block_write_begin(page, pos, len, get_block);
  1887. if (unlikely(status)) {
  1888. unlock_page(page);
  1889. put_page(page);
  1890. page = NULL;
  1891. }
  1892. *pagep = page;
  1893. return status;
  1894. }
  1895. EXPORT_SYMBOL(block_write_begin);
  1896. int block_write_end(struct file *file, struct address_space *mapping,
  1897. loff_t pos, unsigned len, unsigned copied,
  1898. struct page *page, void *fsdata)
  1899. {
  1900. struct inode *inode = mapping->host;
  1901. unsigned start;
  1902. start = pos & (PAGE_SIZE - 1);
  1903. if (unlikely(copied < len)) {
  1904. /*
  1905. * The buffers that were written will now be uptodate, so we
  1906. * don't have to worry about a readpage reading them and
  1907. * overwriting a partial write. However if we have encountered
  1908. * a short write and only partially written into a buffer, it
  1909. * will not be marked uptodate, so a readpage might come in and
  1910. * destroy our partial write.
  1911. *
  1912. * Do the simplest thing, and just treat any short write to a
  1913. * non uptodate page as a zero-length write, and force the
  1914. * caller to redo the whole thing.
  1915. */
  1916. if (!PageUptodate(page))
  1917. copied = 0;
  1918. page_zero_new_buffers(page, start+copied, start+len);
  1919. }
  1920. flush_dcache_page(page);
  1921. /* This could be a short (even 0-length) commit */
  1922. __block_commit_write(inode, page, start, start+copied);
  1923. return copied;
  1924. }
  1925. EXPORT_SYMBOL(block_write_end);
  1926. int generic_write_end(struct file *file, struct address_space *mapping,
  1927. loff_t pos, unsigned len, unsigned copied,
  1928. struct page *page, void *fsdata)
  1929. {
  1930. struct inode *inode = mapping->host;
  1931. loff_t old_size = inode->i_size;
  1932. int i_size_changed = 0;
  1933. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1934. /*
  1935. * No need to use i_size_read() here, the i_size
  1936. * cannot change under us because we hold i_mutex.
  1937. *
  1938. * But it's important to update i_size while still holding page lock:
  1939. * page writeout could otherwise come in and zero beyond i_size.
  1940. */
  1941. if (pos+copied > inode->i_size) {
  1942. i_size_write(inode, pos+copied);
  1943. i_size_changed = 1;
  1944. }
  1945. unlock_page(page);
  1946. put_page(page);
  1947. if (old_size < pos)
  1948. pagecache_isize_extended(inode, old_size, pos);
  1949. /*
  1950. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1951. * makes the holding time of page lock longer. Second, it forces lock
  1952. * ordering of page lock and transaction start for journaling
  1953. * filesystems.
  1954. */
  1955. if (i_size_changed)
  1956. mark_inode_dirty(inode);
  1957. return copied;
  1958. }
  1959. EXPORT_SYMBOL(generic_write_end);
  1960. /*
  1961. * block_is_partially_uptodate checks whether buffers within a page are
  1962. * uptodate or not.
  1963. *
  1964. * Returns true if all buffers which correspond to a file portion
  1965. * we want to read are uptodate.
  1966. */
  1967. int block_is_partially_uptodate(struct page *page, unsigned long from,
  1968. unsigned long count)
  1969. {
  1970. unsigned block_start, block_end, blocksize;
  1971. unsigned to;
  1972. struct buffer_head *bh, *head;
  1973. int ret = 1;
  1974. if (!page_has_buffers(page))
  1975. return 0;
  1976. head = page_buffers(page);
  1977. blocksize = head->b_size;
  1978. to = min_t(unsigned, PAGE_SIZE - from, count);
  1979. to = from + to;
  1980. if (from < blocksize && to > PAGE_SIZE - blocksize)
  1981. return 0;
  1982. bh = head;
  1983. block_start = 0;
  1984. do {
  1985. block_end = block_start + blocksize;
  1986. if (block_end > from && block_start < to) {
  1987. if (!buffer_uptodate(bh)) {
  1988. ret = 0;
  1989. break;
  1990. }
  1991. if (block_end >= to)
  1992. break;
  1993. }
  1994. block_start = block_end;
  1995. bh = bh->b_this_page;
  1996. } while (bh != head);
  1997. return ret;
  1998. }
  1999. EXPORT_SYMBOL(block_is_partially_uptodate);
  2000. /*
  2001. * Generic "read page" function for block devices that have the normal
  2002. * get_block functionality. This is most of the block device filesystems.
  2003. * Reads the page asynchronously --- the unlock_buffer() and
  2004. * set/clear_buffer_uptodate() functions propagate buffer state into the
  2005. * page struct once IO has completed.
  2006. */
  2007. int block_read_full_page(struct page *page, get_block_t *get_block)
  2008. {
  2009. struct inode *inode = page->mapping->host;
  2010. sector_t iblock, lblock;
  2011. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  2012. unsigned int blocksize, bbits;
  2013. int nr, i;
  2014. int fully_mapped = 1;
  2015. head = create_page_buffers(page, inode, 0);
  2016. blocksize = head->b_size;
  2017. bbits = block_size_bits(blocksize);
  2018. iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
  2019. lblock = (i_size_read(inode)+blocksize-1) >> bbits;
  2020. bh = head;
  2021. nr = 0;
  2022. i = 0;
  2023. do {
  2024. if (buffer_uptodate(bh))
  2025. continue;
  2026. if (!buffer_mapped(bh)) {
  2027. int err = 0;
  2028. fully_mapped = 0;
  2029. if (iblock < lblock) {
  2030. WARN_ON(bh->b_size != blocksize);
  2031. err = get_block(inode, iblock, bh, 0);
  2032. if (err)
  2033. SetPageError(page);
  2034. }
  2035. if (!buffer_mapped(bh)) {
  2036. zero_user(page, i * blocksize, blocksize);
  2037. if (!err)
  2038. set_buffer_uptodate(bh);
  2039. continue;
  2040. }
  2041. /*
  2042. * get_block() might have updated the buffer
  2043. * synchronously
  2044. */
  2045. if (buffer_uptodate(bh))
  2046. continue;
  2047. }
  2048. arr[nr++] = bh;
  2049. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  2050. if (fully_mapped)
  2051. SetPageMappedToDisk(page);
  2052. if (!nr) {
  2053. /*
  2054. * All buffers are uptodate - we can set the page uptodate
  2055. * as well. But not if get_block() returned an error.
  2056. */
  2057. if (!PageError(page))
  2058. SetPageUptodate(page);
  2059. unlock_page(page);
  2060. return 0;
  2061. }
  2062. /* Stage two: lock the buffers */
  2063. for (i = 0; i < nr; i++) {
  2064. bh = arr[i];
  2065. lock_buffer(bh);
  2066. mark_buffer_async_read(bh);
  2067. }
  2068. /*
  2069. * Stage 3: start the IO. Check for uptodateness
  2070. * inside the buffer lock in case another process reading
  2071. * the underlying blockdev brought it uptodate (the sct fix).
  2072. */
  2073. for (i = 0; i < nr; i++) {
  2074. bh = arr[i];
  2075. if (buffer_uptodate(bh))
  2076. end_buffer_async_read(bh, 1);
  2077. else
  2078. submit_bh(REQ_OP_READ, 0, bh);
  2079. }
  2080. return 0;
  2081. }
  2082. EXPORT_SYMBOL(block_read_full_page);
  2083. /* utility function for filesystems that need to do work on expanding
  2084. * truncates. Uses filesystem pagecache writes to allow the filesystem to
  2085. * deal with the hole.
  2086. */
  2087. int generic_cont_expand_simple(struct inode *inode, loff_t size)
  2088. {
  2089. struct address_space *mapping = inode->i_mapping;
  2090. struct page *page;
  2091. void *fsdata;
  2092. int err;
  2093. err = inode_newsize_ok(inode, size);
  2094. if (err)
  2095. goto out;
  2096. err = pagecache_write_begin(NULL, mapping, size, 0,
  2097. AOP_FLAG_CONT_EXPAND, &page, &fsdata);
  2098. if (err)
  2099. goto out;
  2100. err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  2101. BUG_ON(err > 0);
  2102. out:
  2103. return err;
  2104. }
  2105. EXPORT_SYMBOL(generic_cont_expand_simple);
  2106. static int cont_expand_zero(struct file *file, struct address_space *mapping,
  2107. loff_t pos, loff_t *bytes)
  2108. {
  2109. struct inode *inode = mapping->host;
  2110. unsigned int blocksize = i_blocksize(inode);
  2111. struct page *page;
  2112. void *fsdata;
  2113. pgoff_t index, curidx;
  2114. loff_t curpos;
  2115. unsigned zerofrom, offset, len;
  2116. int err = 0;
  2117. index = pos >> PAGE_SHIFT;
  2118. offset = pos & ~PAGE_MASK;
  2119. while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
  2120. zerofrom = curpos & ~PAGE_MASK;
  2121. if (zerofrom & (blocksize-1)) {
  2122. *bytes |= (blocksize-1);
  2123. (*bytes)++;
  2124. }
  2125. len = PAGE_SIZE - zerofrom;
  2126. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2127. &page, &fsdata);
  2128. if (err)
  2129. goto out;
  2130. zero_user(page, zerofrom, len);
  2131. err = pagecache_write_end(file, mapping, curpos, len, len,
  2132. page, fsdata);
  2133. if (err < 0)
  2134. goto out;
  2135. BUG_ON(err != len);
  2136. err = 0;
  2137. balance_dirty_pages_ratelimited(mapping);
  2138. if (unlikely(fatal_signal_pending(current))) {
  2139. err = -EINTR;
  2140. goto out;
  2141. }
  2142. }
  2143. /* page covers the boundary, find the boundary offset */
  2144. if (index == curidx) {
  2145. zerofrom = curpos & ~PAGE_MASK;
  2146. /* if we will expand the thing last block will be filled */
  2147. if (offset <= zerofrom) {
  2148. goto out;
  2149. }
  2150. if (zerofrom & (blocksize-1)) {
  2151. *bytes |= (blocksize-1);
  2152. (*bytes)++;
  2153. }
  2154. len = offset - zerofrom;
  2155. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2156. &page, &fsdata);
  2157. if (err)
  2158. goto out;
  2159. zero_user(page, zerofrom, len);
  2160. err = pagecache_write_end(file, mapping, curpos, len, len,
  2161. page, fsdata);
  2162. if (err < 0)
  2163. goto out;
  2164. BUG_ON(err != len);
  2165. err = 0;
  2166. }
  2167. out:
  2168. return err;
  2169. }
  2170. /*
  2171. * For moronic filesystems that do not allow holes in file.
  2172. * We may have to extend the file.
  2173. */
  2174. int cont_write_begin(struct file *file, struct address_space *mapping,
  2175. loff_t pos, unsigned len, unsigned flags,
  2176. struct page **pagep, void **fsdata,
  2177. get_block_t *get_block, loff_t *bytes)
  2178. {
  2179. struct inode *inode = mapping->host;
  2180. unsigned int blocksize = i_blocksize(inode);
  2181. unsigned int zerofrom;
  2182. int err;
  2183. err = cont_expand_zero(file, mapping, pos, bytes);
  2184. if (err)
  2185. return err;
  2186. zerofrom = *bytes & ~PAGE_MASK;
  2187. if (pos+len > *bytes && zerofrom & (blocksize-1)) {
  2188. *bytes |= (blocksize-1);
  2189. (*bytes)++;
  2190. }
  2191. return block_write_begin(mapping, pos, len, flags, pagep, get_block);
  2192. }
  2193. EXPORT_SYMBOL(cont_write_begin);
  2194. int block_commit_write(struct page *page, unsigned from, unsigned to)
  2195. {
  2196. struct inode *inode = page->mapping->host;
  2197. __block_commit_write(inode,page,from,to);
  2198. return 0;
  2199. }
  2200. EXPORT_SYMBOL(block_commit_write);
  2201. /*
  2202. * block_page_mkwrite() is not allowed to change the file size as it gets
  2203. * called from a page fault handler when a page is first dirtied. Hence we must
  2204. * be careful to check for EOF conditions here. We set the page up correctly
  2205. * for a written page which means we get ENOSPC checking when writing into
  2206. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2207. * support these features.
  2208. *
  2209. * We are not allowed to take the i_mutex here so we have to play games to
  2210. * protect against truncate races as the page could now be beyond EOF. Because
  2211. * truncate writes the inode size before removing pages, once we have the
  2212. * page lock we can determine safely if the page is beyond EOF. If it is not
  2213. * beyond EOF, then the page is guaranteed safe against truncation until we
  2214. * unlock the page.
  2215. *
  2216. * Direct callers of this function should protect against filesystem freezing
  2217. * using sb_start_pagefault() - sb_end_pagefault() functions.
  2218. */
  2219. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2220. get_block_t get_block)
  2221. {
  2222. struct page *page = vmf->page;
  2223. struct inode *inode = file_inode(vma->vm_file);
  2224. unsigned long end;
  2225. loff_t size;
  2226. int ret;
  2227. lock_page(page);
  2228. size = i_size_read(inode);
  2229. if ((page->mapping != inode->i_mapping) ||
  2230. (page_offset(page) > size)) {
  2231. /* We overload EFAULT to mean page got truncated */
  2232. ret = -EFAULT;
  2233. goto out_unlock;
  2234. }
  2235. /* page is wholly or partially inside EOF */
  2236. if (((page->index + 1) << PAGE_SHIFT) > size)
  2237. end = size & ~PAGE_MASK;
  2238. else
  2239. end = PAGE_SIZE;
  2240. ret = __block_write_begin(page, 0, end, get_block);
  2241. if (!ret)
  2242. ret = block_commit_write(page, 0, end);
  2243. if (unlikely(ret < 0))
  2244. goto out_unlock;
  2245. set_page_dirty(page);
  2246. wait_for_stable_page(page);
  2247. return 0;
  2248. out_unlock:
  2249. unlock_page(page);
  2250. return ret;
  2251. }
  2252. EXPORT_SYMBOL(block_page_mkwrite);
  2253. /*
  2254. * nobh_write_begin()'s prereads are special: the buffer_heads are freed
  2255. * immediately, while under the page lock. So it needs a special end_io
  2256. * handler which does not touch the bh after unlocking it.
  2257. */
  2258. static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
  2259. {
  2260. __end_buffer_read_notouch(bh, uptodate);
  2261. }
  2262. /*
  2263. * Attach the singly-linked list of buffers created by nobh_write_begin, to
  2264. * the page (converting it to circular linked list and taking care of page
  2265. * dirty races).
  2266. */
  2267. static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
  2268. {
  2269. struct buffer_head *bh;
  2270. BUG_ON(!PageLocked(page));
  2271. spin_lock(&page->mapping->private_lock);
  2272. bh = head;
  2273. do {
  2274. if (PageDirty(page))
  2275. set_buffer_dirty(bh);
  2276. if (!bh->b_this_page)
  2277. bh->b_this_page = head;
  2278. bh = bh->b_this_page;
  2279. } while (bh != head);
  2280. attach_page_buffers(page, head);
  2281. spin_unlock(&page->mapping->private_lock);
  2282. }
  2283. /*
  2284. * On entry, the page is fully not uptodate.
  2285. * On exit the page is fully uptodate in the areas outside (from,to)
  2286. * The filesystem needs to handle block truncation upon failure.
  2287. */
  2288. int nobh_write_begin(struct address_space *mapping,
  2289. loff_t pos, unsigned len, unsigned flags,
  2290. struct page **pagep, void **fsdata,
  2291. get_block_t *get_block)
  2292. {
  2293. struct inode *inode = mapping->host;
  2294. const unsigned blkbits = inode->i_blkbits;
  2295. const unsigned blocksize = 1 << blkbits;
  2296. struct buffer_head *head, *bh;
  2297. struct page *page;
  2298. pgoff_t index;
  2299. unsigned from, to;
  2300. unsigned block_in_page;
  2301. unsigned block_start, block_end;
  2302. sector_t block_in_file;
  2303. int nr_reads = 0;
  2304. int ret = 0;
  2305. int is_mapped_to_disk = 1;
  2306. index = pos >> PAGE_SHIFT;
  2307. from = pos & (PAGE_SIZE - 1);
  2308. to = from + len;
  2309. page = grab_cache_page_write_begin(mapping, index, flags);
  2310. if (!page)
  2311. return -ENOMEM;
  2312. *pagep = page;
  2313. *fsdata = NULL;
  2314. if (page_has_buffers(page)) {
  2315. ret = __block_write_begin(page, pos, len, get_block);
  2316. if (unlikely(ret))
  2317. goto out_release;
  2318. return ret;
  2319. }
  2320. if (PageMappedToDisk(page))
  2321. return 0;
  2322. /*
  2323. * Allocate buffers so that we can keep track of state, and potentially
  2324. * attach them to the page if an error occurs. In the common case of
  2325. * no error, they will just be freed again without ever being attached
  2326. * to the page (which is all OK, because we're under the page lock).
  2327. *
  2328. * Be careful: the buffer linked list is a NULL terminated one, rather
  2329. * than the circular one we're used to.
  2330. */
  2331. head = alloc_page_buffers(page, blocksize, false);
  2332. if (!head) {
  2333. ret = -ENOMEM;
  2334. goto out_release;
  2335. }
  2336. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  2337. /*
  2338. * We loop across all blocks in the page, whether or not they are
  2339. * part of the affected region. This is so we can discover if the
  2340. * page is fully mapped-to-disk.
  2341. */
  2342. for (block_start = 0, block_in_page = 0, bh = head;
  2343. block_start < PAGE_SIZE;
  2344. block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
  2345. int create;
  2346. block_end = block_start + blocksize;
  2347. bh->b_state = 0;
  2348. create = 1;
  2349. if (block_start >= to)
  2350. create = 0;
  2351. ret = get_block(inode, block_in_file + block_in_page,
  2352. bh, create);
  2353. if (ret)
  2354. goto failed;
  2355. if (!buffer_mapped(bh))
  2356. is_mapped_to_disk = 0;
  2357. if (buffer_new(bh))
  2358. clean_bdev_bh_alias(bh);
  2359. if (PageUptodate(page)) {
  2360. set_buffer_uptodate(bh);
  2361. continue;
  2362. }
  2363. if (buffer_new(bh) || !buffer_mapped(bh)) {
  2364. zero_user_segments(page, block_start, from,
  2365. to, block_end);
  2366. continue;
  2367. }
  2368. if (buffer_uptodate(bh))
  2369. continue; /* reiserfs does this */
  2370. if (block_start < from || block_end > to) {
  2371. lock_buffer(bh);
  2372. bh->b_end_io = end_buffer_read_nobh;
  2373. submit_bh(REQ_OP_READ, 0, bh);
  2374. nr_reads++;
  2375. }
  2376. }
  2377. if (nr_reads) {
  2378. /*
  2379. * The page is locked, so these buffers are protected from
  2380. * any VM or truncate activity. Hence we don't need to care
  2381. * for the buffer_head refcounts.
  2382. */
  2383. for (bh = head; bh; bh = bh->b_this_page) {
  2384. wait_on_buffer(bh);
  2385. if (!buffer_uptodate(bh))
  2386. ret = -EIO;
  2387. }
  2388. if (ret)
  2389. goto failed;
  2390. }
  2391. if (is_mapped_to_disk)
  2392. SetPageMappedToDisk(page);
  2393. *fsdata = head; /* to be released by nobh_write_end */
  2394. return 0;
  2395. failed:
  2396. BUG_ON(!ret);
  2397. /*
  2398. * Error recovery is a bit difficult. We need to zero out blocks that
  2399. * were newly allocated, and dirty them to ensure they get written out.
  2400. * Buffers need to be attached to the page at this point, otherwise
  2401. * the handling of potential IO errors during writeout would be hard
  2402. * (could try doing synchronous writeout, but what if that fails too?)
  2403. */
  2404. attach_nobh_buffers(page, head);
  2405. page_zero_new_buffers(page, from, to);
  2406. out_release:
  2407. unlock_page(page);
  2408. put_page(page);
  2409. *pagep = NULL;
  2410. return ret;
  2411. }
  2412. EXPORT_SYMBOL(nobh_write_begin);
  2413. int nobh_write_end(struct file *file, struct address_space *mapping,
  2414. loff_t pos, unsigned len, unsigned copied,
  2415. struct page *page, void *fsdata)
  2416. {
  2417. struct inode *inode = page->mapping->host;
  2418. struct buffer_head *head = fsdata;
  2419. struct buffer_head *bh;
  2420. BUG_ON(fsdata != NULL && page_has_buffers(page));
  2421. if (unlikely(copied < len) && head)
  2422. attach_nobh_buffers(page, head);
  2423. if (page_has_buffers(page))
  2424. return generic_write_end(file, mapping, pos, len,
  2425. copied, page, fsdata);
  2426. SetPageUptodate(page);
  2427. set_page_dirty(page);
  2428. if (pos+copied > inode->i_size) {
  2429. i_size_write(inode, pos+copied);
  2430. mark_inode_dirty(inode);
  2431. }
  2432. unlock_page(page);
  2433. put_page(page);
  2434. while (head) {
  2435. bh = head;
  2436. head = head->b_this_page;
  2437. free_buffer_head(bh);
  2438. }
  2439. return copied;
  2440. }
  2441. EXPORT_SYMBOL(nobh_write_end);
  2442. /*
  2443. * nobh_writepage() - based on block_full_write_page() except
  2444. * that it tries to operate without attaching bufferheads to
  2445. * the page.
  2446. */
  2447. int nobh_writepage(struct page *page, get_block_t *get_block,
  2448. struct writeback_control *wbc)
  2449. {
  2450. struct inode * const inode = page->mapping->host;
  2451. loff_t i_size = i_size_read(inode);
  2452. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2453. unsigned offset;
  2454. int ret;
  2455. /* Is the page fully inside i_size? */
  2456. if (page->index < end_index)
  2457. goto out;
  2458. /* Is the page fully outside i_size? (truncate in progress) */
  2459. offset = i_size & (PAGE_SIZE-1);
  2460. if (page->index >= end_index+1 || !offset) {
  2461. /*
  2462. * The page may have dirty, unmapped buffers. For example,
  2463. * they may have been added in ext3_writepage(). Make them
  2464. * freeable here, so the page does not leak.
  2465. */
  2466. #if 0
  2467. /* Not really sure about this - do we need this ? */
  2468. if (page->mapping->a_ops->invalidatepage)
  2469. page->mapping->a_ops->invalidatepage(page, offset);
  2470. #endif
  2471. unlock_page(page);
  2472. return 0; /* don't care */
  2473. }
  2474. /*
  2475. * The page straddles i_size. It must be zeroed out on each and every
  2476. * writepage invocation because it may be mmapped. "A file is mapped
  2477. * in multiples of the page size. For a file that is not a multiple of
  2478. * the page size, the remaining memory is zeroed when mapped, and
  2479. * writes to that region are not written out to the file."
  2480. */
  2481. zero_user_segment(page, offset, PAGE_SIZE);
  2482. out:
  2483. ret = mpage_writepage(page, get_block, wbc);
  2484. if (ret == -EAGAIN)
  2485. ret = __block_write_full_page(inode, page, get_block, wbc,
  2486. end_buffer_async_write);
  2487. return ret;
  2488. }
  2489. EXPORT_SYMBOL(nobh_writepage);
  2490. int nobh_truncate_page(struct address_space *mapping,
  2491. loff_t from, get_block_t *get_block)
  2492. {
  2493. pgoff_t index = from >> PAGE_SHIFT;
  2494. unsigned offset = from & (PAGE_SIZE-1);
  2495. unsigned blocksize;
  2496. sector_t iblock;
  2497. unsigned length, pos;
  2498. struct inode *inode = mapping->host;
  2499. struct page *page;
  2500. struct buffer_head map_bh;
  2501. int err;
  2502. blocksize = i_blocksize(inode);
  2503. length = offset & (blocksize - 1);
  2504. /* Block boundary? Nothing to do */
  2505. if (!length)
  2506. return 0;
  2507. length = blocksize - length;
  2508. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2509. page = grab_cache_page(mapping, index);
  2510. err = -ENOMEM;
  2511. if (!page)
  2512. goto out;
  2513. if (page_has_buffers(page)) {
  2514. has_buffers:
  2515. unlock_page(page);
  2516. put_page(page);
  2517. return block_truncate_page(mapping, from, get_block);
  2518. }
  2519. /* Find the buffer that contains "offset" */
  2520. pos = blocksize;
  2521. while (offset >= pos) {
  2522. iblock++;
  2523. pos += blocksize;
  2524. }
  2525. map_bh.b_size = blocksize;
  2526. map_bh.b_state = 0;
  2527. err = get_block(inode, iblock, &map_bh, 0);
  2528. if (err)
  2529. goto unlock;
  2530. /* unmapped? It's a hole - nothing to do */
  2531. if (!buffer_mapped(&map_bh))
  2532. goto unlock;
  2533. /* Ok, it's mapped. Make sure it's up-to-date */
  2534. if (!PageUptodate(page)) {
  2535. err = mapping->a_ops->readpage(NULL, page);
  2536. if (err) {
  2537. put_page(page);
  2538. goto out;
  2539. }
  2540. lock_page(page);
  2541. if (!PageUptodate(page)) {
  2542. err = -EIO;
  2543. goto unlock;
  2544. }
  2545. if (page_has_buffers(page))
  2546. goto has_buffers;
  2547. }
  2548. zero_user(page, offset, length);
  2549. set_page_dirty(page);
  2550. err = 0;
  2551. unlock:
  2552. unlock_page(page);
  2553. put_page(page);
  2554. out:
  2555. return err;
  2556. }
  2557. EXPORT_SYMBOL(nobh_truncate_page);
  2558. int block_truncate_page(struct address_space *mapping,
  2559. loff_t from, get_block_t *get_block)
  2560. {
  2561. pgoff_t index = from >> PAGE_SHIFT;
  2562. unsigned offset = from & (PAGE_SIZE-1);
  2563. unsigned blocksize;
  2564. sector_t iblock;
  2565. unsigned length, pos;
  2566. struct inode *inode = mapping->host;
  2567. struct page *page;
  2568. struct buffer_head *bh;
  2569. int err;
  2570. blocksize = i_blocksize(inode);
  2571. length = offset & (blocksize - 1);
  2572. /* Block boundary? Nothing to do */
  2573. if (!length)
  2574. return 0;
  2575. length = blocksize - length;
  2576. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2577. page = grab_cache_page(mapping, index);
  2578. err = -ENOMEM;
  2579. if (!page)
  2580. goto out;
  2581. if (!page_has_buffers(page))
  2582. create_empty_buffers(page, blocksize, 0);
  2583. /* Find the buffer that contains "offset" */
  2584. bh = page_buffers(page);
  2585. pos = blocksize;
  2586. while (offset >= pos) {
  2587. bh = bh->b_this_page;
  2588. iblock++;
  2589. pos += blocksize;
  2590. }
  2591. err = 0;
  2592. if (!buffer_mapped(bh)) {
  2593. WARN_ON(bh->b_size != blocksize);
  2594. err = get_block(inode, iblock, bh, 0);
  2595. if (err)
  2596. goto unlock;
  2597. /* unmapped? It's a hole - nothing to do */
  2598. if (!buffer_mapped(bh))
  2599. goto unlock;
  2600. }
  2601. /* Ok, it's mapped. Make sure it's up-to-date */
  2602. if (PageUptodate(page))
  2603. set_buffer_uptodate(bh);
  2604. if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
  2605. err = -EIO;
  2606. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  2607. wait_on_buffer(bh);
  2608. /* Uhhuh. Read error. Complain and punt. */
  2609. if (!buffer_uptodate(bh))
  2610. goto unlock;
  2611. }
  2612. zero_user(page, offset, length);
  2613. mark_buffer_dirty(bh);
  2614. err = 0;
  2615. unlock:
  2616. unlock_page(page);
  2617. put_page(page);
  2618. out:
  2619. return err;
  2620. }
  2621. EXPORT_SYMBOL(block_truncate_page);
  2622. /*
  2623. * The generic ->writepage function for buffer-backed address_spaces
  2624. */
  2625. int block_write_full_page(struct page *page, get_block_t *get_block,
  2626. struct writeback_control *wbc)
  2627. {
  2628. struct inode * const inode = page->mapping->host;
  2629. loff_t i_size = i_size_read(inode);
  2630. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2631. unsigned offset;
  2632. /* Is the page fully inside i_size? */
  2633. if (page->index < end_index)
  2634. return __block_write_full_page(inode, page, get_block, wbc,
  2635. end_buffer_async_write);
  2636. /* Is the page fully outside i_size? (truncate in progress) */
  2637. offset = i_size & (PAGE_SIZE-1);
  2638. if (page->index >= end_index+1 || !offset) {
  2639. /*
  2640. * The page may have dirty, unmapped buffers. For example,
  2641. * they may have been added in ext3_writepage(). Make them
  2642. * freeable here, so the page does not leak.
  2643. */
  2644. do_invalidatepage(page, 0, PAGE_SIZE);
  2645. unlock_page(page);
  2646. return 0; /* don't care */
  2647. }
  2648. /*
  2649. * The page straddles i_size. It must be zeroed out on each and every
  2650. * writepage invocation because it may be mmapped. "A file is mapped
  2651. * in multiples of the page size. For a file that is not a multiple of
  2652. * the page size, the remaining memory is zeroed when mapped, and
  2653. * writes to that region are not written out to the file."
  2654. */
  2655. zero_user_segment(page, offset, PAGE_SIZE);
  2656. return __block_write_full_page(inode, page, get_block, wbc,
  2657. end_buffer_async_write);
  2658. }
  2659. EXPORT_SYMBOL(block_write_full_page);
  2660. sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
  2661. get_block_t *get_block)
  2662. {
  2663. struct inode *inode = mapping->host;
  2664. struct buffer_head tmp = {
  2665. .b_size = i_blocksize(inode),
  2666. };
  2667. get_block(inode, block, &tmp, 0);
  2668. return tmp.b_blocknr;
  2669. }
  2670. EXPORT_SYMBOL(generic_block_bmap);
  2671. static void end_bio_bh_io_sync(struct bio *bio)
  2672. {
  2673. struct buffer_head *bh = bio->bi_private;
  2674. if (unlikely(bio_flagged(bio, BIO_QUIET)))
  2675. set_bit(BH_Quiet, &bh->b_state);
  2676. bh->b_end_io(bh, !bio->bi_status);
  2677. bio_put(bio);
  2678. }
  2679. /*
  2680. * This allows us to do IO even on the odd last sectors
  2681. * of a device, even if the block size is some multiple
  2682. * of the physical sector size.
  2683. *
  2684. * We'll just truncate the bio to the size of the device,
  2685. * and clear the end of the buffer head manually.
  2686. *
  2687. * Truly out-of-range accesses will turn into actual IO
  2688. * errors, this only handles the "we need to be able to
  2689. * do IO at the final sector" case.
  2690. */
  2691. void guard_bio_eod(int op, struct bio *bio)
  2692. {
  2693. sector_t maxsector;
  2694. struct bio_vec *bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
  2695. unsigned truncated_bytes;
  2696. struct hd_struct *part;
  2697. rcu_read_lock();
  2698. part = __disk_get_part(bio->bi_disk, bio->bi_partno);
  2699. if (part)
  2700. maxsector = part_nr_sects_read(part);
  2701. else
  2702. maxsector = get_capacity(bio->bi_disk);
  2703. rcu_read_unlock();
  2704. if (!maxsector)
  2705. return;
  2706. /*
  2707. * If the *whole* IO is past the end of the device,
  2708. * let it through, and the IO layer will turn it into
  2709. * an EIO.
  2710. */
  2711. if (unlikely(bio->bi_iter.bi_sector >= maxsector))
  2712. return;
  2713. maxsector -= bio->bi_iter.bi_sector;
  2714. if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
  2715. return;
  2716. /* Uhhuh. We've got a bio that straddles the device size! */
  2717. truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
  2718. /* Truncate the bio.. */
  2719. bio->bi_iter.bi_size -= truncated_bytes;
  2720. bvec->bv_len -= truncated_bytes;
  2721. /* ..and clear the end of the buffer for reads */
  2722. if (op == REQ_OP_READ) {
  2723. zero_user(bvec->bv_page, bvec->bv_offset + bvec->bv_len,
  2724. truncated_bytes);
  2725. }
  2726. }
  2727. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  2728. enum rw_hint write_hint, struct writeback_control *wbc)
  2729. {
  2730. struct bio *bio;
  2731. BUG_ON(!buffer_locked(bh));
  2732. BUG_ON(!buffer_mapped(bh));
  2733. BUG_ON(!bh->b_end_io);
  2734. BUG_ON(buffer_delay(bh));
  2735. BUG_ON(buffer_unwritten(bh));
  2736. /*
  2737. * Only clear out a write error when rewriting
  2738. */
  2739. if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
  2740. clear_buffer_write_io_error(bh);
  2741. /*
  2742. * from here on down, it's all bio -- do the initial mapping,
  2743. * submit_bio -> generic_make_request may further map this bio around
  2744. */
  2745. bio = bio_alloc(GFP_NOIO, 1);
  2746. if (wbc) {
  2747. wbc_init_bio(wbc, bio);
  2748. wbc_account_io(wbc, bh->b_page, bh->b_size);
  2749. }
  2750. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  2751. bio_set_dev(bio, bh->b_bdev);
  2752. bio->bi_write_hint = write_hint;
  2753. bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  2754. BUG_ON(bio->bi_iter.bi_size != bh->b_size);
  2755. bio->bi_end_io = end_bio_bh_io_sync;
  2756. bio->bi_private = bh;
  2757. /* Take care of bh's that straddle the end of the device */
  2758. guard_bio_eod(op, bio);
  2759. if (buffer_meta(bh))
  2760. op_flags |= REQ_META;
  2761. if (buffer_prio(bh))
  2762. op_flags |= REQ_PRIO;
  2763. bio_set_op_attrs(bio, op, op_flags);
  2764. submit_bio(bio);
  2765. return 0;
  2766. }
  2767. int submit_bh(int op, int op_flags, struct buffer_head *bh)
  2768. {
  2769. return submit_bh_wbc(op, op_flags, bh, 0, NULL);
  2770. }
  2771. EXPORT_SYMBOL(submit_bh);
  2772. /**
  2773. * ll_rw_block: low-level access to block devices (DEPRECATED)
  2774. * @op: whether to %READ or %WRITE
  2775. * @op_flags: req_flag_bits
  2776. * @nr: number of &struct buffer_heads in the array
  2777. * @bhs: array of pointers to &struct buffer_head
  2778. *
  2779. * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
  2780. * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
  2781. * @op_flags contains flags modifying the detailed I/O behavior, most notably
  2782. * %REQ_RAHEAD.
  2783. *
  2784. * This function drops any buffer that it cannot get a lock on (with the
  2785. * BH_Lock state bit), any buffer that appears to be clean when doing a write
  2786. * request, and any buffer that appears to be up-to-date when doing read
  2787. * request. Further it marks as clean buffers that are processed for
  2788. * writing (the buffer cache won't assume that they are actually clean
  2789. * until the buffer gets unlocked).
  2790. *
  2791. * ll_rw_block sets b_end_io to simple completion handler that marks
  2792. * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
  2793. * any waiters.
  2794. *
  2795. * All of the buffers must be for the same device, and must also be a
  2796. * multiple of the current approved size for the device.
  2797. */
  2798. void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
  2799. {
  2800. int i;
  2801. for (i = 0; i < nr; i++) {
  2802. struct buffer_head *bh = bhs[i];
  2803. if (!trylock_buffer(bh))
  2804. continue;
  2805. if (op == WRITE) {
  2806. if (test_clear_buffer_dirty(bh)) {
  2807. bh->b_end_io = end_buffer_write_sync;
  2808. get_bh(bh);
  2809. submit_bh(op, op_flags, bh);
  2810. continue;
  2811. }
  2812. } else {
  2813. if (!buffer_uptodate(bh)) {
  2814. bh->b_end_io = end_buffer_read_sync;
  2815. get_bh(bh);
  2816. submit_bh(op, op_flags, bh);
  2817. continue;
  2818. }
  2819. }
  2820. unlock_buffer(bh);
  2821. }
  2822. }
  2823. EXPORT_SYMBOL(ll_rw_block);
  2824. void write_dirty_buffer(struct buffer_head *bh, int op_flags)
  2825. {
  2826. lock_buffer(bh);
  2827. if (!test_clear_buffer_dirty(bh)) {
  2828. unlock_buffer(bh);
  2829. return;
  2830. }
  2831. bh->b_end_io = end_buffer_write_sync;
  2832. get_bh(bh);
  2833. submit_bh(REQ_OP_WRITE, op_flags, bh);
  2834. }
  2835. EXPORT_SYMBOL(write_dirty_buffer);
  2836. /*
  2837. * For a data-integrity writeout, we need to wait upon any in-progress I/O
  2838. * and then start new I/O and then wait upon it. The caller must have a ref on
  2839. * the buffer_head.
  2840. */
  2841. int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
  2842. {
  2843. int ret = 0;
  2844. WARN_ON(atomic_read(&bh->b_count) < 1);
  2845. lock_buffer(bh);
  2846. if (test_clear_buffer_dirty(bh)) {
  2847. get_bh(bh);
  2848. bh->b_end_io = end_buffer_write_sync;
  2849. ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
  2850. wait_on_buffer(bh);
  2851. if (!ret && !buffer_uptodate(bh))
  2852. ret = -EIO;
  2853. } else {
  2854. unlock_buffer(bh);
  2855. }
  2856. return ret;
  2857. }
  2858. EXPORT_SYMBOL(__sync_dirty_buffer);
  2859. int sync_dirty_buffer(struct buffer_head *bh)
  2860. {
  2861. return __sync_dirty_buffer(bh, REQ_SYNC);
  2862. }
  2863. EXPORT_SYMBOL(sync_dirty_buffer);
  2864. /*
  2865. * try_to_free_buffers() checks if all the buffers on this particular page
  2866. * are unused, and releases them if so.
  2867. *
  2868. * Exclusion against try_to_free_buffers may be obtained by either
  2869. * locking the page or by holding its mapping's private_lock.
  2870. *
  2871. * If the page is dirty but all the buffers are clean then we need to
  2872. * be sure to mark the page clean as well. This is because the page
  2873. * may be against a block device, and a later reattachment of buffers
  2874. * to a dirty page will set *all* buffers dirty. Which would corrupt
  2875. * filesystem data on the same device.
  2876. *
  2877. * The same applies to regular filesystem pages: if all the buffers are
  2878. * clean then we set the page clean and proceed. To do that, we require
  2879. * total exclusion from __set_page_dirty_buffers(). That is obtained with
  2880. * private_lock.
  2881. *
  2882. * try_to_free_buffers() is non-blocking.
  2883. */
  2884. static inline int buffer_busy(struct buffer_head *bh)
  2885. {
  2886. return atomic_read(&bh->b_count) |
  2887. (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
  2888. }
  2889. static int
  2890. drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
  2891. {
  2892. struct buffer_head *head = page_buffers(page);
  2893. struct buffer_head *bh;
  2894. bh = head;
  2895. do {
  2896. if (buffer_busy(bh))
  2897. goto failed;
  2898. bh = bh->b_this_page;
  2899. } while (bh != head);
  2900. do {
  2901. struct buffer_head *next = bh->b_this_page;
  2902. if (bh->b_assoc_map)
  2903. __remove_assoc_queue(bh);
  2904. bh = next;
  2905. } while (bh != head);
  2906. *buffers_to_free = head;
  2907. __clear_page_buffers(page);
  2908. return 1;
  2909. failed:
  2910. return 0;
  2911. }
  2912. int try_to_free_buffers(struct page *page)
  2913. {
  2914. struct address_space * const mapping = page->mapping;
  2915. struct buffer_head *buffers_to_free = NULL;
  2916. int ret = 0;
  2917. BUG_ON(!PageLocked(page));
  2918. if (PageWriteback(page))
  2919. return 0;
  2920. if (mapping == NULL) { /* can this still happen? */
  2921. ret = drop_buffers(page, &buffers_to_free);
  2922. goto out;
  2923. }
  2924. spin_lock(&mapping->private_lock);
  2925. ret = drop_buffers(page, &buffers_to_free);
  2926. /*
  2927. * If the filesystem writes its buffers by hand (eg ext3)
  2928. * then we can have clean buffers against a dirty page. We
  2929. * clean the page here; otherwise the VM will never notice
  2930. * that the filesystem did any IO at all.
  2931. *
  2932. * Also, during truncate, discard_buffer will have marked all
  2933. * the page's buffers clean. We discover that here and clean
  2934. * the page also.
  2935. *
  2936. * private_lock must be held over this entire operation in order
  2937. * to synchronise against __set_page_dirty_buffers and prevent the
  2938. * dirty bit from being lost.
  2939. */
  2940. if (ret)
  2941. cancel_dirty_page(page);
  2942. spin_unlock(&mapping->private_lock);
  2943. out:
  2944. if (buffers_to_free) {
  2945. struct buffer_head *bh = buffers_to_free;
  2946. do {
  2947. struct buffer_head *next = bh->b_this_page;
  2948. free_buffer_head(bh);
  2949. bh = next;
  2950. } while (bh != buffers_to_free);
  2951. }
  2952. return ret;
  2953. }
  2954. EXPORT_SYMBOL(try_to_free_buffers);
  2955. /*
  2956. * There are no bdflush tunables left. But distributions are
  2957. * still running obsolete flush daemons, so we terminate them here.
  2958. *
  2959. * Use of bdflush() is deprecated and will be removed in a future kernel.
  2960. * The `flush-X' kernel threads fully replace bdflush daemons and this call.
  2961. */
  2962. SYSCALL_DEFINE2(bdflush, int, func, long, data)
  2963. {
  2964. static int msg_count;
  2965. if (!capable(CAP_SYS_ADMIN))
  2966. return -EPERM;
  2967. if (msg_count < 5) {
  2968. msg_count++;
  2969. printk(KERN_INFO
  2970. "warning: process `%s' used the obsolete bdflush"
  2971. " system call\n", current->comm);
  2972. printk(KERN_INFO "Fix your initscripts?\n");
  2973. }
  2974. if (func == 1)
  2975. do_exit(0);
  2976. return 0;
  2977. }
  2978. /*
  2979. * Buffer-head allocation
  2980. */
  2981. static struct kmem_cache *bh_cachep __read_mostly;
  2982. /*
  2983. * Once the number of bh's in the machine exceeds this level, we start
  2984. * stripping them in writeback.
  2985. */
  2986. static unsigned long max_buffer_heads;
  2987. int buffer_heads_over_limit;
  2988. struct bh_accounting {
  2989. int nr; /* Number of live bh's */
  2990. int ratelimit; /* Limit cacheline bouncing */
  2991. };
  2992. static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
  2993. static void recalc_bh_state(void)
  2994. {
  2995. int i;
  2996. int tot = 0;
  2997. if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
  2998. return;
  2999. __this_cpu_write(bh_accounting.ratelimit, 0);
  3000. for_each_online_cpu(i)
  3001. tot += per_cpu(bh_accounting, i).nr;
  3002. buffer_heads_over_limit = (tot > max_buffer_heads);
  3003. }
  3004. struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  3005. {
  3006. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  3007. if (ret) {
  3008. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  3009. preempt_disable();
  3010. __this_cpu_inc(bh_accounting.nr);
  3011. recalc_bh_state();
  3012. preempt_enable();
  3013. }
  3014. return ret;
  3015. }
  3016. EXPORT_SYMBOL(alloc_buffer_head);
  3017. void free_buffer_head(struct buffer_head *bh)
  3018. {
  3019. BUG_ON(!list_empty(&bh->b_assoc_buffers));
  3020. kmem_cache_free(bh_cachep, bh);
  3021. preempt_disable();
  3022. __this_cpu_dec(bh_accounting.nr);
  3023. recalc_bh_state();
  3024. preempt_enable();
  3025. }
  3026. EXPORT_SYMBOL(free_buffer_head);
  3027. static int buffer_exit_cpu_dead(unsigned int cpu)
  3028. {
  3029. int i;
  3030. struct bh_lru *b = &per_cpu(bh_lrus, cpu);
  3031. for (i = 0; i < BH_LRU_SIZE; i++) {
  3032. brelse(b->bhs[i]);
  3033. b->bhs[i] = NULL;
  3034. }
  3035. this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
  3036. per_cpu(bh_accounting, cpu).nr = 0;
  3037. return 0;
  3038. }
  3039. /**
  3040. * bh_uptodate_or_lock - Test whether the buffer is uptodate
  3041. * @bh: struct buffer_head
  3042. *
  3043. * Return true if the buffer is up-to-date and false,
  3044. * with the buffer locked, if not.
  3045. */
  3046. int bh_uptodate_or_lock(struct buffer_head *bh)
  3047. {
  3048. if (!buffer_uptodate(bh)) {
  3049. lock_buffer(bh);
  3050. if (!buffer_uptodate(bh))
  3051. return 0;
  3052. unlock_buffer(bh);
  3053. }
  3054. return 1;
  3055. }
  3056. EXPORT_SYMBOL(bh_uptodate_or_lock);
  3057. /**
  3058. * bh_submit_read - Submit a locked buffer for reading
  3059. * @bh: struct buffer_head
  3060. *
  3061. * Returns zero on success and -EIO on error.
  3062. */
  3063. int bh_submit_read(struct buffer_head *bh)
  3064. {
  3065. BUG_ON(!buffer_locked(bh));
  3066. if (buffer_uptodate(bh)) {
  3067. unlock_buffer(bh);
  3068. return 0;
  3069. }
  3070. get_bh(bh);
  3071. bh->b_end_io = end_buffer_read_sync;
  3072. submit_bh(REQ_OP_READ, 0, bh);
  3073. wait_on_buffer(bh);
  3074. if (buffer_uptodate(bh))
  3075. return 0;
  3076. return -EIO;
  3077. }
  3078. EXPORT_SYMBOL(bh_submit_read);
  3079. /*
  3080. * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
  3081. *
  3082. * Returns the offset within the file on success, and -ENOENT otherwise.
  3083. */
  3084. static loff_t
  3085. page_seek_hole_data(struct page *page, loff_t lastoff, int whence)
  3086. {
  3087. loff_t offset = page_offset(page);
  3088. struct buffer_head *bh, *head;
  3089. bool seek_data = whence == SEEK_DATA;
  3090. if (lastoff < offset)
  3091. lastoff = offset;
  3092. bh = head = page_buffers(page);
  3093. do {
  3094. offset += bh->b_size;
  3095. if (lastoff >= offset)
  3096. continue;
  3097. /*
  3098. * Unwritten extents that have data in the page cache covering
  3099. * them can be identified by the BH_Unwritten state flag.
  3100. * Pages with multiple buffers might have a mix of holes, data
  3101. * and unwritten extents - any buffer with valid data in it
  3102. * should have BH_Uptodate flag set on it.
  3103. */
  3104. if ((buffer_unwritten(bh) || buffer_uptodate(bh)) == seek_data)
  3105. return lastoff;
  3106. lastoff = offset;
  3107. } while ((bh = bh->b_this_page) != head);
  3108. return -ENOENT;
  3109. }
  3110. /*
  3111. * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
  3112. *
  3113. * Within unwritten extents, the page cache determines which parts are holes
  3114. * and which are data: unwritten and uptodate buffer heads count as data;
  3115. * everything else counts as a hole.
  3116. *
  3117. * Returns the resulting offset on successs, and -ENOENT otherwise.
  3118. */
  3119. loff_t
  3120. page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,
  3121. int whence)
  3122. {
  3123. pgoff_t index = offset >> PAGE_SHIFT;
  3124. pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE);
  3125. loff_t lastoff = offset;
  3126. struct pagevec pvec;
  3127. if (length <= 0)
  3128. return -ENOENT;
  3129. pagevec_init(&pvec);
  3130. do {
  3131. unsigned nr_pages, i;
  3132. nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index,
  3133. end - 1);
  3134. if (nr_pages == 0)
  3135. break;
  3136. for (i = 0; i < nr_pages; i++) {
  3137. struct page *page = pvec.pages[i];
  3138. /*
  3139. * At this point, the page may be truncated or
  3140. * invalidated (changing page->mapping to NULL), or
  3141. * even swizzled back from swapper_space to tmpfs file
  3142. * mapping. However, page->index will not change
  3143. * because we have a reference on the page.
  3144. *
  3145. * If current page offset is beyond where we've ended,
  3146. * we've found a hole.
  3147. */
  3148. if (whence == SEEK_HOLE &&
  3149. lastoff < page_offset(page))
  3150. goto check_range;
  3151. lock_page(page);
  3152. if (likely(page->mapping == inode->i_mapping) &&
  3153. page_has_buffers(page)) {
  3154. lastoff = page_seek_hole_data(page, lastoff, whence);
  3155. if (lastoff >= 0) {
  3156. unlock_page(page);
  3157. goto check_range;
  3158. }
  3159. }
  3160. unlock_page(page);
  3161. lastoff = page_offset(page) + PAGE_SIZE;
  3162. }
  3163. pagevec_release(&pvec);
  3164. } while (index < end);
  3165. /* When no page at lastoff and we are not done, we found a hole. */
  3166. if (whence != SEEK_HOLE)
  3167. goto not_found;
  3168. check_range:
  3169. if (lastoff < offset + length)
  3170. goto out;
  3171. not_found:
  3172. lastoff = -ENOENT;
  3173. out:
  3174. pagevec_release(&pvec);
  3175. return lastoff;
  3176. }
  3177. void __init buffer_init(void)
  3178. {
  3179. unsigned long nrpages;
  3180. int ret;
  3181. bh_cachep = kmem_cache_create("buffer_head",
  3182. sizeof(struct buffer_head), 0,
  3183. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  3184. SLAB_MEM_SPREAD),
  3185. NULL);
  3186. /*
  3187. * Limit the bh occupancy to 10% of ZONE_NORMAL
  3188. */
  3189. nrpages = (nr_free_buffer_pages() * 10) / 100;
  3190. max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
  3191. ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
  3192. NULL, buffer_exit_cpu_dead);
  3193. WARN_ON(ret < 0);
  3194. }