buffer.c 93 KB

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