buffer.c 87 KB

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