buffer.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472
  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/cpu.h>
  41. #include <linux/bitops.h>
  42. #include <linux/mpage.h>
  43. #include <linux/bit_spinlock.h>
  44. #include <linux/pagevec.h>
  45. #include <linux/sched/mm.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, private_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.
  170. */
  171. static struct buffer_head *
  172. __find_get_block_slow(struct block_device *bdev, sector_t block)
  173. {
  174. struct inode *bd_inode = bdev->bd_inode;
  175. struct address_space *bd_mapping = bd_inode->i_mapping;
  176. struct buffer_head *ret = NULL;
  177. pgoff_t index;
  178. struct buffer_head *bh;
  179. struct buffer_head *head;
  180. struct page *page;
  181. int all_mapped = 1;
  182. static DEFINE_RATELIMIT_STATE(last_warned, HZ, 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. ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE);
  208. if (all_mapped && __ratelimit(&last_warned)) {
  209. printk("__find_get_block_slow() failed. block=%llu, "
  210. "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, "
  211. "device %pg blocksize: %d\n",
  212. (unsigned long long)block,
  213. (unsigned long long)bh->b_blocknr,
  214. bh->b_state, bh->b_size, 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. void emergency_thaw_bdev(struct super_block *sb)
  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. /**
  458. * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
  459. * @mapping: the mapping which wants those buffers written
  460. *
  461. * Starts I/O against the buffers at mapping->private_list, and waits upon
  462. * that I/O.
  463. *
  464. * Basically, this is a convenience function for fsync().
  465. * @mapping is a file or directory which needs those buffers to be written for
  466. * a successful fsync().
  467. */
  468. int sync_mapping_buffers(struct address_space *mapping)
  469. {
  470. struct address_space *buffer_mapping = mapping->private_data;
  471. if (buffer_mapping == NULL || list_empty(&mapping->private_list))
  472. return 0;
  473. return fsync_buffers_list(&buffer_mapping->private_lock,
  474. &mapping->private_list);
  475. }
  476. EXPORT_SYMBOL(sync_mapping_buffers);
  477. /*
  478. * Called when we've recently written block `bblock', and it is known that
  479. * `bblock' was for a buffer_boundary() buffer. This means that the block at
  480. * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
  481. * dirty, schedule it for IO. So that indirects merge nicely with their data.
  482. */
  483. void write_boundary_block(struct block_device *bdev,
  484. sector_t bblock, unsigned blocksize)
  485. {
  486. struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
  487. if (bh) {
  488. if (buffer_dirty(bh))
  489. ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
  490. put_bh(bh);
  491. }
  492. }
  493. void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
  494. {
  495. struct address_space *mapping = inode->i_mapping;
  496. struct address_space *buffer_mapping = bh->b_page->mapping;
  497. mark_buffer_dirty(bh);
  498. if (!mapping->private_data) {
  499. mapping->private_data = buffer_mapping;
  500. } else {
  501. BUG_ON(mapping->private_data != buffer_mapping);
  502. }
  503. if (!bh->b_assoc_map) {
  504. spin_lock(&buffer_mapping->private_lock);
  505. list_move_tail(&bh->b_assoc_buffers,
  506. &mapping->private_list);
  507. bh->b_assoc_map = mapping;
  508. spin_unlock(&buffer_mapping->private_lock);
  509. }
  510. }
  511. EXPORT_SYMBOL(mark_buffer_dirty_inode);
  512. /*
  513. * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
  514. * dirty.
  515. *
  516. * If warn is true, then emit a warning if the page is not uptodate and has
  517. * not been truncated.
  518. *
  519. * The caller must hold lock_page_memcg().
  520. */
  521. void __set_page_dirty(struct page *page, struct address_space *mapping,
  522. int warn)
  523. {
  524. unsigned long flags;
  525. xa_lock_irqsave(&mapping->i_pages, flags);
  526. if (page->mapping) { /* Race with truncate? */
  527. WARN_ON_ONCE(warn && !PageUptodate(page));
  528. account_page_dirtied(page, mapping);
  529. radix_tree_tag_set(&mapping->i_pages,
  530. page_index(page), PAGECACHE_TAG_DIRTY);
  531. }
  532. xa_unlock_irqrestore(&mapping->i_pages, flags);
  533. }
  534. EXPORT_SYMBOL_GPL(__set_page_dirty);
  535. /*
  536. * Add a page to the dirty page list.
  537. *
  538. * It is a sad fact of life that this function is called from several places
  539. * deeply under spinlocking. It may not sleep.
  540. *
  541. * If the page has buffers, the uptodate buffers are set dirty, to preserve
  542. * dirty-state coherency between the page and the buffers. It the page does
  543. * not have buffers then when they are later attached they will all be set
  544. * dirty.
  545. *
  546. * The buffers are dirtied before the page is dirtied. There's a small race
  547. * window in which a writepage caller may see the page cleanness but not the
  548. * buffer dirtiness. That's fine. If this code were to set the page dirty
  549. * before the buffers, a concurrent writepage caller could clear the page dirty
  550. * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
  551. * page on the dirty page list.
  552. *
  553. * We use private_lock to lock against try_to_free_buffers while using the
  554. * page's buffer list. Also use this to protect against clean buffers being
  555. * added to the page after it was set dirty.
  556. *
  557. * FIXME: may need to call ->reservepage here as well. That's rather up to the
  558. * address_space though.
  559. */
  560. int __set_page_dirty_buffers(struct page *page)
  561. {
  562. int newly_dirty;
  563. struct address_space *mapping = page_mapping(page);
  564. if (unlikely(!mapping))
  565. return !TestSetPageDirty(page);
  566. spin_lock(&mapping->private_lock);
  567. if (page_has_buffers(page)) {
  568. struct buffer_head *head = page_buffers(page);
  569. struct buffer_head *bh = head;
  570. do {
  571. set_buffer_dirty(bh);
  572. bh = bh->b_this_page;
  573. } while (bh != head);
  574. }
  575. /*
  576. * Lock out page->mem_cgroup migration to keep PageDirty
  577. * synchronized with per-memcg dirty page counters.
  578. */
  579. lock_page_memcg(page);
  580. newly_dirty = !TestSetPageDirty(page);
  581. spin_unlock(&mapping->private_lock);
  582. if (newly_dirty)
  583. __set_page_dirty(page, mapping, 1);
  584. unlock_page_memcg(page);
  585. if (newly_dirty)
  586. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  587. return newly_dirty;
  588. }
  589. EXPORT_SYMBOL(__set_page_dirty_buffers);
  590. /*
  591. * Write out and wait upon a list of buffers.
  592. *
  593. * We have conflicting pressures: we want to make sure that all
  594. * initially dirty buffers get waited on, but that any subsequently
  595. * dirtied buffers don't. After all, we don't want fsync to last
  596. * forever if somebody is actively writing to the file.
  597. *
  598. * Do this in two main stages: first we copy dirty buffers to a
  599. * temporary inode list, queueing the writes as we go. Then we clean
  600. * up, waiting for those writes to complete.
  601. *
  602. * During this second stage, any subsequent updates to the file may end
  603. * up refiling the buffer on the original inode's dirty list again, so
  604. * there is a chance we will end up with a buffer queued for write but
  605. * not yet completed on that list. So, as a final cleanup we go through
  606. * the osync code to catch these locked, dirty buffers without requeuing
  607. * any newly dirty buffers for write.
  608. */
  609. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
  610. {
  611. struct buffer_head *bh;
  612. struct list_head tmp;
  613. struct address_space *mapping;
  614. int err = 0, err2;
  615. struct blk_plug plug;
  616. INIT_LIST_HEAD(&tmp);
  617. blk_start_plug(&plug);
  618. spin_lock(lock);
  619. while (!list_empty(list)) {
  620. bh = BH_ENTRY(list->next);
  621. mapping = bh->b_assoc_map;
  622. __remove_assoc_queue(bh);
  623. /* Avoid race with mark_buffer_dirty_inode() which does
  624. * a lockless check and we rely on seeing the dirty bit */
  625. smp_mb();
  626. if (buffer_dirty(bh) || buffer_locked(bh)) {
  627. list_add(&bh->b_assoc_buffers, &tmp);
  628. bh->b_assoc_map = mapping;
  629. if (buffer_dirty(bh)) {
  630. get_bh(bh);
  631. spin_unlock(lock);
  632. /*
  633. * Ensure any pending I/O completes so that
  634. * write_dirty_buffer() actually writes the
  635. * current contents - it is a noop if I/O is
  636. * still in flight on potentially older
  637. * contents.
  638. */
  639. write_dirty_buffer(bh, REQ_SYNC);
  640. /*
  641. * Kick off IO for the previous mapping. Note
  642. * that we will not run the very last mapping,
  643. * wait_on_buffer() will do that for us
  644. * through sync_buffer().
  645. */
  646. brelse(bh);
  647. spin_lock(lock);
  648. }
  649. }
  650. }
  651. spin_unlock(lock);
  652. blk_finish_plug(&plug);
  653. spin_lock(lock);
  654. while (!list_empty(&tmp)) {
  655. bh = BH_ENTRY(tmp.prev);
  656. get_bh(bh);
  657. mapping = bh->b_assoc_map;
  658. __remove_assoc_queue(bh);
  659. /* Avoid race with mark_buffer_dirty_inode() which does
  660. * a lockless check and we rely on seeing the dirty bit */
  661. smp_mb();
  662. if (buffer_dirty(bh)) {
  663. list_add(&bh->b_assoc_buffers,
  664. &mapping->private_list);
  665. bh->b_assoc_map = mapping;
  666. }
  667. spin_unlock(lock);
  668. wait_on_buffer(bh);
  669. if (!buffer_uptodate(bh))
  670. err = -EIO;
  671. brelse(bh);
  672. spin_lock(lock);
  673. }
  674. spin_unlock(lock);
  675. err2 = osync_buffers_list(lock, list);
  676. if (err)
  677. return err;
  678. else
  679. return err2;
  680. }
  681. /*
  682. * Invalidate any and all dirty buffers on a given inode. We are
  683. * probably unmounting the fs, but that doesn't mean we have already
  684. * done a sync(). Just drop the buffers from the inode list.
  685. *
  686. * NOTE: we take the inode's blockdev's mapping's private_lock. Which
  687. * assumes that all the buffers are against the blockdev. Not true
  688. * for reiserfs.
  689. */
  690. void invalidate_inode_buffers(struct inode *inode)
  691. {
  692. if (inode_has_buffers(inode)) {
  693. struct address_space *mapping = &inode->i_data;
  694. struct list_head *list = &mapping->private_list;
  695. struct address_space *buffer_mapping = mapping->private_data;
  696. spin_lock(&buffer_mapping->private_lock);
  697. while (!list_empty(list))
  698. __remove_assoc_queue(BH_ENTRY(list->next));
  699. spin_unlock(&buffer_mapping->private_lock);
  700. }
  701. }
  702. EXPORT_SYMBOL(invalidate_inode_buffers);
  703. /*
  704. * Remove any clean buffers from the inode's buffer list. This is called
  705. * when we're trying to free the inode itself. Those buffers can pin it.
  706. *
  707. * Returns true if all buffers were removed.
  708. */
  709. int remove_inode_buffers(struct inode *inode)
  710. {
  711. int ret = 1;
  712. if (inode_has_buffers(inode)) {
  713. struct address_space *mapping = &inode->i_data;
  714. struct list_head *list = &mapping->private_list;
  715. struct address_space *buffer_mapping = mapping->private_data;
  716. spin_lock(&buffer_mapping->private_lock);
  717. while (!list_empty(list)) {
  718. struct buffer_head *bh = BH_ENTRY(list->next);
  719. if (buffer_dirty(bh)) {
  720. ret = 0;
  721. break;
  722. }
  723. __remove_assoc_queue(bh);
  724. }
  725. spin_unlock(&buffer_mapping->private_lock);
  726. }
  727. return ret;
  728. }
  729. /*
  730. * Create the appropriate buffers when given a page for data area and
  731. * the size of each buffer.. Use the bh->b_this_page linked list to
  732. * follow the buffers created. Return NULL if unable to create more
  733. * buffers.
  734. *
  735. * The retry flag is used to differentiate async IO (paging, swapping)
  736. * which may not fail from ordinary buffer allocations.
  737. */
  738. struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
  739. bool retry)
  740. {
  741. struct buffer_head *bh, *head;
  742. gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
  743. long offset;
  744. struct mem_cgroup *memcg;
  745. if (retry)
  746. gfp |= __GFP_NOFAIL;
  747. memcg = get_mem_cgroup_from_page(page);
  748. memalloc_use_memcg(memcg);
  749. head = NULL;
  750. offset = PAGE_SIZE;
  751. while ((offset -= size) >= 0) {
  752. bh = alloc_buffer_head(gfp);
  753. if (!bh)
  754. goto no_grow;
  755. bh->b_this_page = head;
  756. bh->b_blocknr = -1;
  757. head = bh;
  758. bh->b_size = size;
  759. /* Link the buffer to its page */
  760. set_bh_page(bh, page, offset);
  761. }
  762. out:
  763. memalloc_unuse_memcg();
  764. mem_cgroup_put(memcg);
  765. return head;
  766. /*
  767. * In case anything failed, we just free everything we got.
  768. */
  769. no_grow:
  770. if (head) {
  771. do {
  772. bh = head;
  773. head = head->b_this_page;
  774. free_buffer_head(bh);
  775. } while (head);
  776. }
  777. goto out;
  778. }
  779. EXPORT_SYMBOL_GPL(alloc_page_buffers);
  780. static inline void
  781. link_dev_buffers(struct page *page, struct buffer_head *head)
  782. {
  783. struct buffer_head *bh, *tail;
  784. bh = head;
  785. do {
  786. tail = bh;
  787. bh = bh->b_this_page;
  788. } while (bh);
  789. tail->b_this_page = head;
  790. attach_page_buffers(page, head);
  791. }
  792. static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
  793. {
  794. sector_t retval = ~((sector_t)0);
  795. loff_t sz = i_size_read(bdev->bd_inode);
  796. if (sz) {
  797. unsigned int sizebits = blksize_bits(size);
  798. retval = (sz >> sizebits);
  799. }
  800. return retval;
  801. }
  802. /*
  803. * Initialise the state of a blockdev page's buffers.
  804. */
  805. static sector_t
  806. init_page_buffers(struct page *page, struct block_device *bdev,
  807. sector_t block, int size)
  808. {
  809. struct buffer_head *head = page_buffers(page);
  810. struct buffer_head *bh = head;
  811. int uptodate = PageUptodate(page);
  812. sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
  813. do {
  814. if (!buffer_mapped(bh)) {
  815. bh->b_end_io = NULL;
  816. bh->b_private = NULL;
  817. bh->b_bdev = bdev;
  818. bh->b_blocknr = block;
  819. if (uptodate)
  820. set_buffer_uptodate(bh);
  821. if (block < end_block)
  822. set_buffer_mapped(bh);
  823. }
  824. block++;
  825. bh = bh->b_this_page;
  826. } while (bh != head);
  827. /*
  828. * Caller needs to validate requested block against end of device.
  829. */
  830. return end_block;
  831. }
  832. /*
  833. * Create the page-cache page that contains the requested block.
  834. *
  835. * This is used purely for blockdev mappings.
  836. */
  837. static int
  838. grow_dev_page(struct block_device *bdev, sector_t block,
  839. pgoff_t index, int size, int sizebits, gfp_t gfp)
  840. {
  841. struct inode *inode = bdev->bd_inode;
  842. struct page *page;
  843. struct buffer_head *bh;
  844. sector_t end_block;
  845. int ret = 0; /* Will call free_more_memory() */
  846. gfp_t gfp_mask;
  847. gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
  848. /*
  849. * XXX: __getblk_slow() can not really deal with failure and
  850. * will endlessly loop on improvised global reclaim. Prefer
  851. * looping in the allocator rather than here, at least that
  852. * code knows what it's doing.
  853. */
  854. gfp_mask |= __GFP_NOFAIL;
  855. page = find_or_create_page(inode->i_mapping, index, gfp_mask);
  856. BUG_ON(!PageLocked(page));
  857. if (page_has_buffers(page)) {
  858. bh = page_buffers(page);
  859. if (bh->b_size == size) {
  860. end_block = init_page_buffers(page, bdev,
  861. (sector_t)index << sizebits,
  862. size);
  863. goto done;
  864. }
  865. if (!try_to_free_buffers(page))
  866. goto failed;
  867. }
  868. /*
  869. * Allocate some buffers for this page
  870. */
  871. bh = alloc_page_buffers(page, size, true);
  872. /*
  873. * Link the page to the buffers and initialise them. Take the
  874. * lock to be atomic wrt __find_get_block(), which does not
  875. * run under the page lock.
  876. */
  877. spin_lock(&inode->i_mapping->private_lock);
  878. link_dev_buffers(page, bh);
  879. end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
  880. size);
  881. spin_unlock(&inode->i_mapping->private_lock);
  882. done:
  883. ret = (block < end_block) ? 1 : -ENXIO;
  884. failed:
  885. unlock_page(page);
  886. put_page(page);
  887. return ret;
  888. }
  889. /*
  890. * Create buffers for the specified block device block's page. If
  891. * that page was dirty, the buffers are set dirty also.
  892. */
  893. static int
  894. grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
  895. {
  896. pgoff_t index;
  897. int sizebits;
  898. sizebits = -1;
  899. do {
  900. sizebits++;
  901. } while ((size << sizebits) < PAGE_SIZE);
  902. index = block >> sizebits;
  903. /*
  904. * Check for a block which wants to lie outside our maximum possible
  905. * pagecache index. (this comparison is done using sector_t types).
  906. */
  907. if (unlikely(index != block >> sizebits)) {
  908. printk(KERN_ERR "%s: requested out-of-range block %llu for "
  909. "device %pg\n",
  910. __func__, (unsigned long long)block,
  911. bdev);
  912. return -EIO;
  913. }
  914. /* Create a page with the proper size buffers.. */
  915. return grow_dev_page(bdev, block, index, size, sizebits, gfp);
  916. }
  917. static struct buffer_head *
  918. __getblk_slow(struct block_device *bdev, sector_t block,
  919. unsigned size, gfp_t gfp)
  920. {
  921. /* Size must be multiple of hard sectorsize */
  922. if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
  923. (size < 512 || size > PAGE_SIZE))) {
  924. printk(KERN_ERR "getblk(): invalid block size %d requested\n",
  925. size);
  926. printk(KERN_ERR "logical block size: %d\n",
  927. bdev_logical_block_size(bdev));
  928. dump_stack();
  929. return NULL;
  930. }
  931. for (;;) {
  932. struct buffer_head *bh;
  933. int ret;
  934. bh = __find_get_block(bdev, block, size);
  935. if (bh)
  936. return bh;
  937. ret = grow_buffers(bdev, block, size, gfp);
  938. if (ret < 0)
  939. return NULL;
  940. }
  941. }
  942. /*
  943. * The relationship between dirty buffers and dirty pages:
  944. *
  945. * Whenever a page has any dirty buffers, the page's dirty bit is set, and
  946. * the page is tagged dirty in its radix tree.
  947. *
  948. * At all times, the dirtiness of the buffers represents the dirtiness of
  949. * subsections of the page. If the page has buffers, the page dirty bit is
  950. * merely a hint about the true dirty state.
  951. *
  952. * When a page is set dirty in its entirety, all its buffers are marked dirty
  953. * (if the page has buffers).
  954. *
  955. * When a buffer is marked dirty, its page is dirtied, but the page's other
  956. * buffers are not.
  957. *
  958. * Also. When blockdev buffers are explicitly read with bread(), they
  959. * individually become uptodate. But their backing page remains not
  960. * uptodate - even if all of its buffers are uptodate. A subsequent
  961. * block_read_full_page() against that page will discover all the uptodate
  962. * buffers, will set the page uptodate and will perform no I/O.
  963. */
  964. /**
  965. * mark_buffer_dirty - mark a buffer_head as needing writeout
  966. * @bh: the buffer_head to mark dirty
  967. *
  968. * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
  969. * backing page dirty, then tag the page as dirty in its address_space's radix
  970. * tree and then attach the address_space's inode to its superblock's dirty
  971. * inode list.
  972. *
  973. * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
  974. * i_pages lock and mapping->host->i_lock.
  975. */
  976. void mark_buffer_dirty(struct buffer_head *bh)
  977. {
  978. WARN_ON_ONCE(!buffer_uptodate(bh));
  979. trace_block_dirty_buffer(bh);
  980. /*
  981. * Very *carefully* optimize the it-is-already-dirty case.
  982. *
  983. * Don't let the final "is it dirty" escape to before we
  984. * perhaps modified the buffer.
  985. */
  986. if (buffer_dirty(bh)) {
  987. smp_mb();
  988. if (buffer_dirty(bh))
  989. return;
  990. }
  991. if (!test_set_buffer_dirty(bh)) {
  992. struct page *page = bh->b_page;
  993. struct address_space *mapping = NULL;
  994. lock_page_memcg(page);
  995. if (!TestSetPageDirty(page)) {
  996. mapping = page_mapping(page);
  997. if (mapping)
  998. __set_page_dirty(page, mapping, 0);
  999. }
  1000. unlock_page_memcg(page);
  1001. if (mapping)
  1002. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1003. }
  1004. }
  1005. EXPORT_SYMBOL(mark_buffer_dirty);
  1006. void mark_buffer_write_io_error(struct buffer_head *bh)
  1007. {
  1008. set_buffer_write_io_error(bh);
  1009. /* FIXME: do we need to set this in both places? */
  1010. if (bh->b_page && bh->b_page->mapping)
  1011. mapping_set_error(bh->b_page->mapping, -EIO);
  1012. if (bh->b_assoc_map)
  1013. mapping_set_error(bh->b_assoc_map, -EIO);
  1014. }
  1015. EXPORT_SYMBOL(mark_buffer_write_io_error);
  1016. /*
  1017. * Decrement a buffer_head's reference count. If all buffers against a page
  1018. * have zero reference count, are clean and unlocked, and if the page is clean
  1019. * and unlocked then try_to_free_buffers() may strip the buffers from the page
  1020. * in preparation for freeing it (sometimes, rarely, buffers are removed from
  1021. * a page but it ends up not being freed, and buffers may later be reattached).
  1022. */
  1023. void __brelse(struct buffer_head * buf)
  1024. {
  1025. if (atomic_read(&buf->b_count)) {
  1026. put_bh(buf);
  1027. return;
  1028. }
  1029. WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
  1030. }
  1031. EXPORT_SYMBOL(__brelse);
  1032. /*
  1033. * bforget() is like brelse(), except it discards any
  1034. * potentially dirty data.
  1035. */
  1036. void __bforget(struct buffer_head *bh)
  1037. {
  1038. clear_buffer_dirty(bh);
  1039. if (bh->b_assoc_map) {
  1040. struct address_space *buffer_mapping = bh->b_page->mapping;
  1041. spin_lock(&buffer_mapping->private_lock);
  1042. list_del_init(&bh->b_assoc_buffers);
  1043. bh->b_assoc_map = NULL;
  1044. spin_unlock(&buffer_mapping->private_lock);
  1045. }
  1046. __brelse(bh);
  1047. }
  1048. EXPORT_SYMBOL(__bforget);
  1049. static struct buffer_head *__bread_slow(struct buffer_head *bh)
  1050. {
  1051. lock_buffer(bh);
  1052. if (buffer_uptodate(bh)) {
  1053. unlock_buffer(bh);
  1054. return bh;
  1055. } else {
  1056. get_bh(bh);
  1057. bh->b_end_io = end_buffer_read_sync;
  1058. submit_bh(REQ_OP_READ, 0, bh);
  1059. wait_on_buffer(bh);
  1060. if (buffer_uptodate(bh))
  1061. return bh;
  1062. }
  1063. brelse(bh);
  1064. return NULL;
  1065. }
  1066. /*
  1067. * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
  1068. * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
  1069. * refcount elevated by one when they're in an LRU. A buffer can only appear
  1070. * once in a particular CPU's LRU. A single buffer can be present in multiple
  1071. * CPU's LRUs at the same time.
  1072. *
  1073. * This is a transparent caching front-end to sb_bread(), sb_getblk() and
  1074. * sb_find_get_block().
  1075. *
  1076. * The LRUs themselves only need locking against invalidate_bh_lrus. We use
  1077. * a local interrupt disable for that.
  1078. */
  1079. #define BH_LRU_SIZE 16
  1080. struct bh_lru {
  1081. struct buffer_head *bhs[BH_LRU_SIZE];
  1082. };
  1083. static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
  1084. #ifdef CONFIG_SMP
  1085. #define bh_lru_lock() local_irq_disable()
  1086. #define bh_lru_unlock() local_irq_enable()
  1087. #else
  1088. #define bh_lru_lock() preempt_disable()
  1089. #define bh_lru_unlock() preempt_enable()
  1090. #endif
  1091. static inline void check_irqs_on(void)
  1092. {
  1093. #ifdef irqs_disabled
  1094. BUG_ON(irqs_disabled());
  1095. #endif
  1096. }
  1097. /*
  1098. * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is
  1099. * inserted at the front, and the buffer_head at the back if any is evicted.
  1100. * Or, if already in the LRU it is moved to the front.
  1101. */
  1102. static void bh_lru_install(struct buffer_head *bh)
  1103. {
  1104. struct buffer_head *evictee = bh;
  1105. struct bh_lru *b;
  1106. int i;
  1107. check_irqs_on();
  1108. bh_lru_lock();
  1109. b = this_cpu_ptr(&bh_lrus);
  1110. for (i = 0; i < BH_LRU_SIZE; i++) {
  1111. swap(evictee, b->bhs[i]);
  1112. if (evictee == bh) {
  1113. bh_lru_unlock();
  1114. return;
  1115. }
  1116. }
  1117. get_bh(bh);
  1118. bh_lru_unlock();
  1119. brelse(evictee);
  1120. }
  1121. /*
  1122. * Look up the bh in this cpu's LRU. If it's there, move it to the head.
  1123. */
  1124. static struct buffer_head *
  1125. lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
  1126. {
  1127. struct buffer_head *ret = NULL;
  1128. unsigned int i;
  1129. check_irqs_on();
  1130. bh_lru_lock();
  1131. for (i = 0; i < BH_LRU_SIZE; i++) {
  1132. struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
  1133. if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
  1134. bh->b_size == size) {
  1135. if (i) {
  1136. while (i) {
  1137. __this_cpu_write(bh_lrus.bhs[i],
  1138. __this_cpu_read(bh_lrus.bhs[i - 1]));
  1139. i--;
  1140. }
  1141. __this_cpu_write(bh_lrus.bhs[0], bh);
  1142. }
  1143. get_bh(bh);
  1144. ret = bh;
  1145. break;
  1146. }
  1147. }
  1148. bh_lru_unlock();
  1149. return ret;
  1150. }
  1151. /*
  1152. * Perform a pagecache lookup for the matching buffer. If it's there, refresh
  1153. * it in the LRU and mark it as accessed. If it is not present then return
  1154. * NULL
  1155. */
  1156. struct buffer_head *
  1157. __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
  1158. {
  1159. struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
  1160. if (bh == NULL) {
  1161. /* __find_get_block_slow will mark the page accessed */
  1162. bh = __find_get_block_slow(bdev, block);
  1163. if (bh)
  1164. bh_lru_install(bh);
  1165. } else
  1166. touch_buffer(bh);
  1167. return bh;
  1168. }
  1169. EXPORT_SYMBOL(__find_get_block);
  1170. /*
  1171. * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
  1172. * which corresponds to the passed block_device, block and size. The
  1173. * returned buffer has its reference count incremented.
  1174. *
  1175. * __getblk_gfp() will lock up the machine if grow_dev_page's
  1176. * try_to_free_buffers() attempt is failing. FIXME, perhaps?
  1177. */
  1178. struct buffer_head *
  1179. __getblk_gfp(struct block_device *bdev, sector_t block,
  1180. unsigned size, gfp_t gfp)
  1181. {
  1182. struct buffer_head *bh = __find_get_block(bdev, block, size);
  1183. might_sleep();
  1184. if (bh == NULL)
  1185. bh = __getblk_slow(bdev, block, size, gfp);
  1186. return bh;
  1187. }
  1188. EXPORT_SYMBOL(__getblk_gfp);
  1189. /*
  1190. * Do async read-ahead on a buffer..
  1191. */
  1192. void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
  1193. {
  1194. struct buffer_head *bh = __getblk(bdev, block, size);
  1195. if (likely(bh)) {
  1196. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
  1197. brelse(bh);
  1198. }
  1199. }
  1200. EXPORT_SYMBOL(__breadahead);
  1201. void __breadahead_gfp(struct block_device *bdev, sector_t block, unsigned size,
  1202. gfp_t gfp)
  1203. {
  1204. struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
  1205. if (likely(bh)) {
  1206. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
  1207. brelse(bh);
  1208. }
  1209. }
  1210. EXPORT_SYMBOL(__breadahead_gfp);
  1211. /**
  1212. * __bread_gfp() - reads a specified block and returns the bh
  1213. * @bdev: the block_device to read from
  1214. * @block: number of block
  1215. * @size: size (in bytes) to read
  1216. * @gfp: page allocation flag
  1217. *
  1218. * Reads a specified block, and returns buffer head that contains it.
  1219. * The page cache can be allocated from non-movable area
  1220. * not to prevent page migration if you set gfp to zero.
  1221. * It returns NULL if the block was unreadable.
  1222. */
  1223. struct buffer_head *
  1224. __bread_gfp(struct block_device *bdev, sector_t block,
  1225. unsigned size, gfp_t gfp)
  1226. {
  1227. struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
  1228. if (likely(bh) && !buffer_uptodate(bh))
  1229. bh = __bread_slow(bh);
  1230. return bh;
  1231. }
  1232. EXPORT_SYMBOL(__bread_gfp);
  1233. /*
  1234. * invalidate_bh_lrus() is called rarely - but not only at unmount.
  1235. * This doesn't race because it runs in each cpu either in irq
  1236. * or with preempt disabled.
  1237. */
  1238. static void invalidate_bh_lru(void *arg)
  1239. {
  1240. struct bh_lru *b = &get_cpu_var(bh_lrus);
  1241. int i;
  1242. for (i = 0; i < BH_LRU_SIZE; i++) {
  1243. brelse(b->bhs[i]);
  1244. b->bhs[i] = NULL;
  1245. }
  1246. put_cpu_var(bh_lrus);
  1247. }
  1248. static bool has_bh_in_lru(int cpu, void *dummy)
  1249. {
  1250. struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
  1251. int i;
  1252. for (i = 0; i < BH_LRU_SIZE; i++) {
  1253. if (b->bhs[i])
  1254. return 1;
  1255. }
  1256. return 0;
  1257. }
  1258. void invalidate_bh_lrus(void)
  1259. {
  1260. on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
  1261. }
  1262. EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
  1263. void set_bh_page(struct buffer_head *bh,
  1264. struct page *page, unsigned long offset)
  1265. {
  1266. bh->b_page = page;
  1267. BUG_ON(offset >= PAGE_SIZE);
  1268. if (PageHighMem(page))
  1269. /*
  1270. * This catches illegal uses and preserves the offset:
  1271. */
  1272. bh->b_data = (char *)(0 + offset);
  1273. else
  1274. bh->b_data = page_address(page) + offset;
  1275. }
  1276. EXPORT_SYMBOL(set_bh_page);
  1277. /*
  1278. * Called when truncating a buffer on a page completely.
  1279. */
  1280. /* Bits that are cleared during an invalidate */
  1281. #define BUFFER_FLAGS_DISCARD \
  1282. (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
  1283. 1 << BH_Delay | 1 << BH_Unwritten)
  1284. static void discard_buffer(struct buffer_head * bh)
  1285. {
  1286. unsigned long b_state, b_state_old;
  1287. lock_buffer(bh);
  1288. clear_buffer_dirty(bh);
  1289. bh->b_bdev = NULL;
  1290. b_state = bh->b_state;
  1291. for (;;) {
  1292. b_state_old = cmpxchg(&bh->b_state, b_state,
  1293. (b_state & ~BUFFER_FLAGS_DISCARD));
  1294. if (b_state_old == b_state)
  1295. break;
  1296. b_state = b_state_old;
  1297. }
  1298. unlock_buffer(bh);
  1299. }
  1300. /**
  1301. * block_invalidatepage - invalidate part or all of a buffer-backed page
  1302. *
  1303. * @page: the page which is affected
  1304. * @offset: start of the range to invalidate
  1305. * @length: length of the range to invalidate
  1306. *
  1307. * block_invalidatepage() is called when all or part of the page has become
  1308. * invalidated by a truncate operation.
  1309. *
  1310. * block_invalidatepage() does not have to release all buffers, but it must
  1311. * ensure that no dirty buffer is left outside @offset and that no I/O
  1312. * is underway against any of the blocks which are outside the truncation
  1313. * point. Because the caller is about to free (and possibly reuse) those
  1314. * blocks on-disk.
  1315. */
  1316. void block_invalidatepage(struct page *page, unsigned int offset,
  1317. unsigned int length)
  1318. {
  1319. struct buffer_head *head, *bh, *next;
  1320. unsigned int curr_off = 0;
  1321. unsigned int stop = length + offset;
  1322. BUG_ON(!PageLocked(page));
  1323. if (!page_has_buffers(page))
  1324. goto out;
  1325. /*
  1326. * Check for overflow
  1327. */
  1328. BUG_ON(stop > PAGE_SIZE || stop < length);
  1329. head = page_buffers(page);
  1330. bh = head;
  1331. do {
  1332. unsigned int next_off = curr_off + bh->b_size;
  1333. next = bh->b_this_page;
  1334. /*
  1335. * Are we still fully in range ?
  1336. */
  1337. if (next_off > stop)
  1338. goto out;
  1339. /*
  1340. * is this block fully invalidated?
  1341. */
  1342. if (offset <= curr_off)
  1343. discard_buffer(bh);
  1344. curr_off = next_off;
  1345. bh = next;
  1346. } while (bh != head);
  1347. /*
  1348. * We release buffers only if the entire page is being invalidated.
  1349. * The get_block cached value has been unconditionally invalidated,
  1350. * so real IO is not possible anymore.
  1351. */
  1352. if (length == PAGE_SIZE)
  1353. try_to_release_page(page, 0);
  1354. out:
  1355. return;
  1356. }
  1357. EXPORT_SYMBOL(block_invalidatepage);
  1358. /*
  1359. * We attach and possibly dirty the buffers atomically wrt
  1360. * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
  1361. * is already excluded via the page lock.
  1362. */
  1363. void create_empty_buffers(struct page *page,
  1364. unsigned long blocksize, unsigned long b_state)
  1365. {
  1366. struct buffer_head *bh, *head, *tail;
  1367. head = alloc_page_buffers(page, blocksize, true);
  1368. bh = head;
  1369. do {
  1370. bh->b_state |= b_state;
  1371. tail = bh;
  1372. bh = bh->b_this_page;
  1373. } while (bh);
  1374. tail->b_this_page = head;
  1375. spin_lock(&page->mapping->private_lock);
  1376. if (PageUptodate(page) || PageDirty(page)) {
  1377. bh = head;
  1378. do {
  1379. if (PageDirty(page))
  1380. set_buffer_dirty(bh);
  1381. if (PageUptodate(page))
  1382. set_buffer_uptodate(bh);
  1383. bh = bh->b_this_page;
  1384. } while (bh != head);
  1385. }
  1386. attach_page_buffers(page, head);
  1387. spin_unlock(&page->mapping->private_lock);
  1388. }
  1389. EXPORT_SYMBOL(create_empty_buffers);
  1390. /**
  1391. * clean_bdev_aliases: clean a range of buffers in block device
  1392. * @bdev: Block device to clean buffers in
  1393. * @block: Start of a range of blocks to clean
  1394. * @len: Number of blocks to clean
  1395. *
  1396. * We are taking a range of blocks for data and we don't want writeback of any
  1397. * buffer-cache aliases starting from return from this function and until the
  1398. * moment when something will explicitly mark the buffer dirty (hopefully that
  1399. * will not happen until we will free that block ;-) We don't even need to mark
  1400. * it not-uptodate - nobody can expect anything from a newly allocated buffer
  1401. * anyway. We used to use unmap_buffer() for such invalidation, but that was
  1402. * wrong. We definitely don't want to mark the alias unmapped, for example - it
  1403. * would confuse anyone who might pick it with bread() afterwards...
  1404. *
  1405. * Also.. Note that bforget() doesn't lock the buffer. So there can be
  1406. * writeout I/O going on against recently-freed buffers. We don't wait on that
  1407. * I/O in bforget() - it's more efficient to wait on the I/O only if we really
  1408. * need to. That happens here.
  1409. */
  1410. void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
  1411. {
  1412. struct inode *bd_inode = bdev->bd_inode;
  1413. struct address_space *bd_mapping = bd_inode->i_mapping;
  1414. struct pagevec pvec;
  1415. pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1416. pgoff_t end;
  1417. int i, count;
  1418. struct buffer_head *bh;
  1419. struct buffer_head *head;
  1420. end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1421. pagevec_init(&pvec);
  1422. while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) {
  1423. count = pagevec_count(&pvec);
  1424. for (i = 0; i < count; i++) {
  1425. struct page *page = pvec.pages[i];
  1426. if (!page_has_buffers(page))
  1427. continue;
  1428. /*
  1429. * We use page lock instead of bd_mapping->private_lock
  1430. * to pin buffers here since we can afford to sleep and
  1431. * it scales better than a global spinlock lock.
  1432. */
  1433. lock_page(page);
  1434. /* Recheck when the page is locked which pins bhs */
  1435. if (!page_has_buffers(page))
  1436. goto unlock_page;
  1437. head = page_buffers(page);
  1438. bh = head;
  1439. do {
  1440. if (!buffer_mapped(bh) || (bh->b_blocknr < block))
  1441. goto next;
  1442. if (bh->b_blocknr >= block + len)
  1443. break;
  1444. clear_buffer_dirty(bh);
  1445. wait_on_buffer(bh);
  1446. clear_buffer_req(bh);
  1447. next:
  1448. bh = bh->b_this_page;
  1449. } while (bh != head);
  1450. unlock_page:
  1451. unlock_page(page);
  1452. }
  1453. pagevec_release(&pvec);
  1454. cond_resched();
  1455. /* End of range already reached? */
  1456. if (index > end || !index)
  1457. break;
  1458. }
  1459. }
  1460. EXPORT_SYMBOL(clean_bdev_aliases);
  1461. /*
  1462. * Size is a power-of-two in the range 512..PAGE_SIZE,
  1463. * and the case we care about most is PAGE_SIZE.
  1464. *
  1465. * So this *could* possibly be written with those
  1466. * constraints in mind (relevant mostly if some
  1467. * architecture has a slow bit-scan instruction)
  1468. */
  1469. static inline int block_size_bits(unsigned int blocksize)
  1470. {
  1471. return ilog2(blocksize);
  1472. }
  1473. static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
  1474. {
  1475. BUG_ON(!PageLocked(page));
  1476. if (!page_has_buffers(page))
  1477. create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits),
  1478. b_state);
  1479. return page_buffers(page);
  1480. }
  1481. /*
  1482. * NOTE! All mapped/uptodate combinations are valid:
  1483. *
  1484. * Mapped Uptodate Meaning
  1485. *
  1486. * No No "unknown" - must do get_block()
  1487. * No Yes "hole" - zero-filled
  1488. * Yes No "allocated" - allocated on disk, not read in
  1489. * Yes Yes "valid" - allocated and up-to-date in memory.
  1490. *
  1491. * "Dirty" is valid only with the last case (mapped+uptodate).
  1492. */
  1493. /*
  1494. * While block_write_full_page is writing back the dirty buffers under
  1495. * the page lock, whoever dirtied the buffers may decide to clean them
  1496. * again at any time. We handle that by only looking at the buffer
  1497. * state inside lock_buffer().
  1498. *
  1499. * If block_write_full_page() is called for regular writeback
  1500. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1501. * locked buffer. This only can happen if someone has written the buffer
  1502. * directly, with submit_bh(). At the address_space level PageWriteback
  1503. * prevents this contention from occurring.
  1504. *
  1505. * If block_write_full_page() is called with wbc->sync_mode ==
  1506. * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
  1507. * causes the writes to be flagged as synchronous writes.
  1508. */
  1509. int __block_write_full_page(struct inode *inode, struct page *page,
  1510. get_block_t *get_block, struct writeback_control *wbc,
  1511. bh_end_io_t *handler)
  1512. {
  1513. int err;
  1514. sector_t block;
  1515. sector_t last_block;
  1516. struct buffer_head *bh, *head;
  1517. unsigned int blocksize, bbits;
  1518. int nr_underway = 0;
  1519. int write_flags = wbc_to_write_flags(wbc);
  1520. head = create_page_buffers(page, inode,
  1521. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1522. /*
  1523. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1524. * here, and the (potentially unmapped) buffers may become dirty at
  1525. * any time. If a buffer becomes dirty here after we've inspected it
  1526. * then we just miss that fact, and the page stays dirty.
  1527. *
  1528. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1529. * handle that here by just cleaning them.
  1530. */
  1531. bh = head;
  1532. blocksize = bh->b_size;
  1533. bbits = block_size_bits(blocksize);
  1534. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1535. last_block = (i_size_read(inode) - 1) >> bbits;
  1536. /*
  1537. * Get all the dirty buffers mapped to disk addresses and
  1538. * handle any aliases from the underlying blockdev's mapping.
  1539. */
  1540. do {
  1541. if (block > last_block) {
  1542. /*
  1543. * mapped buffers outside i_size will occur, because
  1544. * this page can be outside i_size when there is a
  1545. * truncate in progress.
  1546. */
  1547. /*
  1548. * The buffer was zeroed by block_write_full_page()
  1549. */
  1550. clear_buffer_dirty(bh);
  1551. set_buffer_uptodate(bh);
  1552. } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
  1553. buffer_dirty(bh)) {
  1554. WARN_ON(bh->b_size != blocksize);
  1555. err = get_block(inode, block, bh, 1);
  1556. if (err)
  1557. goto recover;
  1558. clear_buffer_delay(bh);
  1559. if (buffer_new(bh)) {
  1560. /* blockdev mappings never come here */
  1561. clear_buffer_new(bh);
  1562. clean_bdev_bh_alias(bh);
  1563. }
  1564. }
  1565. bh = bh->b_this_page;
  1566. block++;
  1567. } while (bh != head);
  1568. do {
  1569. if (!buffer_mapped(bh))
  1570. continue;
  1571. /*
  1572. * If it's a fully non-blocking write attempt and we cannot
  1573. * lock the buffer then redirty the page. Note that this can
  1574. * potentially cause a busy-wait loop from writeback threads
  1575. * and kswapd activity, but those code paths have their own
  1576. * higher-level throttling.
  1577. */
  1578. if (wbc->sync_mode != WB_SYNC_NONE) {
  1579. lock_buffer(bh);
  1580. } else if (!trylock_buffer(bh)) {
  1581. redirty_page_for_writepage(wbc, page);
  1582. continue;
  1583. }
  1584. if (test_clear_buffer_dirty(bh)) {
  1585. mark_buffer_async_write_endio(bh, handler);
  1586. } else {
  1587. unlock_buffer(bh);
  1588. }
  1589. } while ((bh = bh->b_this_page) != head);
  1590. /*
  1591. * The page and its buffers are protected by PageWriteback(), so we can
  1592. * drop the bh refcounts early.
  1593. */
  1594. BUG_ON(PageWriteback(page));
  1595. set_page_writeback(page);
  1596. do {
  1597. struct buffer_head *next = bh->b_this_page;
  1598. if (buffer_async_write(bh)) {
  1599. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1600. inode->i_write_hint, wbc);
  1601. nr_underway++;
  1602. }
  1603. bh = next;
  1604. } while (bh != head);
  1605. unlock_page(page);
  1606. err = 0;
  1607. done:
  1608. if (nr_underway == 0) {
  1609. /*
  1610. * The page was marked dirty, but the buffers were
  1611. * clean. Someone wrote them back by hand with
  1612. * ll_rw_block/submit_bh. A rare case.
  1613. */
  1614. end_page_writeback(page);
  1615. /*
  1616. * The page and buffer_heads can be released at any time from
  1617. * here on.
  1618. */
  1619. }
  1620. return err;
  1621. recover:
  1622. /*
  1623. * ENOSPC, or some other error. We may already have added some
  1624. * blocks to the file, so we need to write these out to avoid
  1625. * exposing stale data.
  1626. * The page is currently locked and not marked for writeback
  1627. */
  1628. bh = head;
  1629. /* Recovery: lock and submit the mapped buffers */
  1630. do {
  1631. if (buffer_mapped(bh) && buffer_dirty(bh) &&
  1632. !buffer_delay(bh)) {
  1633. lock_buffer(bh);
  1634. mark_buffer_async_write_endio(bh, handler);
  1635. } else {
  1636. /*
  1637. * The buffer may have been set dirty during
  1638. * attachment to a dirty page.
  1639. */
  1640. clear_buffer_dirty(bh);
  1641. }
  1642. } while ((bh = bh->b_this_page) != head);
  1643. SetPageError(page);
  1644. BUG_ON(PageWriteback(page));
  1645. mapping_set_error(page->mapping, err);
  1646. set_page_writeback(page);
  1647. do {
  1648. struct buffer_head *next = bh->b_this_page;
  1649. if (buffer_async_write(bh)) {
  1650. clear_buffer_dirty(bh);
  1651. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1652. inode->i_write_hint, wbc);
  1653. nr_underway++;
  1654. }
  1655. bh = next;
  1656. } while (bh != head);
  1657. unlock_page(page);
  1658. goto done;
  1659. }
  1660. EXPORT_SYMBOL(__block_write_full_page);
  1661. /*
  1662. * If a page has any new buffers, zero them out here, and mark them uptodate
  1663. * and dirty so they'll be written out (in order to prevent uninitialised
  1664. * block data from leaking). And clear the new bit.
  1665. */
  1666. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1667. {
  1668. unsigned int block_start, block_end;
  1669. struct buffer_head *head, *bh;
  1670. BUG_ON(!PageLocked(page));
  1671. if (!page_has_buffers(page))
  1672. return;
  1673. bh = head = page_buffers(page);
  1674. block_start = 0;
  1675. do {
  1676. block_end = block_start + bh->b_size;
  1677. if (buffer_new(bh)) {
  1678. if (block_end > from && block_start < to) {
  1679. if (!PageUptodate(page)) {
  1680. unsigned start, size;
  1681. start = max(from, block_start);
  1682. size = min(to, block_end) - start;
  1683. zero_user(page, start, size);
  1684. set_buffer_uptodate(bh);
  1685. }
  1686. clear_buffer_new(bh);
  1687. mark_buffer_dirty(bh);
  1688. }
  1689. }
  1690. block_start = block_end;
  1691. bh = bh->b_this_page;
  1692. } while (bh != head);
  1693. }
  1694. EXPORT_SYMBOL(page_zero_new_buffers);
  1695. static void
  1696. iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
  1697. struct iomap *iomap)
  1698. {
  1699. loff_t offset = block << inode->i_blkbits;
  1700. bh->b_bdev = iomap->bdev;
  1701. /*
  1702. * Block points to offset in file we need to map, iomap contains
  1703. * the offset at which the map starts. If the map ends before the
  1704. * current block, then do not map the buffer and let the caller
  1705. * handle it.
  1706. */
  1707. BUG_ON(offset >= iomap->offset + iomap->length);
  1708. switch (iomap->type) {
  1709. case IOMAP_HOLE:
  1710. /*
  1711. * If the buffer is not up to date or beyond the current EOF,
  1712. * we need to mark it as new to ensure sub-block zeroing is
  1713. * executed if necessary.
  1714. */
  1715. if (!buffer_uptodate(bh) ||
  1716. (offset >= i_size_read(inode)))
  1717. set_buffer_new(bh);
  1718. break;
  1719. case IOMAP_DELALLOC:
  1720. if (!buffer_uptodate(bh) ||
  1721. (offset >= i_size_read(inode)))
  1722. set_buffer_new(bh);
  1723. set_buffer_uptodate(bh);
  1724. set_buffer_mapped(bh);
  1725. set_buffer_delay(bh);
  1726. break;
  1727. case IOMAP_UNWRITTEN:
  1728. /*
  1729. * For unwritten regions, we always need to ensure that regions
  1730. * in the block we are not writing to are zeroed. Mark the
  1731. * buffer as new to ensure this.
  1732. */
  1733. set_buffer_new(bh);
  1734. set_buffer_unwritten(bh);
  1735. /* FALLTHRU */
  1736. case IOMAP_MAPPED:
  1737. if ((iomap->flags & IOMAP_F_NEW) ||
  1738. offset >= i_size_read(inode))
  1739. set_buffer_new(bh);
  1740. bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
  1741. inode->i_blkbits;
  1742. set_buffer_mapped(bh);
  1743. break;
  1744. }
  1745. }
  1746. int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
  1747. get_block_t *get_block, struct iomap *iomap)
  1748. {
  1749. unsigned from = pos & (PAGE_SIZE - 1);
  1750. unsigned to = from + len;
  1751. struct inode *inode = page->mapping->host;
  1752. unsigned block_start, block_end;
  1753. sector_t block;
  1754. int err = 0;
  1755. unsigned blocksize, bbits;
  1756. struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
  1757. BUG_ON(!PageLocked(page));
  1758. BUG_ON(from > PAGE_SIZE);
  1759. BUG_ON(to > PAGE_SIZE);
  1760. BUG_ON(from > to);
  1761. head = create_page_buffers(page, inode, 0);
  1762. blocksize = head->b_size;
  1763. bbits = block_size_bits(blocksize);
  1764. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1765. for(bh = head, block_start = 0; bh != head || !block_start;
  1766. block++, block_start=block_end, bh = bh->b_this_page) {
  1767. block_end = block_start + blocksize;
  1768. if (block_end <= from || block_start >= to) {
  1769. if (PageUptodate(page)) {
  1770. if (!buffer_uptodate(bh))
  1771. set_buffer_uptodate(bh);
  1772. }
  1773. continue;
  1774. }
  1775. if (buffer_new(bh))
  1776. clear_buffer_new(bh);
  1777. if (!buffer_mapped(bh)) {
  1778. WARN_ON(bh->b_size != blocksize);
  1779. if (get_block) {
  1780. err = get_block(inode, block, bh, 1);
  1781. if (err)
  1782. break;
  1783. } else {
  1784. iomap_to_bh(inode, block, bh, iomap);
  1785. }
  1786. if (buffer_new(bh)) {
  1787. clean_bdev_bh_alias(bh);
  1788. if (PageUptodate(page)) {
  1789. clear_buffer_new(bh);
  1790. set_buffer_uptodate(bh);
  1791. mark_buffer_dirty(bh);
  1792. continue;
  1793. }
  1794. if (block_end > to || block_start < from)
  1795. zero_user_segments(page,
  1796. to, block_end,
  1797. block_start, from);
  1798. continue;
  1799. }
  1800. }
  1801. if (PageUptodate(page)) {
  1802. if (!buffer_uptodate(bh))
  1803. set_buffer_uptodate(bh);
  1804. continue;
  1805. }
  1806. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1807. !buffer_unwritten(bh) &&
  1808. (block_start < from || block_end > to)) {
  1809. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1810. *wait_bh++=bh;
  1811. }
  1812. }
  1813. /*
  1814. * If we issued read requests - let them complete.
  1815. */
  1816. while(wait_bh > wait) {
  1817. wait_on_buffer(*--wait_bh);
  1818. if (!buffer_uptodate(*wait_bh))
  1819. err = -EIO;
  1820. }
  1821. if (unlikely(err))
  1822. page_zero_new_buffers(page, from, to);
  1823. return err;
  1824. }
  1825. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  1826. get_block_t *get_block)
  1827. {
  1828. return __block_write_begin_int(page, pos, len, get_block, NULL);
  1829. }
  1830. EXPORT_SYMBOL(__block_write_begin);
  1831. static int __block_commit_write(struct inode *inode, struct page *page,
  1832. unsigned from, unsigned to)
  1833. {
  1834. unsigned block_start, block_end;
  1835. int partial = 0;
  1836. unsigned blocksize;
  1837. struct buffer_head *bh, *head;
  1838. bh = head = page_buffers(page);
  1839. blocksize = bh->b_size;
  1840. block_start = 0;
  1841. do {
  1842. block_end = block_start + blocksize;
  1843. if (block_end <= from || block_start >= to) {
  1844. if (!buffer_uptodate(bh))
  1845. partial = 1;
  1846. } else {
  1847. set_buffer_uptodate(bh);
  1848. mark_buffer_dirty(bh);
  1849. }
  1850. clear_buffer_new(bh);
  1851. block_start = block_end;
  1852. bh = bh->b_this_page;
  1853. } while (bh != head);
  1854. /*
  1855. * If this is a partial write which happened to make all buffers
  1856. * uptodate then we can optimize away a bogus readpage() for
  1857. * the next read(). Here we 'discover' whether the page went
  1858. * uptodate as a result of this (potentially partial) write.
  1859. */
  1860. if (!partial)
  1861. SetPageUptodate(page);
  1862. return 0;
  1863. }
  1864. /*
  1865. * block_write_begin takes care of the basic task of block allocation and
  1866. * bringing partial write blocks uptodate first.
  1867. *
  1868. * The filesystem needs to handle block truncation upon failure.
  1869. */
  1870. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  1871. unsigned flags, struct page **pagep, get_block_t *get_block)
  1872. {
  1873. pgoff_t index = pos >> PAGE_SHIFT;
  1874. struct page *page;
  1875. int status;
  1876. page = grab_cache_page_write_begin(mapping, index, flags);
  1877. if (!page)
  1878. return -ENOMEM;
  1879. status = __block_write_begin(page, pos, len, get_block);
  1880. if (unlikely(status)) {
  1881. unlock_page(page);
  1882. put_page(page);
  1883. page = NULL;
  1884. }
  1885. *pagep = page;
  1886. return status;
  1887. }
  1888. EXPORT_SYMBOL(block_write_begin);
  1889. int __generic_write_end(struct inode *inode, loff_t pos, unsigned copied,
  1890. struct page *page)
  1891. {
  1892. loff_t old_size = inode->i_size;
  1893. bool i_size_changed = false;
  1894. /*
  1895. * No need to use i_size_read() here, the i_size cannot change under us
  1896. * because we hold i_rwsem.
  1897. *
  1898. * But it's important to update i_size while still holding page lock:
  1899. * page writeout could otherwise come in and zero beyond i_size.
  1900. */
  1901. if (pos + copied > inode->i_size) {
  1902. i_size_write(inode, pos + copied);
  1903. i_size_changed = true;
  1904. }
  1905. unlock_page(page);
  1906. put_page(page);
  1907. if (old_size < pos)
  1908. pagecache_isize_extended(inode, old_size, pos);
  1909. /*
  1910. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1911. * makes the holding time of page lock longer. Second, it forces lock
  1912. * ordering of page lock and transaction start for journaling
  1913. * filesystems.
  1914. */
  1915. if (i_size_changed)
  1916. mark_inode_dirty(inode);
  1917. return copied;
  1918. }
  1919. int block_write_end(struct file *file, struct address_space *mapping,
  1920. loff_t pos, unsigned len, unsigned copied,
  1921. struct page *page, void *fsdata)
  1922. {
  1923. struct inode *inode = mapping->host;
  1924. unsigned start;
  1925. start = pos & (PAGE_SIZE - 1);
  1926. if (unlikely(copied < len)) {
  1927. /*
  1928. * The buffers that were written will now be uptodate, so we
  1929. * don't have to worry about a readpage reading them and
  1930. * overwriting a partial write. However if we have encountered
  1931. * a short write and only partially written into a buffer, it
  1932. * will not be marked uptodate, so a readpage might come in and
  1933. * destroy our partial write.
  1934. *
  1935. * Do the simplest thing, and just treat any short write to a
  1936. * non uptodate page as a zero-length write, and force the
  1937. * caller to redo the whole thing.
  1938. */
  1939. if (!PageUptodate(page))
  1940. copied = 0;
  1941. page_zero_new_buffers(page, start+copied, start+len);
  1942. }
  1943. flush_dcache_page(page);
  1944. /* This could be a short (even 0-length) commit */
  1945. __block_commit_write(inode, page, start, start+copied);
  1946. return copied;
  1947. }
  1948. EXPORT_SYMBOL(block_write_end);
  1949. int generic_write_end(struct file *file, struct address_space *mapping,
  1950. loff_t pos, unsigned len, unsigned copied,
  1951. struct page *page, void *fsdata)
  1952. {
  1953. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1954. return __generic_write_end(mapping->host, pos, copied, page);
  1955. }
  1956. EXPORT_SYMBOL(generic_write_end);
  1957. /*
  1958. * block_is_partially_uptodate checks whether buffers within a page are
  1959. * uptodate or not.
  1960. *
  1961. * Returns true if all buffers which correspond to a file portion
  1962. * we want to read are uptodate.
  1963. */
  1964. int block_is_partially_uptodate(struct page *page, unsigned long from,
  1965. unsigned long count)
  1966. {
  1967. unsigned block_start, block_end, blocksize;
  1968. unsigned to;
  1969. struct buffer_head *bh, *head;
  1970. int ret = 1;
  1971. if (!page_has_buffers(page))
  1972. return 0;
  1973. head = page_buffers(page);
  1974. blocksize = head->b_size;
  1975. to = min_t(unsigned, PAGE_SIZE - from, count);
  1976. to = from + to;
  1977. if (from < blocksize && to > PAGE_SIZE - blocksize)
  1978. return 0;
  1979. bh = head;
  1980. block_start = 0;
  1981. do {
  1982. block_end = block_start + blocksize;
  1983. if (block_end > from && block_start < to) {
  1984. if (!buffer_uptodate(bh)) {
  1985. ret = 0;
  1986. break;
  1987. }
  1988. if (block_end >= to)
  1989. break;
  1990. }
  1991. block_start = block_end;
  1992. bh = bh->b_this_page;
  1993. } while (bh != head);
  1994. return ret;
  1995. }
  1996. EXPORT_SYMBOL(block_is_partially_uptodate);
  1997. /*
  1998. * Generic "read page" function for block devices that have the normal
  1999. * get_block functionality. This is most of the block device filesystems.
  2000. * Reads the page asynchronously --- the unlock_buffer() and
  2001. * set/clear_buffer_uptodate() functions propagate buffer state into the
  2002. * page struct once IO has completed.
  2003. */
  2004. int block_read_full_page(struct page *page, get_block_t *get_block)
  2005. {
  2006. struct inode *inode = page->mapping->host;
  2007. sector_t iblock, lblock;
  2008. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  2009. unsigned int blocksize, bbits;
  2010. int nr, i;
  2011. int fully_mapped = 1;
  2012. head = create_page_buffers(page, inode, 0);
  2013. blocksize = head->b_size;
  2014. bbits = block_size_bits(blocksize);
  2015. iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
  2016. lblock = (i_size_read(inode)+blocksize-1) >> bbits;
  2017. bh = head;
  2018. nr = 0;
  2019. i = 0;
  2020. do {
  2021. if (buffer_uptodate(bh))
  2022. continue;
  2023. if (!buffer_mapped(bh)) {
  2024. int err = 0;
  2025. fully_mapped = 0;
  2026. if (iblock < lblock) {
  2027. WARN_ON(bh->b_size != blocksize);
  2028. err = get_block(inode, iblock, bh, 0);
  2029. if (err)
  2030. SetPageError(page);
  2031. }
  2032. if (!buffer_mapped(bh)) {
  2033. zero_user(page, i * blocksize, blocksize);
  2034. if (!err)
  2035. set_buffer_uptodate(bh);
  2036. continue;
  2037. }
  2038. /*
  2039. * get_block() might have updated the buffer
  2040. * synchronously
  2041. */
  2042. if (buffer_uptodate(bh))
  2043. continue;
  2044. }
  2045. arr[nr++] = bh;
  2046. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  2047. if (fully_mapped)
  2048. SetPageMappedToDisk(page);
  2049. if (!nr) {
  2050. /*
  2051. * All buffers are uptodate - we can set the page uptodate
  2052. * as well. But not if get_block() returned an error.
  2053. */
  2054. if (!PageError(page))
  2055. SetPageUptodate(page);
  2056. unlock_page(page);
  2057. return 0;
  2058. }
  2059. /* Stage two: lock the buffers */
  2060. for (i = 0; i < nr; i++) {
  2061. bh = arr[i];
  2062. lock_buffer(bh);
  2063. mark_buffer_async_read(bh);
  2064. }
  2065. /*
  2066. * Stage 3: start the IO. Check for uptodateness
  2067. * inside the buffer lock in case another process reading
  2068. * the underlying blockdev brought it uptodate (the sct fix).
  2069. */
  2070. for (i = 0; i < nr; i++) {
  2071. bh = arr[i];
  2072. if (buffer_uptodate(bh))
  2073. end_buffer_async_read(bh, 1);
  2074. else
  2075. submit_bh(REQ_OP_READ, 0, bh);
  2076. }
  2077. return 0;
  2078. }
  2079. EXPORT_SYMBOL(block_read_full_page);
  2080. /* utility function for filesystems that need to do work on expanding
  2081. * truncates. Uses filesystem pagecache writes to allow the filesystem to
  2082. * deal with the hole.
  2083. */
  2084. int generic_cont_expand_simple(struct inode *inode, loff_t size)
  2085. {
  2086. struct address_space *mapping = inode->i_mapping;
  2087. struct page *page;
  2088. void *fsdata;
  2089. int err;
  2090. err = inode_newsize_ok(inode, size);
  2091. if (err)
  2092. goto out;
  2093. err = pagecache_write_begin(NULL, mapping, size, 0,
  2094. AOP_FLAG_CONT_EXPAND, &page, &fsdata);
  2095. if (err)
  2096. goto out;
  2097. err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  2098. BUG_ON(err > 0);
  2099. out:
  2100. return err;
  2101. }
  2102. EXPORT_SYMBOL(generic_cont_expand_simple);
  2103. static int cont_expand_zero(struct file *file, struct address_space *mapping,
  2104. loff_t pos, loff_t *bytes)
  2105. {
  2106. struct inode *inode = mapping->host;
  2107. unsigned int blocksize = i_blocksize(inode);
  2108. struct page *page;
  2109. void *fsdata;
  2110. pgoff_t index, curidx;
  2111. loff_t curpos;
  2112. unsigned zerofrom, offset, len;
  2113. int err = 0;
  2114. index = pos >> PAGE_SHIFT;
  2115. offset = pos & ~PAGE_MASK;
  2116. while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
  2117. zerofrom = curpos & ~PAGE_MASK;
  2118. if (zerofrom & (blocksize-1)) {
  2119. *bytes |= (blocksize-1);
  2120. (*bytes)++;
  2121. }
  2122. len = PAGE_SIZE - zerofrom;
  2123. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2124. &page, &fsdata);
  2125. if (err)
  2126. goto out;
  2127. zero_user(page, zerofrom, len);
  2128. err = pagecache_write_end(file, mapping, curpos, len, len,
  2129. page, fsdata);
  2130. if (err < 0)
  2131. goto out;
  2132. BUG_ON(err != len);
  2133. err = 0;
  2134. balance_dirty_pages_ratelimited(mapping);
  2135. if (unlikely(fatal_signal_pending(current))) {
  2136. err = -EINTR;
  2137. goto out;
  2138. }
  2139. }
  2140. /* page covers the boundary, find the boundary offset */
  2141. if (index == curidx) {
  2142. zerofrom = curpos & ~PAGE_MASK;
  2143. /* if we will expand the thing last block will be filled */
  2144. if (offset <= zerofrom) {
  2145. goto out;
  2146. }
  2147. if (zerofrom & (blocksize-1)) {
  2148. *bytes |= (blocksize-1);
  2149. (*bytes)++;
  2150. }
  2151. len = offset - zerofrom;
  2152. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2153. &page, &fsdata);
  2154. if (err)
  2155. goto out;
  2156. zero_user(page, zerofrom, len);
  2157. err = pagecache_write_end(file, mapping, curpos, len, len,
  2158. page, fsdata);
  2159. if (err < 0)
  2160. goto out;
  2161. BUG_ON(err != len);
  2162. err = 0;
  2163. }
  2164. out:
  2165. return err;
  2166. }
  2167. /*
  2168. * For moronic filesystems that do not allow holes in file.
  2169. * We may have to extend the file.
  2170. */
  2171. int cont_write_begin(struct file *file, struct address_space *mapping,
  2172. loff_t pos, unsigned len, unsigned flags,
  2173. struct page **pagep, void **fsdata,
  2174. get_block_t *get_block, loff_t *bytes)
  2175. {
  2176. struct inode *inode = mapping->host;
  2177. unsigned int blocksize = i_blocksize(inode);
  2178. unsigned int zerofrom;
  2179. int err;
  2180. err = cont_expand_zero(file, mapping, pos, bytes);
  2181. if (err)
  2182. return err;
  2183. zerofrom = *bytes & ~PAGE_MASK;
  2184. if (pos+len > *bytes && zerofrom & (blocksize-1)) {
  2185. *bytes |= (blocksize-1);
  2186. (*bytes)++;
  2187. }
  2188. return block_write_begin(mapping, pos, len, flags, pagep, get_block);
  2189. }
  2190. EXPORT_SYMBOL(cont_write_begin);
  2191. int block_commit_write(struct page *page, unsigned from, unsigned to)
  2192. {
  2193. struct inode *inode = page->mapping->host;
  2194. __block_commit_write(inode,page,from,to);
  2195. return 0;
  2196. }
  2197. EXPORT_SYMBOL(block_commit_write);
  2198. /*
  2199. * block_page_mkwrite() is not allowed to change the file size as it gets
  2200. * called from a page fault handler when a page is first dirtied. Hence we must
  2201. * be careful to check for EOF conditions here. We set the page up correctly
  2202. * for a written page which means we get ENOSPC checking when writing into
  2203. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2204. * support these features.
  2205. *
  2206. * We are not allowed to take the i_mutex here so we have to play games to
  2207. * protect against truncate races as the page could now be beyond EOF. Because
  2208. * truncate writes the inode size before removing pages, once we have the
  2209. * page lock we can determine safely if the page is beyond EOF. If it is not
  2210. * beyond EOF, then the page is guaranteed safe against truncation until we
  2211. * unlock the page.
  2212. *
  2213. * Direct callers of this function should protect against filesystem freezing
  2214. * using sb_start_pagefault() - sb_end_pagefault() functions.
  2215. */
  2216. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2217. get_block_t get_block)
  2218. {
  2219. struct page *page = vmf->page;
  2220. struct inode *inode = file_inode(vma->vm_file);
  2221. unsigned long end;
  2222. loff_t size;
  2223. int ret;
  2224. lock_page(page);
  2225. size = i_size_read(inode);
  2226. if ((page->mapping != inode->i_mapping) ||
  2227. (page_offset(page) > size)) {
  2228. /* We overload EFAULT to mean page got truncated */
  2229. ret = -EFAULT;
  2230. goto out_unlock;
  2231. }
  2232. /* page is wholly or partially inside EOF */
  2233. if (((page->index + 1) << PAGE_SHIFT) > size)
  2234. end = size & ~PAGE_MASK;
  2235. else
  2236. end = PAGE_SIZE;
  2237. ret = __block_write_begin(page, 0, end, get_block);
  2238. if (!ret)
  2239. ret = block_commit_write(page, 0, end);
  2240. if (unlikely(ret < 0))
  2241. goto out_unlock;
  2242. set_page_dirty(page);
  2243. wait_for_stable_page(page);
  2244. return 0;
  2245. out_unlock:
  2246. unlock_page(page);
  2247. return ret;
  2248. }
  2249. EXPORT_SYMBOL(block_page_mkwrite);
  2250. /*
  2251. * nobh_write_begin()'s prereads are special: the buffer_heads are freed
  2252. * immediately, while under the page lock. So it needs a special end_io
  2253. * handler which does not touch the bh after unlocking it.
  2254. */
  2255. static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
  2256. {
  2257. __end_buffer_read_notouch(bh, uptodate);
  2258. }
  2259. /*
  2260. * Attach the singly-linked list of buffers created by nobh_write_begin, to
  2261. * the page (converting it to circular linked list and taking care of page
  2262. * dirty races).
  2263. */
  2264. static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
  2265. {
  2266. struct buffer_head *bh;
  2267. BUG_ON(!PageLocked(page));
  2268. spin_lock(&page->mapping->private_lock);
  2269. bh = head;
  2270. do {
  2271. if (PageDirty(page))
  2272. set_buffer_dirty(bh);
  2273. if (!bh->b_this_page)
  2274. bh->b_this_page = head;
  2275. bh = bh->b_this_page;
  2276. } while (bh != head);
  2277. attach_page_buffers(page, head);
  2278. spin_unlock(&page->mapping->private_lock);
  2279. }
  2280. /*
  2281. * On entry, the page is fully not uptodate.
  2282. * On exit the page is fully uptodate in the areas outside (from,to)
  2283. * The filesystem needs to handle block truncation upon failure.
  2284. */
  2285. int nobh_write_begin(struct address_space *mapping,
  2286. loff_t pos, unsigned len, unsigned flags,
  2287. struct page **pagep, void **fsdata,
  2288. get_block_t *get_block)
  2289. {
  2290. struct inode *inode = mapping->host;
  2291. const unsigned blkbits = inode->i_blkbits;
  2292. const unsigned blocksize = 1 << blkbits;
  2293. struct buffer_head *head, *bh;
  2294. struct page *page;
  2295. pgoff_t index;
  2296. unsigned from, to;
  2297. unsigned block_in_page;
  2298. unsigned block_start, block_end;
  2299. sector_t block_in_file;
  2300. int nr_reads = 0;
  2301. int ret = 0;
  2302. int is_mapped_to_disk = 1;
  2303. index = pos >> PAGE_SHIFT;
  2304. from = pos & (PAGE_SIZE - 1);
  2305. to = from + len;
  2306. page = grab_cache_page_write_begin(mapping, index, flags);
  2307. if (!page)
  2308. return -ENOMEM;
  2309. *pagep = page;
  2310. *fsdata = NULL;
  2311. if (page_has_buffers(page)) {
  2312. ret = __block_write_begin(page, pos, len, get_block);
  2313. if (unlikely(ret))
  2314. goto out_release;
  2315. return ret;
  2316. }
  2317. if (PageMappedToDisk(page))
  2318. return 0;
  2319. /*
  2320. * Allocate buffers so that we can keep track of state, and potentially
  2321. * attach them to the page if an error occurs. In the common case of
  2322. * no error, they will just be freed again without ever being attached
  2323. * to the page (which is all OK, because we're under the page lock).
  2324. *
  2325. * Be careful: the buffer linked list is a NULL terminated one, rather
  2326. * than the circular one we're used to.
  2327. */
  2328. head = alloc_page_buffers(page, blocksize, false);
  2329. if (!head) {
  2330. ret = -ENOMEM;
  2331. goto out_release;
  2332. }
  2333. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  2334. /*
  2335. * We loop across all blocks in the page, whether or not they are
  2336. * part of the affected region. This is so we can discover if the
  2337. * page is fully mapped-to-disk.
  2338. */
  2339. for (block_start = 0, block_in_page = 0, bh = head;
  2340. block_start < PAGE_SIZE;
  2341. block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
  2342. int create;
  2343. block_end = block_start + blocksize;
  2344. bh->b_state = 0;
  2345. create = 1;
  2346. if (block_start >= to)
  2347. create = 0;
  2348. ret = get_block(inode, block_in_file + block_in_page,
  2349. bh, create);
  2350. if (ret)
  2351. goto failed;
  2352. if (!buffer_mapped(bh))
  2353. is_mapped_to_disk = 0;
  2354. if (buffer_new(bh))
  2355. clean_bdev_bh_alias(bh);
  2356. if (PageUptodate(page)) {
  2357. set_buffer_uptodate(bh);
  2358. continue;
  2359. }
  2360. if (buffer_new(bh) || !buffer_mapped(bh)) {
  2361. zero_user_segments(page, block_start, from,
  2362. to, block_end);
  2363. continue;
  2364. }
  2365. if (buffer_uptodate(bh))
  2366. continue; /* reiserfs does this */
  2367. if (block_start < from || block_end > to) {
  2368. lock_buffer(bh);
  2369. bh->b_end_io = end_buffer_read_nobh;
  2370. submit_bh(REQ_OP_READ, 0, bh);
  2371. nr_reads++;
  2372. }
  2373. }
  2374. if (nr_reads) {
  2375. /*
  2376. * The page is locked, so these buffers are protected from
  2377. * any VM or truncate activity. Hence we don't need to care
  2378. * for the buffer_head refcounts.
  2379. */
  2380. for (bh = head; bh; bh = bh->b_this_page) {
  2381. wait_on_buffer(bh);
  2382. if (!buffer_uptodate(bh))
  2383. ret = -EIO;
  2384. }
  2385. if (ret)
  2386. goto failed;
  2387. }
  2388. if (is_mapped_to_disk)
  2389. SetPageMappedToDisk(page);
  2390. *fsdata = head; /* to be released by nobh_write_end */
  2391. return 0;
  2392. failed:
  2393. BUG_ON(!ret);
  2394. /*
  2395. * Error recovery is a bit difficult. We need to zero out blocks that
  2396. * were newly allocated, and dirty them to ensure they get written out.
  2397. * Buffers need to be attached to the page at this point, otherwise
  2398. * the handling of potential IO errors during writeout would be hard
  2399. * (could try doing synchronous writeout, but what if that fails too?)
  2400. */
  2401. attach_nobh_buffers(page, head);
  2402. page_zero_new_buffers(page, from, to);
  2403. out_release:
  2404. unlock_page(page);
  2405. put_page(page);
  2406. *pagep = NULL;
  2407. return ret;
  2408. }
  2409. EXPORT_SYMBOL(nobh_write_begin);
  2410. int nobh_write_end(struct file *file, struct address_space *mapping,
  2411. loff_t pos, unsigned len, unsigned copied,
  2412. struct page *page, void *fsdata)
  2413. {
  2414. struct inode *inode = page->mapping->host;
  2415. struct buffer_head *head = fsdata;
  2416. struct buffer_head *bh;
  2417. BUG_ON(fsdata != NULL && page_has_buffers(page));
  2418. if (unlikely(copied < len) && head)
  2419. attach_nobh_buffers(page, head);
  2420. if (page_has_buffers(page))
  2421. return generic_write_end(file, mapping, pos, len,
  2422. copied, page, fsdata);
  2423. SetPageUptodate(page);
  2424. set_page_dirty(page);
  2425. if (pos+copied > inode->i_size) {
  2426. i_size_write(inode, pos+copied);
  2427. mark_inode_dirty(inode);
  2428. }
  2429. unlock_page(page);
  2430. put_page(page);
  2431. while (head) {
  2432. bh = head;
  2433. head = head->b_this_page;
  2434. free_buffer_head(bh);
  2435. }
  2436. return copied;
  2437. }
  2438. EXPORT_SYMBOL(nobh_write_end);
  2439. /*
  2440. * nobh_writepage() - based on block_full_write_page() except
  2441. * that it tries to operate without attaching bufferheads to
  2442. * the page.
  2443. */
  2444. int nobh_writepage(struct page *page, get_block_t *get_block,
  2445. struct writeback_control *wbc)
  2446. {
  2447. struct inode * const inode = page->mapping->host;
  2448. loff_t i_size = i_size_read(inode);
  2449. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2450. unsigned offset;
  2451. int ret;
  2452. /* Is the page fully inside i_size? */
  2453. if (page->index < end_index)
  2454. goto out;
  2455. /* Is the page fully outside i_size? (truncate in progress) */
  2456. offset = i_size & (PAGE_SIZE-1);
  2457. if (page->index >= end_index+1 || !offset) {
  2458. unlock_page(page);
  2459. return 0; /* don't care */
  2460. }
  2461. /*
  2462. * The page straddles i_size. It must be zeroed out on each and every
  2463. * writepage invocation because it may be mmapped. "A file is mapped
  2464. * in multiples of the page size. For a file that is not a multiple of
  2465. * the page size, the remaining memory is zeroed when mapped, and
  2466. * writes to that region are not written out to the file."
  2467. */
  2468. zero_user_segment(page, offset, PAGE_SIZE);
  2469. out:
  2470. ret = mpage_writepage(page, get_block, wbc);
  2471. if (ret == -EAGAIN)
  2472. ret = __block_write_full_page(inode, page, get_block, wbc,
  2473. end_buffer_async_write);
  2474. return ret;
  2475. }
  2476. EXPORT_SYMBOL(nobh_writepage);
  2477. int nobh_truncate_page(struct address_space *mapping,
  2478. loff_t from, get_block_t *get_block)
  2479. {
  2480. pgoff_t index = from >> PAGE_SHIFT;
  2481. unsigned offset = from & (PAGE_SIZE-1);
  2482. unsigned blocksize;
  2483. sector_t iblock;
  2484. unsigned length, pos;
  2485. struct inode *inode = mapping->host;
  2486. struct page *page;
  2487. struct buffer_head map_bh;
  2488. int err;
  2489. blocksize = i_blocksize(inode);
  2490. length = offset & (blocksize - 1);
  2491. /* Block boundary? Nothing to do */
  2492. if (!length)
  2493. return 0;
  2494. length = blocksize - length;
  2495. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2496. page = grab_cache_page(mapping, index);
  2497. err = -ENOMEM;
  2498. if (!page)
  2499. goto out;
  2500. if (page_has_buffers(page)) {
  2501. has_buffers:
  2502. unlock_page(page);
  2503. put_page(page);
  2504. return block_truncate_page(mapping, from, get_block);
  2505. }
  2506. /* Find the buffer that contains "offset" */
  2507. pos = blocksize;
  2508. while (offset >= pos) {
  2509. iblock++;
  2510. pos += blocksize;
  2511. }
  2512. map_bh.b_size = blocksize;
  2513. map_bh.b_state = 0;
  2514. err = get_block(inode, iblock, &map_bh, 0);
  2515. if (err)
  2516. goto unlock;
  2517. /* unmapped? It's a hole - nothing to do */
  2518. if (!buffer_mapped(&map_bh))
  2519. goto unlock;
  2520. /* Ok, it's mapped. Make sure it's up-to-date */
  2521. if (!PageUptodate(page)) {
  2522. err = mapping->a_ops->readpage(NULL, page);
  2523. if (err) {
  2524. put_page(page);
  2525. goto out;
  2526. }
  2527. lock_page(page);
  2528. if (!PageUptodate(page)) {
  2529. err = -EIO;
  2530. goto unlock;
  2531. }
  2532. if (page_has_buffers(page))
  2533. goto has_buffers;
  2534. }
  2535. zero_user(page, offset, length);
  2536. set_page_dirty(page);
  2537. err = 0;
  2538. unlock:
  2539. unlock_page(page);
  2540. put_page(page);
  2541. out:
  2542. return err;
  2543. }
  2544. EXPORT_SYMBOL(nobh_truncate_page);
  2545. int block_truncate_page(struct address_space *mapping,
  2546. loff_t from, get_block_t *get_block)
  2547. {
  2548. pgoff_t index = from >> PAGE_SHIFT;
  2549. unsigned offset = from & (PAGE_SIZE-1);
  2550. unsigned blocksize;
  2551. sector_t iblock;
  2552. unsigned length, pos;
  2553. struct inode *inode = mapping->host;
  2554. struct page *page;
  2555. struct buffer_head *bh;
  2556. int err;
  2557. blocksize = i_blocksize(inode);
  2558. length = offset & (blocksize - 1);
  2559. /* Block boundary? Nothing to do */
  2560. if (!length)
  2561. return 0;
  2562. length = blocksize - length;
  2563. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2564. page = grab_cache_page(mapping, index);
  2565. err = -ENOMEM;
  2566. if (!page)
  2567. goto out;
  2568. if (!page_has_buffers(page))
  2569. create_empty_buffers(page, blocksize, 0);
  2570. /* Find the buffer that contains "offset" */
  2571. bh = page_buffers(page);
  2572. pos = blocksize;
  2573. while (offset >= pos) {
  2574. bh = bh->b_this_page;
  2575. iblock++;
  2576. pos += blocksize;
  2577. }
  2578. err = 0;
  2579. if (!buffer_mapped(bh)) {
  2580. WARN_ON(bh->b_size != blocksize);
  2581. err = get_block(inode, iblock, bh, 0);
  2582. if (err)
  2583. goto unlock;
  2584. /* unmapped? It's a hole - nothing to do */
  2585. if (!buffer_mapped(bh))
  2586. goto unlock;
  2587. }
  2588. /* Ok, it's mapped. Make sure it's up-to-date */
  2589. if (PageUptodate(page))
  2590. set_buffer_uptodate(bh);
  2591. if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
  2592. err = -EIO;
  2593. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  2594. wait_on_buffer(bh);
  2595. /* Uhhuh. Read error. Complain and punt. */
  2596. if (!buffer_uptodate(bh))
  2597. goto unlock;
  2598. }
  2599. zero_user(page, offset, length);
  2600. mark_buffer_dirty(bh);
  2601. err = 0;
  2602. unlock:
  2603. unlock_page(page);
  2604. put_page(page);
  2605. out:
  2606. return err;
  2607. }
  2608. EXPORT_SYMBOL(block_truncate_page);
  2609. /*
  2610. * The generic ->writepage function for buffer-backed address_spaces
  2611. */
  2612. int block_write_full_page(struct page *page, get_block_t *get_block,
  2613. struct writeback_control *wbc)
  2614. {
  2615. struct inode * const inode = page->mapping->host;
  2616. loff_t i_size = i_size_read(inode);
  2617. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2618. unsigned offset;
  2619. /* Is the page fully inside i_size? */
  2620. if (page->index < end_index)
  2621. return __block_write_full_page(inode, page, get_block, wbc,
  2622. end_buffer_async_write);
  2623. /* Is the page fully outside i_size? (truncate in progress) */
  2624. offset = i_size & (PAGE_SIZE-1);
  2625. if (page->index >= end_index+1 || !offset) {
  2626. unlock_page(page);
  2627. return 0; /* don't care */
  2628. }
  2629. /*
  2630. * The page straddles i_size. It must be zeroed out on each and every
  2631. * writepage invocation because it may be mmapped. "A file is mapped
  2632. * in multiples of the page size. For a file that is not a multiple of
  2633. * the page size, the remaining memory is zeroed when mapped, and
  2634. * writes to that region are not written out to the file."
  2635. */
  2636. zero_user_segment(page, offset, PAGE_SIZE);
  2637. return __block_write_full_page(inode, page, get_block, wbc,
  2638. end_buffer_async_write);
  2639. }
  2640. EXPORT_SYMBOL(block_write_full_page);
  2641. sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
  2642. get_block_t *get_block)
  2643. {
  2644. struct inode *inode = mapping->host;
  2645. struct buffer_head tmp = {
  2646. .b_size = i_blocksize(inode),
  2647. };
  2648. get_block(inode, block, &tmp, 0);
  2649. return tmp.b_blocknr;
  2650. }
  2651. EXPORT_SYMBOL(generic_block_bmap);
  2652. static void end_bio_bh_io_sync(struct bio *bio)
  2653. {
  2654. struct buffer_head *bh = bio->bi_private;
  2655. if (unlikely(bio_flagged(bio, BIO_QUIET)))
  2656. set_bit(BH_Quiet, &bh->b_state);
  2657. bh->b_end_io(bh, !bio->bi_status);
  2658. bio_put(bio);
  2659. }
  2660. /*
  2661. * This allows us to do IO even on the odd last sectors
  2662. * of a device, even if the block size is some multiple
  2663. * of the physical sector size.
  2664. *
  2665. * We'll just truncate the bio to the size of the device,
  2666. * and clear the end of the buffer head manually.
  2667. *
  2668. * Truly out-of-range accesses will turn into actual IO
  2669. * errors, this only handles the "we need to be able to
  2670. * do IO at the final sector" case.
  2671. */
  2672. void guard_bio_eod(int op, struct bio *bio)
  2673. {
  2674. sector_t maxsector;
  2675. struct bio_vec *bvec = bio_last_bvec_all(bio);
  2676. unsigned truncated_bytes;
  2677. struct hd_struct *part;
  2678. rcu_read_lock();
  2679. part = __disk_get_part(bio->bi_disk, bio->bi_partno);
  2680. if (part)
  2681. maxsector = part_nr_sects_read(part);
  2682. else
  2683. maxsector = get_capacity(bio->bi_disk);
  2684. rcu_read_unlock();
  2685. if (!maxsector)
  2686. return;
  2687. /*
  2688. * If the *whole* IO is past the end of the device,
  2689. * let it through, and the IO layer will turn it into
  2690. * an EIO.
  2691. */
  2692. if (unlikely(bio->bi_iter.bi_sector >= maxsector))
  2693. return;
  2694. maxsector -= bio->bi_iter.bi_sector;
  2695. if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
  2696. return;
  2697. /* Uhhuh. We've got a bio that straddles the device size! */
  2698. truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
  2699. /*
  2700. * The bio contains more than one segment which spans EOD, just return
  2701. * and let IO layer turn it into an EIO
  2702. */
  2703. if (truncated_bytes > bvec->bv_len)
  2704. return;
  2705. /* Truncate the bio.. */
  2706. bio->bi_iter.bi_size -= truncated_bytes;
  2707. bvec->bv_len -= truncated_bytes;
  2708. /* ..and clear the end of the buffer for reads */
  2709. if (op == REQ_OP_READ) {
  2710. zero_user(bvec->bv_page, bvec->bv_offset + bvec->bv_len,
  2711. truncated_bytes);
  2712. }
  2713. }
  2714. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  2715. enum rw_hint write_hint, struct writeback_control *wbc)
  2716. {
  2717. struct bio *bio;
  2718. BUG_ON(!buffer_locked(bh));
  2719. BUG_ON(!buffer_mapped(bh));
  2720. BUG_ON(!bh->b_end_io);
  2721. BUG_ON(buffer_delay(bh));
  2722. BUG_ON(buffer_unwritten(bh));
  2723. /*
  2724. * Only clear out a write error when rewriting
  2725. */
  2726. if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
  2727. clear_buffer_write_io_error(bh);
  2728. /*
  2729. * from here on down, it's all bio -- do the initial mapping,
  2730. * submit_bio -> generic_make_request may further map this bio around
  2731. */
  2732. bio = bio_alloc(GFP_NOIO, 1);
  2733. if (wbc) {
  2734. wbc_init_bio(wbc, bio);
  2735. wbc_account_io(wbc, bh->b_page, bh->b_size);
  2736. }
  2737. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  2738. bio_set_dev(bio, bh->b_bdev);
  2739. bio->bi_write_hint = write_hint;
  2740. bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  2741. BUG_ON(bio->bi_iter.bi_size != bh->b_size);
  2742. bio->bi_end_io = end_bio_bh_io_sync;
  2743. bio->bi_private = bh;
  2744. /* Take care of bh's that straddle the end of the device */
  2745. guard_bio_eod(op, bio);
  2746. if (buffer_meta(bh))
  2747. op_flags |= REQ_META;
  2748. if (buffer_prio(bh))
  2749. op_flags |= REQ_PRIO;
  2750. bio_set_op_attrs(bio, op, op_flags);
  2751. submit_bio(bio);
  2752. return 0;
  2753. }
  2754. int submit_bh(int op, int op_flags, struct buffer_head *bh)
  2755. {
  2756. return submit_bh_wbc(op, op_flags, bh, 0, NULL);
  2757. }
  2758. EXPORT_SYMBOL(submit_bh);
  2759. /**
  2760. * ll_rw_block: low-level access to block devices (DEPRECATED)
  2761. * @op: whether to %READ or %WRITE
  2762. * @op_flags: req_flag_bits
  2763. * @nr: number of &struct buffer_heads in the array
  2764. * @bhs: array of pointers to &struct buffer_head
  2765. *
  2766. * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
  2767. * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
  2768. * @op_flags contains flags modifying the detailed I/O behavior, most notably
  2769. * %REQ_RAHEAD.
  2770. *
  2771. * This function drops any buffer that it cannot get a lock on (with the
  2772. * BH_Lock state bit), any buffer that appears to be clean when doing a write
  2773. * request, and any buffer that appears to be up-to-date when doing read
  2774. * request. Further it marks as clean buffers that are processed for
  2775. * writing (the buffer cache won't assume that they are actually clean
  2776. * until the buffer gets unlocked).
  2777. *
  2778. * ll_rw_block sets b_end_io to simple completion handler that marks
  2779. * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
  2780. * any waiters.
  2781. *
  2782. * All of the buffers must be for the same device, and must also be a
  2783. * multiple of the current approved size for the device.
  2784. */
  2785. void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
  2786. {
  2787. int i;
  2788. for (i = 0; i < nr; i++) {
  2789. struct buffer_head *bh = bhs[i];
  2790. if (!trylock_buffer(bh))
  2791. continue;
  2792. if (op == WRITE) {
  2793. if (test_clear_buffer_dirty(bh)) {
  2794. bh->b_end_io = end_buffer_write_sync;
  2795. get_bh(bh);
  2796. submit_bh(op, op_flags, bh);
  2797. continue;
  2798. }
  2799. } else {
  2800. if (!buffer_uptodate(bh)) {
  2801. bh->b_end_io = end_buffer_read_sync;
  2802. get_bh(bh);
  2803. submit_bh(op, op_flags, bh);
  2804. continue;
  2805. }
  2806. }
  2807. unlock_buffer(bh);
  2808. }
  2809. }
  2810. EXPORT_SYMBOL(ll_rw_block);
  2811. void write_dirty_buffer(struct buffer_head *bh, int op_flags)
  2812. {
  2813. lock_buffer(bh);
  2814. if (!test_clear_buffer_dirty(bh)) {
  2815. unlock_buffer(bh);
  2816. return;
  2817. }
  2818. bh->b_end_io = end_buffer_write_sync;
  2819. get_bh(bh);
  2820. submit_bh(REQ_OP_WRITE, op_flags, bh);
  2821. }
  2822. EXPORT_SYMBOL(write_dirty_buffer);
  2823. /*
  2824. * For a data-integrity writeout, we need to wait upon any in-progress I/O
  2825. * and then start new I/O and then wait upon it. The caller must have a ref on
  2826. * the buffer_head.
  2827. */
  2828. int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
  2829. {
  2830. int ret = 0;
  2831. WARN_ON(atomic_read(&bh->b_count) < 1);
  2832. lock_buffer(bh);
  2833. if (test_clear_buffer_dirty(bh)) {
  2834. /*
  2835. * The bh should be mapped, but it might not be if the
  2836. * device was hot-removed. Not much we can do but fail the I/O.
  2837. */
  2838. if (!buffer_mapped(bh)) {
  2839. unlock_buffer(bh);
  2840. return -EIO;
  2841. }
  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. void __init buffer_init(void)
  3075. {
  3076. unsigned long nrpages;
  3077. int ret;
  3078. bh_cachep = kmem_cache_create("buffer_head",
  3079. sizeof(struct buffer_head), 0,
  3080. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  3081. SLAB_MEM_SPREAD),
  3082. NULL);
  3083. /*
  3084. * Limit the bh occupancy to 10% of ZONE_NORMAL
  3085. */
  3086. nrpages = (nr_free_buffer_pages() * 10) / 100;
  3087. max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
  3088. ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
  3089. NULL, buffer_exit_cpu_dead);
  3090. WARN_ON(ret < 0);
  3091. }