buffer.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469
  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. /**
  1202. * __bread_gfp() - reads a specified block and returns the bh
  1203. * @bdev: the block_device to read from
  1204. * @block: number of block
  1205. * @size: size (in bytes) to read
  1206. * @gfp: page allocation flag
  1207. *
  1208. * Reads a specified block, and returns buffer head that contains it.
  1209. * The page cache can be allocated from non-movable area
  1210. * not to prevent page migration if you set gfp to zero.
  1211. * It returns NULL if the block was unreadable.
  1212. */
  1213. struct buffer_head *
  1214. __bread_gfp(struct block_device *bdev, sector_t block,
  1215. unsigned size, gfp_t gfp)
  1216. {
  1217. struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
  1218. if (likely(bh) && !buffer_uptodate(bh))
  1219. bh = __bread_slow(bh);
  1220. return bh;
  1221. }
  1222. EXPORT_SYMBOL(__bread_gfp);
  1223. /*
  1224. * invalidate_bh_lrus() is called rarely - but not only at unmount.
  1225. * This doesn't race because it runs in each cpu either in irq
  1226. * or with preempt disabled.
  1227. */
  1228. static void invalidate_bh_lru(void *arg)
  1229. {
  1230. struct bh_lru *b = &get_cpu_var(bh_lrus);
  1231. int i;
  1232. for (i = 0; i < BH_LRU_SIZE; i++) {
  1233. brelse(b->bhs[i]);
  1234. b->bhs[i] = NULL;
  1235. }
  1236. put_cpu_var(bh_lrus);
  1237. }
  1238. static bool has_bh_in_lru(int cpu, void *dummy)
  1239. {
  1240. struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
  1241. int i;
  1242. for (i = 0; i < BH_LRU_SIZE; i++) {
  1243. if (b->bhs[i])
  1244. return 1;
  1245. }
  1246. return 0;
  1247. }
  1248. void invalidate_bh_lrus(void)
  1249. {
  1250. on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
  1251. }
  1252. EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
  1253. void set_bh_page(struct buffer_head *bh,
  1254. struct page *page, unsigned long offset)
  1255. {
  1256. bh->b_page = page;
  1257. BUG_ON(offset >= PAGE_SIZE);
  1258. if (PageHighMem(page))
  1259. /*
  1260. * This catches illegal uses and preserves the offset:
  1261. */
  1262. bh->b_data = (char *)(0 + offset);
  1263. else
  1264. bh->b_data = page_address(page) + offset;
  1265. }
  1266. EXPORT_SYMBOL(set_bh_page);
  1267. /*
  1268. * Called when truncating a buffer on a page completely.
  1269. */
  1270. /* Bits that are cleared during an invalidate */
  1271. #define BUFFER_FLAGS_DISCARD \
  1272. (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
  1273. 1 << BH_Delay | 1 << BH_Unwritten)
  1274. static void discard_buffer(struct buffer_head * bh)
  1275. {
  1276. unsigned long b_state, b_state_old;
  1277. lock_buffer(bh);
  1278. clear_buffer_dirty(bh);
  1279. bh->b_bdev = NULL;
  1280. b_state = bh->b_state;
  1281. for (;;) {
  1282. b_state_old = cmpxchg(&bh->b_state, b_state,
  1283. (b_state & ~BUFFER_FLAGS_DISCARD));
  1284. if (b_state_old == b_state)
  1285. break;
  1286. b_state = b_state_old;
  1287. }
  1288. unlock_buffer(bh);
  1289. }
  1290. /**
  1291. * block_invalidatepage - invalidate part or all of a buffer-backed page
  1292. *
  1293. * @page: the page which is affected
  1294. * @offset: start of the range to invalidate
  1295. * @length: length of the range to invalidate
  1296. *
  1297. * block_invalidatepage() is called when all or part of the page has become
  1298. * invalidated by a truncate operation.
  1299. *
  1300. * block_invalidatepage() does not have to release all buffers, but it must
  1301. * ensure that no dirty buffer is left outside @offset and that no I/O
  1302. * is underway against any of the blocks which are outside the truncation
  1303. * point. Because the caller is about to free (and possibly reuse) those
  1304. * blocks on-disk.
  1305. */
  1306. void block_invalidatepage(struct page *page, unsigned int offset,
  1307. unsigned int length)
  1308. {
  1309. struct buffer_head *head, *bh, *next;
  1310. unsigned int curr_off = 0;
  1311. unsigned int stop = length + offset;
  1312. BUG_ON(!PageLocked(page));
  1313. if (!page_has_buffers(page))
  1314. goto out;
  1315. /*
  1316. * Check for overflow
  1317. */
  1318. BUG_ON(stop > PAGE_SIZE || stop < length);
  1319. head = page_buffers(page);
  1320. bh = head;
  1321. do {
  1322. unsigned int next_off = curr_off + bh->b_size;
  1323. next = bh->b_this_page;
  1324. /*
  1325. * Are we still fully in range ?
  1326. */
  1327. if (next_off > stop)
  1328. goto out;
  1329. /*
  1330. * is this block fully invalidated?
  1331. */
  1332. if (offset <= curr_off)
  1333. discard_buffer(bh);
  1334. curr_off = next_off;
  1335. bh = next;
  1336. } while (bh != head);
  1337. /*
  1338. * We release buffers only if the entire page is being invalidated.
  1339. * The get_block cached value has been unconditionally invalidated,
  1340. * so real IO is not possible anymore.
  1341. */
  1342. if (length == PAGE_SIZE)
  1343. try_to_release_page(page, 0);
  1344. out:
  1345. return;
  1346. }
  1347. EXPORT_SYMBOL(block_invalidatepage);
  1348. /*
  1349. * We attach and possibly dirty the buffers atomically wrt
  1350. * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
  1351. * is already excluded via the page lock.
  1352. */
  1353. void create_empty_buffers(struct page *page,
  1354. unsigned long blocksize, unsigned long b_state)
  1355. {
  1356. struct buffer_head *bh, *head, *tail;
  1357. head = alloc_page_buffers(page, blocksize, true);
  1358. bh = head;
  1359. do {
  1360. bh->b_state |= b_state;
  1361. tail = bh;
  1362. bh = bh->b_this_page;
  1363. } while (bh);
  1364. tail->b_this_page = head;
  1365. spin_lock(&page->mapping->private_lock);
  1366. if (PageUptodate(page) || PageDirty(page)) {
  1367. bh = head;
  1368. do {
  1369. if (PageDirty(page))
  1370. set_buffer_dirty(bh);
  1371. if (PageUptodate(page))
  1372. set_buffer_uptodate(bh);
  1373. bh = bh->b_this_page;
  1374. } while (bh != head);
  1375. }
  1376. attach_page_buffers(page, head);
  1377. spin_unlock(&page->mapping->private_lock);
  1378. }
  1379. EXPORT_SYMBOL(create_empty_buffers);
  1380. /**
  1381. * clean_bdev_aliases: clean a range of buffers in block device
  1382. * @bdev: Block device to clean buffers in
  1383. * @block: Start of a range of blocks to clean
  1384. * @len: Number of blocks to clean
  1385. *
  1386. * We are taking a range of blocks for data and we don't want writeback of any
  1387. * buffer-cache aliases starting from return from this function and until the
  1388. * moment when something will explicitly mark the buffer dirty (hopefully that
  1389. * will not happen until we will free that block ;-) We don't even need to mark
  1390. * it not-uptodate - nobody can expect anything from a newly allocated buffer
  1391. * anyway. We used to use unmap_buffer() for such invalidation, but that was
  1392. * wrong. We definitely don't want to mark the alias unmapped, for example - it
  1393. * would confuse anyone who might pick it with bread() afterwards...
  1394. *
  1395. * Also.. Note that bforget() doesn't lock the buffer. So there can be
  1396. * writeout I/O going on against recently-freed buffers. We don't wait on that
  1397. * I/O in bforget() - it's more efficient to wait on the I/O only if we really
  1398. * need to. That happens here.
  1399. */
  1400. void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
  1401. {
  1402. struct inode *bd_inode = bdev->bd_inode;
  1403. struct address_space *bd_mapping = bd_inode->i_mapping;
  1404. struct pagevec pvec;
  1405. pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1406. pgoff_t end;
  1407. int i, count;
  1408. struct buffer_head *bh;
  1409. struct buffer_head *head;
  1410. end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
  1411. pagevec_init(&pvec);
  1412. while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) {
  1413. count = pagevec_count(&pvec);
  1414. for (i = 0; i < count; i++) {
  1415. struct page *page = pvec.pages[i];
  1416. if (!page_has_buffers(page))
  1417. continue;
  1418. /*
  1419. * We use page lock instead of bd_mapping->private_lock
  1420. * to pin buffers here since we can afford to sleep and
  1421. * it scales better than a global spinlock lock.
  1422. */
  1423. lock_page(page);
  1424. /* Recheck when the page is locked which pins bhs */
  1425. if (!page_has_buffers(page))
  1426. goto unlock_page;
  1427. head = page_buffers(page);
  1428. bh = head;
  1429. do {
  1430. if (!buffer_mapped(bh) || (bh->b_blocknr < block))
  1431. goto next;
  1432. if (bh->b_blocknr >= block + len)
  1433. break;
  1434. clear_buffer_dirty(bh);
  1435. wait_on_buffer(bh);
  1436. clear_buffer_req(bh);
  1437. next:
  1438. bh = bh->b_this_page;
  1439. } while (bh != head);
  1440. unlock_page:
  1441. unlock_page(page);
  1442. }
  1443. pagevec_release(&pvec);
  1444. cond_resched();
  1445. /* End of range already reached? */
  1446. if (index > end || !index)
  1447. break;
  1448. }
  1449. }
  1450. EXPORT_SYMBOL(clean_bdev_aliases);
  1451. /*
  1452. * Size is a power-of-two in the range 512..PAGE_SIZE,
  1453. * and the case we care about most is PAGE_SIZE.
  1454. *
  1455. * So this *could* possibly be written with those
  1456. * constraints in mind (relevant mostly if some
  1457. * architecture has a slow bit-scan instruction)
  1458. */
  1459. static inline int block_size_bits(unsigned int blocksize)
  1460. {
  1461. return ilog2(blocksize);
  1462. }
  1463. static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
  1464. {
  1465. BUG_ON(!PageLocked(page));
  1466. if (!page_has_buffers(page))
  1467. create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits),
  1468. b_state);
  1469. return page_buffers(page);
  1470. }
  1471. /*
  1472. * NOTE! All mapped/uptodate combinations are valid:
  1473. *
  1474. * Mapped Uptodate Meaning
  1475. *
  1476. * No No "unknown" - must do get_block()
  1477. * No Yes "hole" - zero-filled
  1478. * Yes No "allocated" - allocated on disk, not read in
  1479. * Yes Yes "valid" - allocated and up-to-date in memory.
  1480. *
  1481. * "Dirty" is valid only with the last case (mapped+uptodate).
  1482. */
  1483. /*
  1484. * While block_write_full_page is writing back the dirty buffers under
  1485. * the page lock, whoever dirtied the buffers may decide to clean them
  1486. * again at any time. We handle that by only looking at the buffer
  1487. * state inside lock_buffer().
  1488. *
  1489. * If block_write_full_page() is called for regular writeback
  1490. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1491. * locked buffer. This only can happen if someone has written the buffer
  1492. * directly, with submit_bh(). At the address_space level PageWriteback
  1493. * prevents this contention from occurring.
  1494. *
  1495. * If block_write_full_page() is called with wbc->sync_mode ==
  1496. * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
  1497. * causes the writes to be flagged as synchronous writes.
  1498. */
  1499. int __block_write_full_page(struct inode *inode, struct page *page,
  1500. get_block_t *get_block, struct writeback_control *wbc,
  1501. bh_end_io_t *handler)
  1502. {
  1503. int err;
  1504. sector_t block;
  1505. sector_t last_block;
  1506. struct buffer_head *bh, *head;
  1507. unsigned int blocksize, bbits;
  1508. int nr_underway = 0;
  1509. int write_flags = wbc_to_write_flags(wbc);
  1510. head = create_page_buffers(page, inode,
  1511. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1512. /*
  1513. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1514. * here, and the (potentially unmapped) buffers may become dirty at
  1515. * any time. If a buffer becomes dirty here after we've inspected it
  1516. * then we just miss that fact, and the page stays dirty.
  1517. *
  1518. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1519. * handle that here by just cleaning them.
  1520. */
  1521. bh = head;
  1522. blocksize = bh->b_size;
  1523. bbits = block_size_bits(blocksize);
  1524. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1525. last_block = (i_size_read(inode) - 1) >> bbits;
  1526. /*
  1527. * Get all the dirty buffers mapped to disk addresses and
  1528. * handle any aliases from the underlying blockdev's mapping.
  1529. */
  1530. do {
  1531. if (block > last_block) {
  1532. /*
  1533. * mapped buffers outside i_size will occur, because
  1534. * this page can be outside i_size when there is a
  1535. * truncate in progress.
  1536. */
  1537. /*
  1538. * The buffer was zeroed by block_write_full_page()
  1539. */
  1540. clear_buffer_dirty(bh);
  1541. set_buffer_uptodate(bh);
  1542. } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
  1543. buffer_dirty(bh)) {
  1544. WARN_ON(bh->b_size != blocksize);
  1545. err = get_block(inode, block, bh, 1);
  1546. if (err)
  1547. goto recover;
  1548. clear_buffer_delay(bh);
  1549. if (buffer_new(bh)) {
  1550. /* blockdev mappings never come here */
  1551. clear_buffer_new(bh);
  1552. clean_bdev_bh_alias(bh);
  1553. }
  1554. }
  1555. bh = bh->b_this_page;
  1556. block++;
  1557. } while (bh != head);
  1558. do {
  1559. if (!buffer_mapped(bh))
  1560. continue;
  1561. /*
  1562. * If it's a fully non-blocking write attempt and we cannot
  1563. * lock the buffer then redirty the page. Note that this can
  1564. * potentially cause a busy-wait loop from writeback threads
  1565. * and kswapd activity, but those code paths have their own
  1566. * higher-level throttling.
  1567. */
  1568. if (wbc->sync_mode != WB_SYNC_NONE) {
  1569. lock_buffer(bh);
  1570. } else if (!trylock_buffer(bh)) {
  1571. redirty_page_for_writepage(wbc, page);
  1572. continue;
  1573. }
  1574. if (test_clear_buffer_dirty(bh)) {
  1575. mark_buffer_async_write_endio(bh, handler);
  1576. } else {
  1577. unlock_buffer(bh);
  1578. }
  1579. } while ((bh = bh->b_this_page) != head);
  1580. /*
  1581. * The page and its buffers are protected by PageWriteback(), so we can
  1582. * drop the bh refcounts early.
  1583. */
  1584. BUG_ON(PageWriteback(page));
  1585. set_page_writeback(page);
  1586. do {
  1587. struct buffer_head *next = bh->b_this_page;
  1588. if (buffer_async_write(bh)) {
  1589. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1590. inode->i_write_hint, wbc);
  1591. nr_underway++;
  1592. }
  1593. bh = next;
  1594. } while (bh != head);
  1595. unlock_page(page);
  1596. err = 0;
  1597. done:
  1598. if (nr_underway == 0) {
  1599. /*
  1600. * The page was marked dirty, but the buffers were
  1601. * clean. Someone wrote them back by hand with
  1602. * ll_rw_block/submit_bh. A rare case.
  1603. */
  1604. end_page_writeback(page);
  1605. /*
  1606. * The page and buffer_heads can be released at any time from
  1607. * here on.
  1608. */
  1609. }
  1610. return err;
  1611. recover:
  1612. /*
  1613. * ENOSPC, or some other error. We may already have added some
  1614. * blocks to the file, so we need to write these out to avoid
  1615. * exposing stale data.
  1616. * The page is currently locked and not marked for writeback
  1617. */
  1618. bh = head;
  1619. /* Recovery: lock and submit the mapped buffers */
  1620. do {
  1621. if (buffer_mapped(bh) && buffer_dirty(bh) &&
  1622. !buffer_delay(bh)) {
  1623. lock_buffer(bh);
  1624. mark_buffer_async_write_endio(bh, handler);
  1625. } else {
  1626. /*
  1627. * The buffer may have been set dirty during
  1628. * attachment to a dirty page.
  1629. */
  1630. clear_buffer_dirty(bh);
  1631. }
  1632. } while ((bh = bh->b_this_page) != head);
  1633. SetPageError(page);
  1634. BUG_ON(PageWriteback(page));
  1635. mapping_set_error(page->mapping, err);
  1636. set_page_writeback(page);
  1637. do {
  1638. struct buffer_head *next = bh->b_this_page;
  1639. if (buffer_async_write(bh)) {
  1640. clear_buffer_dirty(bh);
  1641. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
  1642. inode->i_write_hint, wbc);
  1643. nr_underway++;
  1644. }
  1645. bh = next;
  1646. } while (bh != head);
  1647. unlock_page(page);
  1648. goto done;
  1649. }
  1650. EXPORT_SYMBOL(__block_write_full_page);
  1651. /*
  1652. * If a page has any new buffers, zero them out here, and mark them uptodate
  1653. * and dirty so they'll be written out (in order to prevent uninitialised
  1654. * block data from leaking). And clear the new bit.
  1655. */
  1656. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1657. {
  1658. unsigned int block_start, block_end;
  1659. struct buffer_head *head, *bh;
  1660. BUG_ON(!PageLocked(page));
  1661. if (!page_has_buffers(page))
  1662. return;
  1663. bh = head = page_buffers(page);
  1664. block_start = 0;
  1665. do {
  1666. block_end = block_start + bh->b_size;
  1667. if (buffer_new(bh)) {
  1668. if (block_end > from && block_start < to) {
  1669. if (!PageUptodate(page)) {
  1670. unsigned start, size;
  1671. start = max(from, block_start);
  1672. size = min(to, block_end) - start;
  1673. zero_user(page, start, size);
  1674. set_buffer_uptodate(bh);
  1675. }
  1676. clear_buffer_new(bh);
  1677. mark_buffer_dirty(bh);
  1678. }
  1679. }
  1680. block_start = block_end;
  1681. bh = bh->b_this_page;
  1682. } while (bh != head);
  1683. }
  1684. EXPORT_SYMBOL(page_zero_new_buffers);
  1685. static void
  1686. iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
  1687. struct iomap *iomap)
  1688. {
  1689. loff_t offset = block << inode->i_blkbits;
  1690. bh->b_bdev = iomap->bdev;
  1691. /*
  1692. * Block points to offset in file we need to map, iomap contains
  1693. * the offset at which the map starts. If the map ends before the
  1694. * current block, then do not map the buffer and let the caller
  1695. * handle it.
  1696. */
  1697. BUG_ON(offset >= iomap->offset + iomap->length);
  1698. switch (iomap->type) {
  1699. case IOMAP_HOLE:
  1700. /*
  1701. * If the buffer is not up to date or beyond the current EOF,
  1702. * we need to mark it as new to ensure sub-block zeroing is
  1703. * executed if necessary.
  1704. */
  1705. if (!buffer_uptodate(bh) ||
  1706. (offset >= i_size_read(inode)))
  1707. set_buffer_new(bh);
  1708. break;
  1709. case IOMAP_DELALLOC:
  1710. if (!buffer_uptodate(bh) ||
  1711. (offset >= i_size_read(inode)))
  1712. set_buffer_new(bh);
  1713. set_buffer_uptodate(bh);
  1714. set_buffer_mapped(bh);
  1715. set_buffer_delay(bh);
  1716. break;
  1717. case IOMAP_UNWRITTEN:
  1718. /*
  1719. * For unwritten regions, we always need to ensure that regions
  1720. * in the block we are not writing to are zeroed. Mark the
  1721. * buffer as new to ensure this.
  1722. */
  1723. set_buffer_new(bh);
  1724. set_buffer_unwritten(bh);
  1725. /* FALLTHRU */
  1726. case IOMAP_MAPPED:
  1727. if ((iomap->flags & IOMAP_F_NEW) ||
  1728. offset >= i_size_read(inode))
  1729. set_buffer_new(bh);
  1730. bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
  1731. inode->i_blkbits;
  1732. set_buffer_mapped(bh);
  1733. break;
  1734. }
  1735. }
  1736. int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
  1737. get_block_t *get_block, struct iomap *iomap)
  1738. {
  1739. unsigned from = pos & (PAGE_SIZE - 1);
  1740. unsigned to = from + len;
  1741. struct inode *inode = page->mapping->host;
  1742. unsigned block_start, block_end;
  1743. sector_t block;
  1744. int err = 0;
  1745. unsigned blocksize, bbits;
  1746. struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
  1747. BUG_ON(!PageLocked(page));
  1748. BUG_ON(from > PAGE_SIZE);
  1749. BUG_ON(to > PAGE_SIZE);
  1750. BUG_ON(from > to);
  1751. head = create_page_buffers(page, inode, 0);
  1752. blocksize = head->b_size;
  1753. bbits = block_size_bits(blocksize);
  1754. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1755. for(bh = head, block_start = 0; bh != head || !block_start;
  1756. block++, block_start=block_end, bh = bh->b_this_page) {
  1757. block_end = block_start + blocksize;
  1758. if (block_end <= from || block_start >= to) {
  1759. if (PageUptodate(page)) {
  1760. if (!buffer_uptodate(bh))
  1761. set_buffer_uptodate(bh);
  1762. }
  1763. continue;
  1764. }
  1765. if (buffer_new(bh))
  1766. clear_buffer_new(bh);
  1767. if (!buffer_mapped(bh)) {
  1768. WARN_ON(bh->b_size != blocksize);
  1769. if (get_block) {
  1770. err = get_block(inode, block, bh, 1);
  1771. if (err)
  1772. break;
  1773. } else {
  1774. iomap_to_bh(inode, block, bh, iomap);
  1775. }
  1776. if (buffer_new(bh)) {
  1777. clean_bdev_bh_alias(bh);
  1778. if (PageUptodate(page)) {
  1779. clear_buffer_new(bh);
  1780. set_buffer_uptodate(bh);
  1781. mark_buffer_dirty(bh);
  1782. continue;
  1783. }
  1784. if (block_end > to || block_start < from)
  1785. zero_user_segments(page,
  1786. to, block_end,
  1787. block_start, from);
  1788. continue;
  1789. }
  1790. }
  1791. if (PageUptodate(page)) {
  1792. if (!buffer_uptodate(bh))
  1793. set_buffer_uptodate(bh);
  1794. continue;
  1795. }
  1796. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1797. !buffer_unwritten(bh) &&
  1798. (block_start < from || block_end > to)) {
  1799. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1800. *wait_bh++=bh;
  1801. }
  1802. }
  1803. /*
  1804. * If we issued read requests - let them complete.
  1805. */
  1806. while(wait_bh > wait) {
  1807. wait_on_buffer(*--wait_bh);
  1808. if (!buffer_uptodate(*wait_bh))
  1809. err = -EIO;
  1810. }
  1811. if (unlikely(err))
  1812. page_zero_new_buffers(page, from, to);
  1813. return err;
  1814. }
  1815. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  1816. get_block_t *get_block)
  1817. {
  1818. return __block_write_begin_int(page, pos, len, get_block, NULL);
  1819. }
  1820. EXPORT_SYMBOL(__block_write_begin);
  1821. static int __block_commit_write(struct inode *inode, struct page *page,
  1822. unsigned from, unsigned to)
  1823. {
  1824. unsigned block_start, block_end;
  1825. int partial = 0;
  1826. unsigned blocksize;
  1827. struct buffer_head *bh, *head;
  1828. bh = head = page_buffers(page);
  1829. blocksize = bh->b_size;
  1830. block_start = 0;
  1831. do {
  1832. block_end = block_start + blocksize;
  1833. if (block_end <= from || block_start >= to) {
  1834. if (!buffer_uptodate(bh))
  1835. partial = 1;
  1836. } else {
  1837. set_buffer_uptodate(bh);
  1838. mark_buffer_dirty(bh);
  1839. }
  1840. clear_buffer_new(bh);
  1841. block_start = block_end;
  1842. bh = bh->b_this_page;
  1843. } while (bh != head);
  1844. /*
  1845. * If this is a partial write which happened to make all buffers
  1846. * uptodate then we can optimize away a bogus readpage() for
  1847. * the next read(). Here we 'discover' whether the page went
  1848. * uptodate as a result of this (potentially partial) write.
  1849. */
  1850. if (!partial)
  1851. SetPageUptodate(page);
  1852. return 0;
  1853. }
  1854. /*
  1855. * block_write_begin takes care of the basic task of block allocation and
  1856. * bringing partial write blocks uptodate first.
  1857. *
  1858. * The filesystem needs to handle block truncation upon failure.
  1859. */
  1860. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  1861. unsigned flags, struct page **pagep, get_block_t *get_block)
  1862. {
  1863. pgoff_t index = pos >> PAGE_SHIFT;
  1864. struct page *page;
  1865. int status;
  1866. page = grab_cache_page_write_begin(mapping, index, flags);
  1867. if (!page)
  1868. return -ENOMEM;
  1869. status = __block_write_begin(page, pos, len, get_block);
  1870. if (unlikely(status)) {
  1871. unlock_page(page);
  1872. put_page(page);
  1873. page = NULL;
  1874. }
  1875. *pagep = page;
  1876. return status;
  1877. }
  1878. EXPORT_SYMBOL(block_write_begin);
  1879. int __generic_write_end(struct inode *inode, loff_t pos, unsigned copied,
  1880. struct page *page)
  1881. {
  1882. loff_t old_size = inode->i_size;
  1883. bool i_size_changed = false;
  1884. /*
  1885. * No need to use i_size_read() here, the i_size cannot change under us
  1886. * because we hold i_rwsem.
  1887. *
  1888. * But it's important to update i_size while still holding page lock:
  1889. * page writeout could otherwise come in and zero beyond i_size.
  1890. */
  1891. if (pos + copied > inode->i_size) {
  1892. i_size_write(inode, pos + copied);
  1893. i_size_changed = true;
  1894. }
  1895. unlock_page(page);
  1896. put_page(page);
  1897. if (old_size < pos)
  1898. pagecache_isize_extended(inode, old_size, pos);
  1899. /*
  1900. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1901. * makes the holding time of page lock longer. Second, it forces lock
  1902. * ordering of page lock and transaction start for journaling
  1903. * filesystems.
  1904. */
  1905. if (i_size_changed)
  1906. mark_inode_dirty(inode);
  1907. return copied;
  1908. }
  1909. int block_write_end(struct file *file, struct address_space *mapping,
  1910. loff_t pos, unsigned len, unsigned copied,
  1911. struct page *page, void *fsdata)
  1912. {
  1913. struct inode *inode = mapping->host;
  1914. unsigned start;
  1915. start = pos & (PAGE_SIZE - 1);
  1916. if (unlikely(copied < len)) {
  1917. /*
  1918. * The buffers that were written will now be uptodate, so we
  1919. * don't have to worry about a readpage reading them and
  1920. * overwriting a partial write. However if we have encountered
  1921. * a short write and only partially written into a buffer, it
  1922. * will not be marked uptodate, so a readpage might come in and
  1923. * destroy our partial write.
  1924. *
  1925. * Do the simplest thing, and just treat any short write to a
  1926. * non uptodate page as a zero-length write, and force the
  1927. * caller to redo the whole thing.
  1928. */
  1929. if (!PageUptodate(page))
  1930. copied = 0;
  1931. page_zero_new_buffers(page, start+copied, start+len);
  1932. }
  1933. flush_dcache_page(page);
  1934. /* This could be a short (even 0-length) commit */
  1935. __block_commit_write(inode, page, start, start+copied);
  1936. return copied;
  1937. }
  1938. EXPORT_SYMBOL(block_write_end);
  1939. int generic_write_end(struct file *file, struct address_space *mapping,
  1940. loff_t pos, unsigned len, unsigned copied,
  1941. struct page *page, void *fsdata)
  1942. {
  1943. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1944. return __generic_write_end(mapping->host, pos, copied, page);
  1945. }
  1946. EXPORT_SYMBOL(generic_write_end);
  1947. /*
  1948. * block_is_partially_uptodate checks whether buffers within a page are
  1949. * uptodate or not.
  1950. *
  1951. * Returns true if all buffers which correspond to a file portion
  1952. * we want to read are uptodate.
  1953. */
  1954. int block_is_partially_uptodate(struct page *page, unsigned long from,
  1955. unsigned long count)
  1956. {
  1957. unsigned block_start, block_end, blocksize;
  1958. unsigned to;
  1959. struct buffer_head *bh, *head;
  1960. int ret = 1;
  1961. if (!page_has_buffers(page))
  1962. return 0;
  1963. head = page_buffers(page);
  1964. blocksize = head->b_size;
  1965. to = min_t(unsigned, PAGE_SIZE - from, count);
  1966. to = from + to;
  1967. if (from < blocksize && to > PAGE_SIZE - blocksize)
  1968. return 0;
  1969. bh = head;
  1970. block_start = 0;
  1971. do {
  1972. block_end = block_start + blocksize;
  1973. if (block_end > from && block_start < to) {
  1974. if (!buffer_uptodate(bh)) {
  1975. ret = 0;
  1976. break;
  1977. }
  1978. if (block_end >= to)
  1979. break;
  1980. }
  1981. block_start = block_end;
  1982. bh = bh->b_this_page;
  1983. } while (bh != head);
  1984. return ret;
  1985. }
  1986. EXPORT_SYMBOL(block_is_partially_uptodate);
  1987. /*
  1988. * Generic "read page" function for block devices that have the normal
  1989. * get_block functionality. This is most of the block device filesystems.
  1990. * Reads the page asynchronously --- the unlock_buffer() and
  1991. * set/clear_buffer_uptodate() functions propagate buffer state into the
  1992. * page struct once IO has completed.
  1993. */
  1994. int block_read_full_page(struct page *page, get_block_t *get_block)
  1995. {
  1996. struct inode *inode = page->mapping->host;
  1997. sector_t iblock, lblock;
  1998. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  1999. unsigned int blocksize, bbits;
  2000. int nr, i;
  2001. int fully_mapped = 1;
  2002. head = create_page_buffers(page, inode, 0);
  2003. blocksize = head->b_size;
  2004. bbits = block_size_bits(blocksize);
  2005. iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
  2006. lblock = (i_size_read(inode)+blocksize-1) >> bbits;
  2007. bh = head;
  2008. nr = 0;
  2009. i = 0;
  2010. do {
  2011. if (buffer_uptodate(bh))
  2012. continue;
  2013. if (!buffer_mapped(bh)) {
  2014. int err = 0;
  2015. fully_mapped = 0;
  2016. if (iblock < lblock) {
  2017. WARN_ON(bh->b_size != blocksize);
  2018. err = get_block(inode, iblock, bh, 0);
  2019. if (err)
  2020. SetPageError(page);
  2021. }
  2022. if (!buffer_mapped(bh)) {
  2023. zero_user(page, i * blocksize, blocksize);
  2024. if (!err)
  2025. set_buffer_uptodate(bh);
  2026. continue;
  2027. }
  2028. /*
  2029. * get_block() might have updated the buffer
  2030. * synchronously
  2031. */
  2032. if (buffer_uptodate(bh))
  2033. continue;
  2034. }
  2035. arr[nr++] = bh;
  2036. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  2037. if (fully_mapped)
  2038. SetPageMappedToDisk(page);
  2039. if (!nr) {
  2040. /*
  2041. * All buffers are uptodate - we can set the page uptodate
  2042. * as well. But not if get_block() returned an error.
  2043. */
  2044. if (!PageError(page))
  2045. SetPageUptodate(page);
  2046. unlock_page(page);
  2047. return 0;
  2048. }
  2049. /* Stage two: lock the buffers */
  2050. for (i = 0; i < nr; i++) {
  2051. bh = arr[i];
  2052. lock_buffer(bh);
  2053. mark_buffer_async_read(bh);
  2054. }
  2055. /*
  2056. * Stage 3: start the IO. Check for uptodateness
  2057. * inside the buffer lock in case another process reading
  2058. * the underlying blockdev brought it uptodate (the sct fix).
  2059. */
  2060. for (i = 0; i < nr; i++) {
  2061. bh = arr[i];
  2062. if (buffer_uptodate(bh))
  2063. end_buffer_async_read(bh, 1);
  2064. else
  2065. submit_bh(REQ_OP_READ, 0, bh);
  2066. }
  2067. return 0;
  2068. }
  2069. EXPORT_SYMBOL(block_read_full_page);
  2070. /* utility function for filesystems that need to do work on expanding
  2071. * truncates. Uses filesystem pagecache writes to allow the filesystem to
  2072. * deal with the hole.
  2073. */
  2074. int generic_cont_expand_simple(struct inode *inode, loff_t size)
  2075. {
  2076. struct address_space *mapping = inode->i_mapping;
  2077. struct page *page;
  2078. void *fsdata;
  2079. int err;
  2080. err = inode_newsize_ok(inode, size);
  2081. if (err)
  2082. goto out;
  2083. err = pagecache_write_begin(NULL, mapping, size, 0,
  2084. AOP_FLAG_CONT_EXPAND, &page, &fsdata);
  2085. if (err)
  2086. goto out;
  2087. err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  2088. BUG_ON(err > 0);
  2089. out:
  2090. return err;
  2091. }
  2092. EXPORT_SYMBOL(generic_cont_expand_simple);
  2093. static int cont_expand_zero(struct file *file, struct address_space *mapping,
  2094. loff_t pos, loff_t *bytes)
  2095. {
  2096. struct inode *inode = mapping->host;
  2097. unsigned int blocksize = i_blocksize(inode);
  2098. struct page *page;
  2099. void *fsdata;
  2100. pgoff_t index, curidx;
  2101. loff_t curpos;
  2102. unsigned zerofrom, offset, len;
  2103. int err = 0;
  2104. index = pos >> PAGE_SHIFT;
  2105. offset = pos & ~PAGE_MASK;
  2106. while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
  2107. zerofrom = curpos & ~PAGE_MASK;
  2108. if (zerofrom & (blocksize-1)) {
  2109. *bytes |= (blocksize-1);
  2110. (*bytes)++;
  2111. }
  2112. len = PAGE_SIZE - zerofrom;
  2113. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2114. &page, &fsdata);
  2115. if (err)
  2116. goto out;
  2117. zero_user(page, zerofrom, len);
  2118. err = pagecache_write_end(file, mapping, curpos, len, len,
  2119. page, fsdata);
  2120. if (err < 0)
  2121. goto out;
  2122. BUG_ON(err != len);
  2123. err = 0;
  2124. balance_dirty_pages_ratelimited(mapping);
  2125. if (unlikely(fatal_signal_pending(current))) {
  2126. err = -EINTR;
  2127. goto out;
  2128. }
  2129. }
  2130. /* page covers the boundary, find the boundary offset */
  2131. if (index == curidx) {
  2132. zerofrom = curpos & ~PAGE_MASK;
  2133. /* if we will expand the thing last block will be filled */
  2134. if (offset <= zerofrom) {
  2135. goto out;
  2136. }
  2137. if (zerofrom & (blocksize-1)) {
  2138. *bytes |= (blocksize-1);
  2139. (*bytes)++;
  2140. }
  2141. len = offset - zerofrom;
  2142. err = pagecache_write_begin(file, mapping, curpos, len, 0,
  2143. &page, &fsdata);
  2144. if (err)
  2145. goto out;
  2146. zero_user(page, zerofrom, len);
  2147. err = pagecache_write_end(file, mapping, curpos, len, len,
  2148. page, fsdata);
  2149. if (err < 0)
  2150. goto out;
  2151. BUG_ON(err != len);
  2152. err = 0;
  2153. }
  2154. out:
  2155. return err;
  2156. }
  2157. /*
  2158. * For moronic filesystems that do not allow holes in file.
  2159. * We may have to extend the file.
  2160. */
  2161. int cont_write_begin(struct file *file, struct address_space *mapping,
  2162. loff_t pos, unsigned len, unsigned flags,
  2163. struct page **pagep, void **fsdata,
  2164. get_block_t *get_block, loff_t *bytes)
  2165. {
  2166. struct inode *inode = mapping->host;
  2167. unsigned int blocksize = i_blocksize(inode);
  2168. unsigned int zerofrom;
  2169. int err;
  2170. err = cont_expand_zero(file, mapping, pos, bytes);
  2171. if (err)
  2172. return err;
  2173. zerofrom = *bytes & ~PAGE_MASK;
  2174. if (pos+len > *bytes && zerofrom & (blocksize-1)) {
  2175. *bytes |= (blocksize-1);
  2176. (*bytes)++;
  2177. }
  2178. return block_write_begin(mapping, pos, len, flags, pagep, get_block);
  2179. }
  2180. EXPORT_SYMBOL(cont_write_begin);
  2181. int block_commit_write(struct page *page, unsigned from, unsigned to)
  2182. {
  2183. struct inode *inode = page->mapping->host;
  2184. __block_commit_write(inode,page,from,to);
  2185. return 0;
  2186. }
  2187. EXPORT_SYMBOL(block_commit_write);
  2188. /*
  2189. * block_page_mkwrite() is not allowed to change the file size as it gets
  2190. * called from a page fault handler when a page is first dirtied. Hence we must
  2191. * be careful to check for EOF conditions here. We set the page up correctly
  2192. * for a written page which means we get ENOSPC checking when writing into
  2193. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2194. * support these features.
  2195. *
  2196. * We are not allowed to take the i_mutex here so we have to play games to
  2197. * protect against truncate races as the page could now be beyond EOF. Because
  2198. * truncate writes the inode size before removing pages, once we have the
  2199. * page lock we can determine safely if the page is beyond EOF. If it is not
  2200. * beyond EOF, then the page is guaranteed safe against truncation until we
  2201. * unlock the page.
  2202. *
  2203. * Direct callers of this function should protect against filesystem freezing
  2204. * using sb_start_pagefault() - sb_end_pagefault() functions.
  2205. */
  2206. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2207. get_block_t get_block)
  2208. {
  2209. struct page *page = vmf->page;
  2210. struct inode *inode = file_inode(vma->vm_file);
  2211. unsigned long end;
  2212. loff_t size;
  2213. int ret;
  2214. lock_page(page);
  2215. size = i_size_read(inode);
  2216. if ((page->mapping != inode->i_mapping) ||
  2217. (page_offset(page) > size)) {
  2218. /* We overload EFAULT to mean page got truncated */
  2219. ret = -EFAULT;
  2220. goto out_unlock;
  2221. }
  2222. /* page is wholly or partially inside EOF */
  2223. if (((page->index + 1) << PAGE_SHIFT) > size)
  2224. end = size & ~PAGE_MASK;
  2225. else
  2226. end = PAGE_SIZE;
  2227. ret = __block_write_begin(page, 0, end, get_block);
  2228. if (!ret)
  2229. ret = block_commit_write(page, 0, end);
  2230. if (unlikely(ret < 0))
  2231. goto out_unlock;
  2232. set_page_dirty(page);
  2233. wait_for_stable_page(page);
  2234. return 0;
  2235. out_unlock:
  2236. unlock_page(page);
  2237. return ret;
  2238. }
  2239. EXPORT_SYMBOL(block_page_mkwrite);
  2240. /*
  2241. * nobh_write_begin()'s prereads are special: the buffer_heads are freed
  2242. * immediately, while under the page lock. So it needs a special end_io
  2243. * handler which does not touch the bh after unlocking it.
  2244. */
  2245. static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
  2246. {
  2247. __end_buffer_read_notouch(bh, uptodate);
  2248. }
  2249. /*
  2250. * Attach the singly-linked list of buffers created by nobh_write_begin, to
  2251. * the page (converting it to circular linked list and taking care of page
  2252. * dirty races).
  2253. */
  2254. static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
  2255. {
  2256. struct buffer_head *bh;
  2257. BUG_ON(!PageLocked(page));
  2258. spin_lock(&page->mapping->private_lock);
  2259. bh = head;
  2260. do {
  2261. if (PageDirty(page))
  2262. set_buffer_dirty(bh);
  2263. if (!bh->b_this_page)
  2264. bh->b_this_page = head;
  2265. bh = bh->b_this_page;
  2266. } while (bh != head);
  2267. attach_page_buffers(page, head);
  2268. spin_unlock(&page->mapping->private_lock);
  2269. }
  2270. /*
  2271. * On entry, the page is fully not uptodate.
  2272. * On exit the page is fully uptodate in the areas outside (from,to)
  2273. * The filesystem needs to handle block truncation upon failure.
  2274. */
  2275. int nobh_write_begin(struct address_space *mapping,
  2276. loff_t pos, unsigned len, unsigned flags,
  2277. struct page **pagep, void **fsdata,
  2278. get_block_t *get_block)
  2279. {
  2280. struct inode *inode = mapping->host;
  2281. const unsigned blkbits = inode->i_blkbits;
  2282. const unsigned blocksize = 1 << blkbits;
  2283. struct buffer_head *head, *bh;
  2284. struct page *page;
  2285. pgoff_t index;
  2286. unsigned from, to;
  2287. unsigned block_in_page;
  2288. unsigned block_start, block_end;
  2289. sector_t block_in_file;
  2290. int nr_reads = 0;
  2291. int ret = 0;
  2292. int is_mapped_to_disk = 1;
  2293. index = pos >> PAGE_SHIFT;
  2294. from = pos & (PAGE_SIZE - 1);
  2295. to = from + len;
  2296. page = grab_cache_page_write_begin(mapping, index, flags);
  2297. if (!page)
  2298. return -ENOMEM;
  2299. *pagep = page;
  2300. *fsdata = NULL;
  2301. if (page_has_buffers(page)) {
  2302. ret = __block_write_begin(page, pos, len, get_block);
  2303. if (unlikely(ret))
  2304. goto out_release;
  2305. return ret;
  2306. }
  2307. if (PageMappedToDisk(page))
  2308. return 0;
  2309. /*
  2310. * Allocate buffers so that we can keep track of state, and potentially
  2311. * attach them to the page if an error occurs. In the common case of
  2312. * no error, they will just be freed again without ever being attached
  2313. * to the page (which is all OK, because we're under the page lock).
  2314. *
  2315. * Be careful: the buffer linked list is a NULL terminated one, rather
  2316. * than the circular one we're used to.
  2317. */
  2318. head = alloc_page_buffers(page, blocksize, false);
  2319. if (!head) {
  2320. ret = -ENOMEM;
  2321. goto out_release;
  2322. }
  2323. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  2324. /*
  2325. * We loop across all blocks in the page, whether or not they are
  2326. * part of the affected region. This is so we can discover if the
  2327. * page is fully mapped-to-disk.
  2328. */
  2329. for (block_start = 0, block_in_page = 0, bh = head;
  2330. block_start < PAGE_SIZE;
  2331. block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
  2332. int create;
  2333. block_end = block_start + blocksize;
  2334. bh->b_state = 0;
  2335. create = 1;
  2336. if (block_start >= to)
  2337. create = 0;
  2338. ret = get_block(inode, block_in_file + block_in_page,
  2339. bh, create);
  2340. if (ret)
  2341. goto failed;
  2342. if (!buffer_mapped(bh))
  2343. is_mapped_to_disk = 0;
  2344. if (buffer_new(bh))
  2345. clean_bdev_bh_alias(bh);
  2346. if (PageUptodate(page)) {
  2347. set_buffer_uptodate(bh);
  2348. continue;
  2349. }
  2350. if (buffer_new(bh) || !buffer_mapped(bh)) {
  2351. zero_user_segments(page, block_start, from,
  2352. to, block_end);
  2353. continue;
  2354. }
  2355. if (buffer_uptodate(bh))
  2356. continue; /* reiserfs does this */
  2357. if (block_start < from || block_end > to) {
  2358. lock_buffer(bh);
  2359. bh->b_end_io = end_buffer_read_nobh;
  2360. submit_bh(REQ_OP_READ, 0, bh);
  2361. nr_reads++;
  2362. }
  2363. }
  2364. if (nr_reads) {
  2365. /*
  2366. * The page is locked, so these buffers are protected from
  2367. * any VM or truncate activity. Hence we don't need to care
  2368. * for the buffer_head refcounts.
  2369. */
  2370. for (bh = head; bh; bh = bh->b_this_page) {
  2371. wait_on_buffer(bh);
  2372. if (!buffer_uptodate(bh))
  2373. ret = -EIO;
  2374. }
  2375. if (ret)
  2376. goto failed;
  2377. }
  2378. if (is_mapped_to_disk)
  2379. SetPageMappedToDisk(page);
  2380. *fsdata = head; /* to be released by nobh_write_end */
  2381. return 0;
  2382. failed:
  2383. BUG_ON(!ret);
  2384. /*
  2385. * Error recovery is a bit difficult. We need to zero out blocks that
  2386. * were newly allocated, and dirty them to ensure they get written out.
  2387. * Buffers need to be attached to the page at this point, otherwise
  2388. * the handling of potential IO errors during writeout would be hard
  2389. * (could try doing synchronous writeout, but what if that fails too?)
  2390. */
  2391. attach_nobh_buffers(page, head);
  2392. page_zero_new_buffers(page, from, to);
  2393. out_release:
  2394. unlock_page(page);
  2395. put_page(page);
  2396. *pagep = NULL;
  2397. return ret;
  2398. }
  2399. EXPORT_SYMBOL(nobh_write_begin);
  2400. int nobh_write_end(struct file *file, struct address_space *mapping,
  2401. loff_t pos, unsigned len, unsigned copied,
  2402. struct page *page, void *fsdata)
  2403. {
  2404. struct inode *inode = page->mapping->host;
  2405. struct buffer_head *head = fsdata;
  2406. struct buffer_head *bh;
  2407. BUG_ON(fsdata != NULL && page_has_buffers(page));
  2408. if (unlikely(copied < len) && head)
  2409. attach_nobh_buffers(page, head);
  2410. if (page_has_buffers(page))
  2411. return generic_write_end(file, mapping, pos, len,
  2412. copied, page, fsdata);
  2413. SetPageUptodate(page);
  2414. set_page_dirty(page);
  2415. if (pos+copied > inode->i_size) {
  2416. i_size_write(inode, pos+copied);
  2417. mark_inode_dirty(inode);
  2418. }
  2419. unlock_page(page);
  2420. put_page(page);
  2421. while (head) {
  2422. bh = head;
  2423. head = head->b_this_page;
  2424. free_buffer_head(bh);
  2425. }
  2426. return copied;
  2427. }
  2428. EXPORT_SYMBOL(nobh_write_end);
  2429. /*
  2430. * nobh_writepage() - based on block_full_write_page() except
  2431. * that it tries to operate without attaching bufferheads to
  2432. * the page.
  2433. */
  2434. int nobh_writepage(struct page *page, get_block_t *get_block,
  2435. struct writeback_control *wbc)
  2436. {
  2437. struct inode * const inode = page->mapping->host;
  2438. loff_t i_size = i_size_read(inode);
  2439. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2440. unsigned offset;
  2441. int ret;
  2442. /* Is the page fully inside i_size? */
  2443. if (page->index < end_index)
  2444. goto out;
  2445. /* Is the page fully outside i_size? (truncate in progress) */
  2446. offset = i_size & (PAGE_SIZE-1);
  2447. if (page->index >= end_index+1 || !offset) {
  2448. /*
  2449. * The page may have dirty, unmapped buffers. For example,
  2450. * they may have been added in ext3_writepage(). Make them
  2451. * freeable here, so the page does not leak.
  2452. */
  2453. #if 0
  2454. /* Not really sure about this - do we need this ? */
  2455. if (page->mapping->a_ops->invalidatepage)
  2456. page->mapping->a_ops->invalidatepage(page, offset);
  2457. #endif
  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. /*
  2627. * The page may have dirty, unmapped buffers. For example,
  2628. * they may have been added in ext3_writepage(). Make them
  2629. * freeable here, so the page does not leak.
  2630. */
  2631. do_invalidatepage(page, 0, PAGE_SIZE);
  2632. unlock_page(page);
  2633. return 0; /* don't care */
  2634. }
  2635. /*
  2636. * The page straddles i_size. It must be zeroed out on each and every
  2637. * writepage invocation because it may be mmapped. "A file is mapped
  2638. * in multiples of the page size. For a file that is not a multiple of
  2639. * the page size, the remaining memory is zeroed when mapped, and
  2640. * writes to that region are not written out to the file."
  2641. */
  2642. zero_user_segment(page, offset, PAGE_SIZE);
  2643. return __block_write_full_page(inode, page, get_block, wbc,
  2644. end_buffer_async_write);
  2645. }
  2646. EXPORT_SYMBOL(block_write_full_page);
  2647. sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
  2648. get_block_t *get_block)
  2649. {
  2650. struct inode *inode = mapping->host;
  2651. struct buffer_head tmp = {
  2652. .b_size = i_blocksize(inode),
  2653. };
  2654. get_block(inode, block, &tmp, 0);
  2655. return tmp.b_blocknr;
  2656. }
  2657. EXPORT_SYMBOL(generic_block_bmap);
  2658. static void end_bio_bh_io_sync(struct bio *bio)
  2659. {
  2660. struct buffer_head *bh = bio->bi_private;
  2661. if (unlikely(bio_flagged(bio, BIO_QUIET)))
  2662. set_bit(BH_Quiet, &bh->b_state);
  2663. bh->b_end_io(bh, !bio->bi_status);
  2664. bio_put(bio);
  2665. }
  2666. /*
  2667. * This allows us to do IO even on the odd last sectors
  2668. * of a device, even if the block size is some multiple
  2669. * of the physical sector size.
  2670. *
  2671. * We'll just truncate the bio to the size of the device,
  2672. * and clear the end of the buffer head manually.
  2673. *
  2674. * Truly out-of-range accesses will turn into actual IO
  2675. * errors, this only handles the "we need to be able to
  2676. * do IO at the final sector" case.
  2677. */
  2678. void guard_bio_eod(int op, struct bio *bio)
  2679. {
  2680. sector_t maxsector;
  2681. struct bio_vec *bvec = bio_last_bvec_all(bio);
  2682. unsigned truncated_bytes;
  2683. struct hd_struct *part;
  2684. rcu_read_lock();
  2685. part = __disk_get_part(bio->bi_disk, bio->bi_partno);
  2686. if (part)
  2687. maxsector = part_nr_sects_read(part);
  2688. else
  2689. maxsector = get_capacity(bio->bi_disk);
  2690. rcu_read_unlock();
  2691. if (!maxsector)
  2692. return;
  2693. /*
  2694. * If the *whole* IO is past the end of the device,
  2695. * let it through, and the IO layer will turn it into
  2696. * an EIO.
  2697. */
  2698. if (unlikely(bio->bi_iter.bi_sector >= maxsector))
  2699. return;
  2700. maxsector -= bio->bi_iter.bi_sector;
  2701. if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
  2702. return;
  2703. /* Uhhuh. We've got a bio that straddles the device size! */
  2704. truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
  2705. /*
  2706. * The bio contains more than one segment which spans EOD, just return
  2707. * and let IO layer turn it into an EIO
  2708. */
  2709. if (truncated_bytes > bvec->bv_len)
  2710. return;
  2711. /* Truncate the bio.. */
  2712. bio->bi_iter.bi_size -= truncated_bytes;
  2713. bvec->bv_len -= truncated_bytes;
  2714. /* ..and clear the end of the buffer for reads */
  2715. if (op == REQ_OP_READ) {
  2716. zero_user(bvec->bv_page, bvec->bv_offset + bvec->bv_len,
  2717. truncated_bytes);
  2718. }
  2719. }
  2720. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  2721. enum rw_hint write_hint, struct writeback_control *wbc)
  2722. {
  2723. struct bio *bio;
  2724. BUG_ON(!buffer_locked(bh));
  2725. BUG_ON(!buffer_mapped(bh));
  2726. BUG_ON(!bh->b_end_io);
  2727. BUG_ON(buffer_delay(bh));
  2728. BUG_ON(buffer_unwritten(bh));
  2729. /*
  2730. * Only clear out a write error when rewriting
  2731. */
  2732. if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
  2733. clear_buffer_write_io_error(bh);
  2734. /*
  2735. * from here on down, it's all bio -- do the initial mapping,
  2736. * submit_bio -> generic_make_request may further map this bio around
  2737. */
  2738. bio = bio_alloc(GFP_NOIO, 1);
  2739. if (wbc) {
  2740. wbc_init_bio(wbc, bio);
  2741. wbc_account_io(wbc, bh->b_page, bh->b_size);
  2742. }
  2743. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  2744. bio_set_dev(bio, bh->b_bdev);
  2745. bio->bi_write_hint = write_hint;
  2746. bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  2747. BUG_ON(bio->bi_iter.bi_size != bh->b_size);
  2748. bio->bi_end_io = end_bio_bh_io_sync;
  2749. bio->bi_private = bh;
  2750. /* Take care of bh's that straddle the end of the device */
  2751. guard_bio_eod(op, bio);
  2752. if (buffer_meta(bh))
  2753. op_flags |= REQ_META;
  2754. if (buffer_prio(bh))
  2755. op_flags |= REQ_PRIO;
  2756. bio_set_op_attrs(bio, op, op_flags);
  2757. submit_bio(bio);
  2758. return 0;
  2759. }
  2760. int submit_bh(int op, int op_flags, struct buffer_head *bh)
  2761. {
  2762. return submit_bh_wbc(op, op_flags, bh, 0, NULL);
  2763. }
  2764. EXPORT_SYMBOL(submit_bh);
  2765. /**
  2766. * ll_rw_block: low-level access to block devices (DEPRECATED)
  2767. * @op: whether to %READ or %WRITE
  2768. * @op_flags: req_flag_bits
  2769. * @nr: number of &struct buffer_heads in the array
  2770. * @bhs: array of pointers to &struct buffer_head
  2771. *
  2772. * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
  2773. * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
  2774. * @op_flags contains flags modifying the detailed I/O behavior, most notably
  2775. * %REQ_RAHEAD.
  2776. *
  2777. * This function drops any buffer that it cannot get a lock on (with the
  2778. * BH_Lock state bit), any buffer that appears to be clean when doing a write
  2779. * request, and any buffer that appears to be up-to-date when doing read
  2780. * request. Further it marks as clean buffers that are processed for
  2781. * writing (the buffer cache won't assume that they are actually clean
  2782. * until the buffer gets unlocked).
  2783. *
  2784. * ll_rw_block sets b_end_io to simple completion handler that marks
  2785. * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
  2786. * any waiters.
  2787. *
  2788. * All of the buffers must be for the same device, and must also be a
  2789. * multiple of the current approved size for the device.
  2790. */
  2791. void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
  2792. {
  2793. int i;
  2794. for (i = 0; i < nr; i++) {
  2795. struct buffer_head *bh = bhs[i];
  2796. if (!trylock_buffer(bh))
  2797. continue;
  2798. if (op == WRITE) {
  2799. if (test_clear_buffer_dirty(bh)) {
  2800. bh->b_end_io = end_buffer_write_sync;
  2801. get_bh(bh);
  2802. submit_bh(op, op_flags, bh);
  2803. continue;
  2804. }
  2805. } else {
  2806. if (!buffer_uptodate(bh)) {
  2807. bh->b_end_io = end_buffer_read_sync;
  2808. get_bh(bh);
  2809. submit_bh(op, op_flags, bh);
  2810. continue;
  2811. }
  2812. }
  2813. unlock_buffer(bh);
  2814. }
  2815. }
  2816. EXPORT_SYMBOL(ll_rw_block);
  2817. void write_dirty_buffer(struct buffer_head *bh, int op_flags)
  2818. {
  2819. lock_buffer(bh);
  2820. if (!test_clear_buffer_dirty(bh)) {
  2821. unlock_buffer(bh);
  2822. return;
  2823. }
  2824. bh->b_end_io = end_buffer_write_sync;
  2825. get_bh(bh);
  2826. submit_bh(REQ_OP_WRITE, op_flags, bh);
  2827. }
  2828. EXPORT_SYMBOL(write_dirty_buffer);
  2829. /*
  2830. * For a data-integrity writeout, we need to wait upon any in-progress I/O
  2831. * and then start new I/O and then wait upon it. The caller must have a ref on
  2832. * the buffer_head.
  2833. */
  2834. int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
  2835. {
  2836. int ret = 0;
  2837. WARN_ON(atomic_read(&bh->b_count) < 1);
  2838. lock_buffer(bh);
  2839. if (test_clear_buffer_dirty(bh)) {
  2840. get_bh(bh);
  2841. bh->b_end_io = end_buffer_write_sync;
  2842. ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
  2843. wait_on_buffer(bh);
  2844. if (!ret && !buffer_uptodate(bh))
  2845. ret = -EIO;
  2846. } else {
  2847. unlock_buffer(bh);
  2848. }
  2849. return ret;
  2850. }
  2851. EXPORT_SYMBOL(__sync_dirty_buffer);
  2852. int sync_dirty_buffer(struct buffer_head *bh)
  2853. {
  2854. return __sync_dirty_buffer(bh, REQ_SYNC);
  2855. }
  2856. EXPORT_SYMBOL(sync_dirty_buffer);
  2857. /*
  2858. * try_to_free_buffers() checks if all the buffers on this particular page
  2859. * are unused, and releases them if so.
  2860. *
  2861. * Exclusion against try_to_free_buffers may be obtained by either
  2862. * locking the page or by holding its mapping's private_lock.
  2863. *
  2864. * If the page is dirty but all the buffers are clean then we need to
  2865. * be sure to mark the page clean as well. This is because the page
  2866. * may be against a block device, and a later reattachment of buffers
  2867. * to a dirty page will set *all* buffers dirty. Which would corrupt
  2868. * filesystem data on the same device.
  2869. *
  2870. * The same applies to regular filesystem pages: if all the buffers are
  2871. * clean then we set the page clean and proceed. To do that, we require
  2872. * total exclusion from __set_page_dirty_buffers(). That is obtained with
  2873. * private_lock.
  2874. *
  2875. * try_to_free_buffers() is non-blocking.
  2876. */
  2877. static inline int buffer_busy(struct buffer_head *bh)
  2878. {
  2879. return atomic_read(&bh->b_count) |
  2880. (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
  2881. }
  2882. static int
  2883. drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
  2884. {
  2885. struct buffer_head *head = page_buffers(page);
  2886. struct buffer_head *bh;
  2887. bh = head;
  2888. do {
  2889. if (buffer_busy(bh))
  2890. goto failed;
  2891. bh = bh->b_this_page;
  2892. } while (bh != head);
  2893. do {
  2894. struct buffer_head *next = bh->b_this_page;
  2895. if (bh->b_assoc_map)
  2896. __remove_assoc_queue(bh);
  2897. bh = next;
  2898. } while (bh != head);
  2899. *buffers_to_free = head;
  2900. __clear_page_buffers(page);
  2901. return 1;
  2902. failed:
  2903. return 0;
  2904. }
  2905. int try_to_free_buffers(struct page *page)
  2906. {
  2907. struct address_space * const mapping = page->mapping;
  2908. struct buffer_head *buffers_to_free = NULL;
  2909. int ret = 0;
  2910. BUG_ON(!PageLocked(page));
  2911. if (PageWriteback(page))
  2912. return 0;
  2913. if (mapping == NULL) { /* can this still happen? */
  2914. ret = drop_buffers(page, &buffers_to_free);
  2915. goto out;
  2916. }
  2917. spin_lock(&mapping->private_lock);
  2918. ret = drop_buffers(page, &buffers_to_free);
  2919. /*
  2920. * If the filesystem writes its buffers by hand (eg ext3)
  2921. * then we can have clean buffers against a dirty page. We
  2922. * clean the page here; otherwise the VM will never notice
  2923. * that the filesystem did any IO at all.
  2924. *
  2925. * Also, during truncate, discard_buffer will have marked all
  2926. * the page's buffers clean. We discover that here and clean
  2927. * the page also.
  2928. *
  2929. * private_lock must be held over this entire operation in order
  2930. * to synchronise against __set_page_dirty_buffers and prevent the
  2931. * dirty bit from being lost.
  2932. */
  2933. if (ret)
  2934. cancel_dirty_page(page);
  2935. spin_unlock(&mapping->private_lock);
  2936. out:
  2937. if (buffers_to_free) {
  2938. struct buffer_head *bh = buffers_to_free;
  2939. do {
  2940. struct buffer_head *next = bh->b_this_page;
  2941. free_buffer_head(bh);
  2942. bh = next;
  2943. } while (bh != buffers_to_free);
  2944. }
  2945. return ret;
  2946. }
  2947. EXPORT_SYMBOL(try_to_free_buffers);
  2948. /*
  2949. * There are no bdflush tunables left. But distributions are
  2950. * still running obsolete flush daemons, so we terminate them here.
  2951. *
  2952. * Use of bdflush() is deprecated and will be removed in a future kernel.
  2953. * The `flush-X' kernel threads fully replace bdflush daemons and this call.
  2954. */
  2955. SYSCALL_DEFINE2(bdflush, int, func, long, data)
  2956. {
  2957. static int msg_count;
  2958. if (!capable(CAP_SYS_ADMIN))
  2959. return -EPERM;
  2960. if (msg_count < 5) {
  2961. msg_count++;
  2962. printk(KERN_INFO
  2963. "warning: process `%s' used the obsolete bdflush"
  2964. " system call\n", current->comm);
  2965. printk(KERN_INFO "Fix your initscripts?\n");
  2966. }
  2967. if (func == 1)
  2968. do_exit(0);
  2969. return 0;
  2970. }
  2971. /*
  2972. * Buffer-head allocation
  2973. */
  2974. static struct kmem_cache *bh_cachep __read_mostly;
  2975. /*
  2976. * Once the number of bh's in the machine exceeds this level, we start
  2977. * stripping them in writeback.
  2978. */
  2979. static unsigned long max_buffer_heads;
  2980. int buffer_heads_over_limit;
  2981. struct bh_accounting {
  2982. int nr; /* Number of live bh's */
  2983. int ratelimit; /* Limit cacheline bouncing */
  2984. };
  2985. static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
  2986. static void recalc_bh_state(void)
  2987. {
  2988. int i;
  2989. int tot = 0;
  2990. if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
  2991. return;
  2992. __this_cpu_write(bh_accounting.ratelimit, 0);
  2993. for_each_online_cpu(i)
  2994. tot += per_cpu(bh_accounting, i).nr;
  2995. buffer_heads_over_limit = (tot > max_buffer_heads);
  2996. }
  2997. struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  2998. {
  2999. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  3000. if (ret) {
  3001. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  3002. preempt_disable();
  3003. __this_cpu_inc(bh_accounting.nr);
  3004. recalc_bh_state();
  3005. preempt_enable();
  3006. }
  3007. return ret;
  3008. }
  3009. EXPORT_SYMBOL(alloc_buffer_head);
  3010. void free_buffer_head(struct buffer_head *bh)
  3011. {
  3012. BUG_ON(!list_empty(&bh->b_assoc_buffers));
  3013. kmem_cache_free(bh_cachep, bh);
  3014. preempt_disable();
  3015. __this_cpu_dec(bh_accounting.nr);
  3016. recalc_bh_state();
  3017. preempt_enable();
  3018. }
  3019. EXPORT_SYMBOL(free_buffer_head);
  3020. static int buffer_exit_cpu_dead(unsigned int cpu)
  3021. {
  3022. int i;
  3023. struct bh_lru *b = &per_cpu(bh_lrus, cpu);
  3024. for (i = 0; i < BH_LRU_SIZE; i++) {
  3025. brelse(b->bhs[i]);
  3026. b->bhs[i] = NULL;
  3027. }
  3028. this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
  3029. per_cpu(bh_accounting, cpu).nr = 0;
  3030. return 0;
  3031. }
  3032. /**
  3033. * bh_uptodate_or_lock - Test whether the buffer is uptodate
  3034. * @bh: struct buffer_head
  3035. *
  3036. * Return true if the buffer is up-to-date and false,
  3037. * with the buffer locked, if not.
  3038. */
  3039. int bh_uptodate_or_lock(struct buffer_head *bh)
  3040. {
  3041. if (!buffer_uptodate(bh)) {
  3042. lock_buffer(bh);
  3043. if (!buffer_uptodate(bh))
  3044. return 0;
  3045. unlock_buffer(bh);
  3046. }
  3047. return 1;
  3048. }
  3049. EXPORT_SYMBOL(bh_uptodate_or_lock);
  3050. /**
  3051. * bh_submit_read - Submit a locked buffer for reading
  3052. * @bh: struct buffer_head
  3053. *
  3054. * Returns zero on success and -EIO on error.
  3055. */
  3056. int bh_submit_read(struct buffer_head *bh)
  3057. {
  3058. BUG_ON(!buffer_locked(bh));
  3059. if (buffer_uptodate(bh)) {
  3060. unlock_buffer(bh);
  3061. return 0;
  3062. }
  3063. get_bh(bh);
  3064. bh->b_end_io = end_buffer_read_sync;
  3065. submit_bh(REQ_OP_READ, 0, bh);
  3066. wait_on_buffer(bh);
  3067. if (buffer_uptodate(bh))
  3068. return 0;
  3069. return -EIO;
  3070. }
  3071. EXPORT_SYMBOL(bh_submit_read);
  3072. void __init buffer_init(void)
  3073. {
  3074. unsigned long nrpages;
  3075. int ret;
  3076. bh_cachep = kmem_cache_create("buffer_head",
  3077. sizeof(struct buffer_head), 0,
  3078. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  3079. SLAB_MEM_SPREAD),
  3080. NULL);
  3081. /*
  3082. * Limit the bh occupancy to 10% of ZONE_NORMAL
  3083. */
  3084. nrpages = (nr_free_buffer_pages() * 10) / 100;
  3085. max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
  3086. ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
  3087. NULL, buffer_exit_cpu_dead);
  3088. WARN_ON(ret < 0);
  3089. }