buffer.c 90 KB

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