buffer.c 91 KB

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