buffer.c 89 KB

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