buffer.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370
  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. init_buffer(bh, NULL, NULL);
  788. }
  789. return head;
  790. /*
  791. * In case anything failed, we just free everything we got.
  792. */
  793. no_grow:
  794. if (head) {
  795. do {
  796. bh = head;
  797. head = head->b_this_page;
  798. free_buffer_head(bh);
  799. } while (head);
  800. }
  801. /*
  802. * Return failure for non-async IO requests. Async IO requests
  803. * are not allowed to fail, so we have to wait until buffer heads
  804. * become available. But we don't want tasks sleeping with
  805. * partially complete buffers, so all were released above.
  806. */
  807. if (!retry)
  808. return NULL;
  809. /* We're _really_ low on memory. Now we just
  810. * wait for old buffer heads to become free due to
  811. * finishing IO. Since this is an async request and
  812. * the reserve list is empty, we're sure there are
  813. * async buffer heads in use.
  814. */
  815. free_more_memory();
  816. goto try_again;
  817. }
  818. EXPORT_SYMBOL_GPL(alloc_page_buffers);
  819. static inline void
  820. link_dev_buffers(struct page *page, struct buffer_head *head)
  821. {
  822. struct buffer_head *bh, *tail;
  823. bh = head;
  824. do {
  825. tail = bh;
  826. bh = bh->b_this_page;
  827. } while (bh);
  828. tail->b_this_page = head;
  829. attach_page_buffers(page, head);
  830. }
  831. static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
  832. {
  833. sector_t retval = ~((sector_t)0);
  834. loff_t sz = i_size_read(bdev->bd_inode);
  835. if (sz) {
  836. unsigned int sizebits = blksize_bits(size);
  837. retval = (sz >> sizebits);
  838. }
  839. return retval;
  840. }
  841. /*
  842. * Initialise the state of a blockdev page's buffers.
  843. */
  844. static sector_t
  845. init_page_buffers(struct page *page, struct block_device *bdev,
  846. sector_t block, int size)
  847. {
  848. struct buffer_head *head = page_buffers(page);
  849. struct buffer_head *bh = head;
  850. int uptodate = PageUptodate(page);
  851. sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
  852. do {
  853. if (!buffer_mapped(bh)) {
  854. init_buffer(bh, NULL, NULL);
  855. bh->b_bdev = bdev;
  856. bh->b_blocknr = block;
  857. if (uptodate)
  858. set_buffer_uptodate(bh);
  859. if (block < end_block)
  860. set_buffer_mapped(bh);
  861. }
  862. block++;
  863. bh = bh->b_this_page;
  864. } while (bh != head);
  865. /*
  866. * Caller needs to validate requested block against end of device.
  867. */
  868. return end_block;
  869. }
  870. /*
  871. * Create the page-cache page that contains the requested block.
  872. *
  873. * This is used purely for blockdev mappings.
  874. */
  875. static int
  876. grow_dev_page(struct block_device *bdev, sector_t block,
  877. pgoff_t index, int size, int sizebits)
  878. {
  879. struct inode *inode = bdev->bd_inode;
  880. struct page *page;
  881. struct buffer_head *bh;
  882. sector_t end_block;
  883. int ret = 0; /* Will call free_more_memory() */
  884. page = find_or_create_page(inode->i_mapping, index,
  885. (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
  886. if (!page)
  887. return ret;
  888. BUG_ON(!PageLocked(page));
  889. if (page_has_buffers(page)) {
  890. bh = page_buffers(page);
  891. if (bh->b_size == size) {
  892. end_block = init_page_buffers(page, bdev,
  893. index << sizebits, size);
  894. goto done;
  895. }
  896. if (!try_to_free_buffers(page))
  897. goto failed;
  898. }
  899. /*
  900. * Allocate some buffers for this page
  901. */
  902. bh = alloc_page_buffers(page, size, 0);
  903. if (!bh)
  904. goto failed;
  905. /*
  906. * Link the page to the buffers and initialise them. Take the
  907. * lock to be atomic wrt __find_get_block(), which does not
  908. * run under the page lock.
  909. */
  910. spin_lock(&inode->i_mapping->private_lock);
  911. link_dev_buffers(page, bh);
  912. end_block = init_page_buffers(page, bdev, index << sizebits, size);
  913. spin_unlock(&inode->i_mapping->private_lock);
  914. done:
  915. ret = (block < end_block) ? 1 : -ENXIO;
  916. failed:
  917. unlock_page(page);
  918. page_cache_release(page);
  919. return ret;
  920. }
  921. /*
  922. * Create buffers for the specified block device block's page. If
  923. * that page was dirty, the buffers are set dirty also.
  924. */
  925. static int
  926. grow_buffers(struct block_device *bdev, sector_t block, int size)
  927. {
  928. pgoff_t index;
  929. int sizebits;
  930. sizebits = -1;
  931. do {
  932. sizebits++;
  933. } while ((size << sizebits) < PAGE_SIZE);
  934. index = block >> sizebits;
  935. /*
  936. * Check for a block which wants to lie outside our maximum possible
  937. * pagecache index. (this comparison is done using sector_t types).
  938. */
  939. if (unlikely(index != block >> sizebits)) {
  940. char b[BDEVNAME_SIZE];
  941. printk(KERN_ERR "%s: requested out-of-range block %llu for "
  942. "device %s\n",
  943. __func__, (unsigned long long)block,
  944. bdevname(bdev, b));
  945. return -EIO;
  946. }
  947. /* Create a page with the proper size buffers.. */
  948. return grow_dev_page(bdev, block, index, size, sizebits);
  949. }
  950. static struct buffer_head *
  951. __getblk_slow(struct block_device *bdev, sector_t block, int size)
  952. {
  953. /* Size must be multiple of hard sectorsize */
  954. if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
  955. (size < 512 || size > PAGE_SIZE))) {
  956. printk(KERN_ERR "getblk(): invalid block size %d requested\n",
  957. size);
  958. printk(KERN_ERR "logical block size: %d\n",
  959. bdev_logical_block_size(bdev));
  960. dump_stack();
  961. return NULL;
  962. }
  963. for (;;) {
  964. struct buffer_head *bh;
  965. int ret;
  966. bh = __find_get_block(bdev, block, size);
  967. if (bh)
  968. return bh;
  969. ret = grow_buffers(bdev, block, size);
  970. if (ret < 0)
  971. return NULL;
  972. if (ret == 0)
  973. free_more_memory();
  974. }
  975. }
  976. /*
  977. * The relationship between dirty buffers and dirty pages:
  978. *
  979. * Whenever a page has any dirty buffers, the page's dirty bit is set, and
  980. * the page is tagged dirty in its radix tree.
  981. *
  982. * At all times, the dirtiness of the buffers represents the dirtiness of
  983. * subsections of the page. If the page has buffers, the page dirty bit is
  984. * merely a hint about the true dirty state.
  985. *
  986. * When a page is set dirty in its entirety, all its buffers are marked dirty
  987. * (if the page has buffers).
  988. *
  989. * When a buffer is marked dirty, its page is dirtied, but the page's other
  990. * buffers are not.
  991. *
  992. * Also. When blockdev buffers are explicitly read with bread(), they
  993. * individually become uptodate. But their backing page remains not
  994. * uptodate - even if all of its buffers are uptodate. A subsequent
  995. * block_read_full_page() against that page will discover all the uptodate
  996. * buffers, will set the page uptodate and will perform no I/O.
  997. */
  998. /**
  999. * mark_buffer_dirty - mark a buffer_head as needing writeout
  1000. * @bh: the buffer_head to mark dirty
  1001. *
  1002. * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
  1003. * backing page dirty, then tag the page as dirty in its address_space's radix
  1004. * tree and then attach the address_space's inode to its superblock's dirty
  1005. * inode list.
  1006. *
  1007. * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
  1008. * mapping->tree_lock and mapping->host->i_lock.
  1009. */
  1010. void mark_buffer_dirty(struct buffer_head *bh)
  1011. {
  1012. WARN_ON_ONCE(!buffer_uptodate(bh));
  1013. trace_block_dirty_buffer(bh);
  1014. /*
  1015. * Very *carefully* optimize the it-is-already-dirty case.
  1016. *
  1017. * Don't let the final "is it dirty" escape to before we
  1018. * perhaps modified the buffer.
  1019. */
  1020. if (buffer_dirty(bh)) {
  1021. smp_mb();
  1022. if (buffer_dirty(bh))
  1023. return;
  1024. }
  1025. if (!test_set_buffer_dirty(bh)) {
  1026. struct page *page = bh->b_page;
  1027. if (!TestSetPageDirty(page)) {
  1028. struct address_space *mapping = page_mapping(page);
  1029. if (mapping)
  1030. __set_page_dirty(page, mapping, 0);
  1031. }
  1032. }
  1033. }
  1034. EXPORT_SYMBOL(mark_buffer_dirty);
  1035. /*
  1036. * Decrement a buffer_head's reference count. If all buffers against a page
  1037. * have zero reference count, are clean and unlocked, and if the page is clean
  1038. * and unlocked then try_to_free_buffers() may strip the buffers from the page
  1039. * in preparation for freeing it (sometimes, rarely, buffers are removed from
  1040. * a page but it ends up not being freed, and buffers may later be reattached).
  1041. */
  1042. void __brelse(struct buffer_head * buf)
  1043. {
  1044. if (atomic_read(&buf->b_count)) {
  1045. put_bh(buf);
  1046. return;
  1047. }
  1048. WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
  1049. }
  1050. EXPORT_SYMBOL(__brelse);
  1051. /*
  1052. * bforget() is like brelse(), except it discards any
  1053. * potentially dirty data.
  1054. */
  1055. void __bforget(struct buffer_head *bh)
  1056. {
  1057. clear_buffer_dirty(bh);
  1058. if (bh->b_assoc_map) {
  1059. struct address_space *buffer_mapping = bh->b_page->mapping;
  1060. spin_lock(&buffer_mapping->private_lock);
  1061. list_del_init(&bh->b_assoc_buffers);
  1062. bh->b_assoc_map = NULL;
  1063. spin_unlock(&buffer_mapping->private_lock);
  1064. }
  1065. __brelse(bh);
  1066. }
  1067. EXPORT_SYMBOL(__bforget);
  1068. static struct buffer_head *__bread_slow(struct buffer_head *bh)
  1069. {
  1070. lock_buffer(bh);
  1071. if (buffer_uptodate(bh)) {
  1072. unlock_buffer(bh);
  1073. return bh;
  1074. } else {
  1075. get_bh(bh);
  1076. bh->b_end_io = end_buffer_read_sync;
  1077. submit_bh(READ, bh);
  1078. wait_on_buffer(bh);
  1079. if (buffer_uptodate(bh))
  1080. return bh;
  1081. }
  1082. brelse(bh);
  1083. return NULL;
  1084. }
  1085. /*
  1086. * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
  1087. * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
  1088. * refcount elevated by one when they're in an LRU. A buffer can only appear
  1089. * once in a particular CPU's LRU. A single buffer can be present in multiple
  1090. * CPU's LRUs at the same time.
  1091. *
  1092. * This is a transparent caching front-end to sb_bread(), sb_getblk() and
  1093. * sb_find_get_block().
  1094. *
  1095. * The LRUs themselves only need locking against invalidate_bh_lrus. We use
  1096. * a local interrupt disable for that.
  1097. */
  1098. #define BH_LRU_SIZE 8
  1099. struct bh_lru {
  1100. struct buffer_head *bhs[BH_LRU_SIZE];
  1101. };
  1102. static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
  1103. #ifdef CONFIG_SMP
  1104. #define bh_lru_lock() local_irq_disable()
  1105. #define bh_lru_unlock() local_irq_enable()
  1106. #else
  1107. #define bh_lru_lock() preempt_disable()
  1108. #define bh_lru_unlock() preempt_enable()
  1109. #endif
  1110. static inline void check_irqs_on(void)
  1111. {
  1112. #ifdef irqs_disabled
  1113. BUG_ON(irqs_disabled());
  1114. #endif
  1115. }
  1116. /*
  1117. * The LRU management algorithm is dopey-but-simple. Sorry.
  1118. */
  1119. static void bh_lru_install(struct buffer_head *bh)
  1120. {
  1121. struct buffer_head *evictee = NULL;
  1122. check_irqs_on();
  1123. bh_lru_lock();
  1124. if (__this_cpu_read(bh_lrus.bhs[0]) != bh) {
  1125. struct buffer_head *bhs[BH_LRU_SIZE];
  1126. int in;
  1127. int out = 0;
  1128. get_bh(bh);
  1129. bhs[out++] = bh;
  1130. for (in = 0; in < BH_LRU_SIZE; in++) {
  1131. struct buffer_head *bh2 =
  1132. __this_cpu_read(bh_lrus.bhs[in]);
  1133. if (bh2 == bh) {
  1134. __brelse(bh2);
  1135. } else {
  1136. if (out >= BH_LRU_SIZE) {
  1137. BUG_ON(evictee != NULL);
  1138. evictee = bh2;
  1139. } else {
  1140. bhs[out++] = bh2;
  1141. }
  1142. }
  1143. }
  1144. while (out < BH_LRU_SIZE)
  1145. bhs[out++] = NULL;
  1146. memcpy(__this_cpu_ptr(&bh_lrus.bhs), bhs, sizeof(bhs));
  1147. }
  1148. bh_lru_unlock();
  1149. if (evictee)
  1150. __brelse(evictee);
  1151. }
  1152. /*
  1153. * Look up the bh in this cpu's LRU. If it's there, move it to the head.
  1154. */
  1155. static struct buffer_head *
  1156. lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
  1157. {
  1158. struct buffer_head *ret = NULL;
  1159. unsigned int i;
  1160. check_irqs_on();
  1161. bh_lru_lock();
  1162. for (i = 0; i < BH_LRU_SIZE; i++) {
  1163. struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
  1164. if (bh && bh->b_bdev == bdev &&
  1165. bh->b_blocknr == block && bh->b_size == size) {
  1166. if (i) {
  1167. while (i) {
  1168. __this_cpu_write(bh_lrus.bhs[i],
  1169. __this_cpu_read(bh_lrus.bhs[i - 1]));
  1170. i--;
  1171. }
  1172. __this_cpu_write(bh_lrus.bhs[0], bh);
  1173. }
  1174. get_bh(bh);
  1175. ret = bh;
  1176. break;
  1177. }
  1178. }
  1179. bh_lru_unlock();
  1180. return ret;
  1181. }
  1182. /*
  1183. * Perform a pagecache lookup for the matching buffer. If it's there, refresh
  1184. * it in the LRU and mark it as accessed. If it is not present then return
  1185. * NULL
  1186. */
  1187. struct buffer_head *
  1188. __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
  1189. {
  1190. struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
  1191. if (bh == NULL) {
  1192. bh = __find_get_block_slow(bdev, block);
  1193. if (bh)
  1194. bh_lru_install(bh);
  1195. }
  1196. if (bh)
  1197. touch_buffer(bh);
  1198. return bh;
  1199. }
  1200. EXPORT_SYMBOL(__find_get_block);
  1201. /*
  1202. * __getblk will locate (and, if necessary, create) the buffer_head
  1203. * which corresponds to the passed block_device, block and size. The
  1204. * returned buffer has its reference count incremented.
  1205. *
  1206. * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
  1207. * attempt is failing. FIXME, perhaps?
  1208. */
  1209. struct buffer_head *
  1210. __getblk(struct block_device *bdev, sector_t block, unsigned size)
  1211. {
  1212. struct buffer_head *bh = __find_get_block(bdev, block, size);
  1213. might_sleep();
  1214. if (bh == NULL)
  1215. bh = __getblk_slow(bdev, block, size);
  1216. return bh;
  1217. }
  1218. EXPORT_SYMBOL(__getblk);
  1219. /*
  1220. * Do async read-ahead on a buffer..
  1221. */
  1222. void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
  1223. {
  1224. struct buffer_head *bh = __getblk(bdev, block, size);
  1225. if (likely(bh)) {
  1226. ll_rw_block(READA, 1, &bh);
  1227. brelse(bh);
  1228. }
  1229. }
  1230. EXPORT_SYMBOL(__breadahead);
  1231. /**
  1232. * __bread() - reads a specified block and returns the bh
  1233. * @bdev: the block_device to read from
  1234. * @block: number of block
  1235. * @size: size (in bytes) to read
  1236. *
  1237. * Reads a specified block, and returns buffer head that contains it.
  1238. * It returns NULL if the block was unreadable.
  1239. */
  1240. struct buffer_head *
  1241. __bread(struct block_device *bdev, sector_t block, unsigned size)
  1242. {
  1243. struct buffer_head *bh = __getblk(bdev, block, size);
  1244. if (likely(bh) && !buffer_uptodate(bh))
  1245. bh = __bread_slow(bh);
  1246. return bh;
  1247. }
  1248. EXPORT_SYMBOL(__bread);
  1249. /*
  1250. * invalidate_bh_lrus() is called rarely - but not only at unmount.
  1251. * This doesn't race because it runs in each cpu either in irq
  1252. * or with preempt disabled.
  1253. */
  1254. static void invalidate_bh_lru(void *arg)
  1255. {
  1256. struct bh_lru *b = &get_cpu_var(bh_lrus);
  1257. int i;
  1258. for (i = 0; i < BH_LRU_SIZE; i++) {
  1259. brelse(b->bhs[i]);
  1260. b->bhs[i] = NULL;
  1261. }
  1262. put_cpu_var(bh_lrus);
  1263. }
  1264. static bool has_bh_in_lru(int cpu, void *dummy)
  1265. {
  1266. struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
  1267. int i;
  1268. for (i = 0; i < BH_LRU_SIZE; i++) {
  1269. if (b->bhs[i])
  1270. return 1;
  1271. }
  1272. return 0;
  1273. }
  1274. void invalidate_bh_lrus(void)
  1275. {
  1276. on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
  1277. }
  1278. EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
  1279. void set_bh_page(struct buffer_head *bh,
  1280. struct page *page, unsigned long offset)
  1281. {
  1282. bh->b_page = page;
  1283. BUG_ON(offset >= PAGE_SIZE);
  1284. if (PageHighMem(page))
  1285. /*
  1286. * This catches illegal uses and preserves the offset:
  1287. */
  1288. bh->b_data = (char *)(0 + offset);
  1289. else
  1290. bh->b_data = page_address(page) + offset;
  1291. }
  1292. EXPORT_SYMBOL(set_bh_page);
  1293. /*
  1294. * Called when truncating a buffer on a page completely.
  1295. */
  1296. static void discard_buffer(struct buffer_head * bh)
  1297. {
  1298. lock_buffer(bh);
  1299. clear_buffer_dirty(bh);
  1300. bh->b_bdev = NULL;
  1301. clear_buffer_mapped(bh);
  1302. clear_buffer_req(bh);
  1303. clear_buffer_new(bh);
  1304. clear_buffer_delay(bh);
  1305. clear_buffer_unwritten(bh);
  1306. unlock_buffer(bh);
  1307. }
  1308. /**
  1309. * block_invalidatepage - invalidate part or all of a buffer-backed page
  1310. *
  1311. * @page: the page which is affected
  1312. * @offset: the index of the truncation point
  1313. *
  1314. * block_invalidatepage() is called when all or part of the page has become
  1315. * invalidated by a truncate operation.
  1316. *
  1317. * block_invalidatepage() does not have to release all buffers, but it must
  1318. * ensure that no dirty buffer is left outside @offset and that no I/O
  1319. * is underway against any of the blocks which are outside the truncation
  1320. * point. Because the caller is about to free (and possibly reuse) those
  1321. * blocks on-disk.
  1322. */
  1323. void block_invalidatepage(struct page *page, unsigned long offset)
  1324. {
  1325. struct buffer_head *head, *bh, *next;
  1326. unsigned int curr_off = 0;
  1327. BUG_ON(!PageLocked(page));
  1328. if (!page_has_buffers(page))
  1329. goto out;
  1330. head = page_buffers(page);
  1331. bh = head;
  1332. do {
  1333. unsigned int next_off = curr_off + bh->b_size;
  1334. next = bh->b_this_page;
  1335. /*
  1336. * is this block fully invalidated?
  1337. */
  1338. if (offset <= curr_off)
  1339. discard_buffer(bh);
  1340. curr_off = next_off;
  1341. bh = next;
  1342. } while (bh != head);
  1343. /*
  1344. * We release buffers only if the entire page is being invalidated.
  1345. * The get_block cached value has been unconditionally invalidated,
  1346. * so real IO is not possible anymore.
  1347. */
  1348. if (offset == 0)
  1349. try_to_release_page(page, 0);
  1350. out:
  1351. return;
  1352. }
  1353. EXPORT_SYMBOL(block_invalidatepage);
  1354. /*
  1355. * We attach and possibly dirty the buffers atomically wrt
  1356. * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
  1357. * is already excluded via the page lock.
  1358. */
  1359. void create_empty_buffers(struct page *page,
  1360. unsigned long blocksize, unsigned long b_state)
  1361. {
  1362. struct buffer_head *bh, *head, *tail;
  1363. head = alloc_page_buffers(page, blocksize, 1);
  1364. bh = head;
  1365. do {
  1366. bh->b_state |= b_state;
  1367. tail = bh;
  1368. bh = bh->b_this_page;
  1369. } while (bh);
  1370. tail->b_this_page = head;
  1371. spin_lock(&page->mapping->private_lock);
  1372. if (PageUptodate(page) || PageDirty(page)) {
  1373. bh = head;
  1374. do {
  1375. if (PageDirty(page))
  1376. set_buffer_dirty(bh);
  1377. if (PageUptodate(page))
  1378. set_buffer_uptodate(bh);
  1379. bh = bh->b_this_page;
  1380. } while (bh != head);
  1381. }
  1382. attach_page_buffers(page, head);
  1383. spin_unlock(&page->mapping->private_lock);
  1384. }
  1385. EXPORT_SYMBOL(create_empty_buffers);
  1386. /*
  1387. * We are taking a block for data and we don't want any output from any
  1388. * buffer-cache aliases starting from return from that function and
  1389. * until the moment when something will explicitly mark the buffer
  1390. * dirty (hopefully that will not happen until we will free that block ;-)
  1391. * We don't even need to mark it not-uptodate - nobody can expect
  1392. * anything from a newly allocated buffer anyway. We used to used
  1393. * unmap_buffer() for such invalidation, but that was wrong. We definitely
  1394. * don't want to mark the alias unmapped, for example - it would confuse
  1395. * anyone who might pick it with bread() afterwards...
  1396. *
  1397. * Also.. Note that bforget() doesn't lock the buffer. So there can
  1398. * be writeout I/O going on against recently-freed buffers. We don't
  1399. * wait on that I/O in bforget() - it's more efficient to wait on the I/O
  1400. * only if we really need to. That happens here.
  1401. */
  1402. void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
  1403. {
  1404. struct buffer_head *old_bh;
  1405. might_sleep();
  1406. old_bh = __find_get_block_slow(bdev, block);
  1407. if (old_bh) {
  1408. clear_buffer_dirty(old_bh);
  1409. wait_on_buffer(old_bh);
  1410. clear_buffer_req(old_bh);
  1411. __brelse(old_bh);
  1412. }
  1413. }
  1414. EXPORT_SYMBOL(unmap_underlying_metadata);
  1415. /*
  1416. * Size is a power-of-two in the range 512..PAGE_SIZE,
  1417. * and the case we care about most is PAGE_SIZE.
  1418. *
  1419. * So this *could* possibly be written with those
  1420. * constraints in mind (relevant mostly if some
  1421. * architecture has a slow bit-scan instruction)
  1422. */
  1423. static inline int block_size_bits(unsigned int blocksize)
  1424. {
  1425. return ilog2(blocksize);
  1426. }
  1427. static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
  1428. {
  1429. BUG_ON(!PageLocked(page));
  1430. if (!page_has_buffers(page))
  1431. create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
  1432. return page_buffers(page);
  1433. }
  1434. /*
  1435. * NOTE! All mapped/uptodate combinations are valid:
  1436. *
  1437. * Mapped Uptodate Meaning
  1438. *
  1439. * No No "unknown" - must do get_block()
  1440. * No Yes "hole" - zero-filled
  1441. * Yes No "allocated" - allocated on disk, not read in
  1442. * Yes Yes "valid" - allocated and up-to-date in memory.
  1443. *
  1444. * "Dirty" is valid only with the last case (mapped+uptodate).
  1445. */
  1446. /*
  1447. * While block_write_full_page is writing back the dirty buffers under
  1448. * the page lock, whoever dirtied the buffers may decide to clean them
  1449. * again at any time. We handle that by only looking at the buffer
  1450. * state inside lock_buffer().
  1451. *
  1452. * If block_write_full_page() is called for regular writeback
  1453. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1454. * locked buffer. This only can happen if someone has written the buffer
  1455. * directly, with submit_bh(). At the address_space level PageWriteback
  1456. * prevents this contention from occurring.
  1457. *
  1458. * If block_write_full_page() is called with wbc->sync_mode ==
  1459. * WB_SYNC_ALL, the writes are posted using WRITE_SYNC; this
  1460. * causes the writes to be flagged as synchronous writes.
  1461. */
  1462. static int __block_write_full_page(struct inode *inode, struct page *page,
  1463. get_block_t *get_block, struct writeback_control *wbc,
  1464. bh_end_io_t *handler)
  1465. {
  1466. int err;
  1467. sector_t block;
  1468. sector_t last_block;
  1469. struct buffer_head *bh, *head;
  1470. unsigned int blocksize, bbits;
  1471. int nr_underway = 0;
  1472. int write_op = (wbc->sync_mode == WB_SYNC_ALL ?
  1473. WRITE_SYNC : WRITE);
  1474. head = create_page_buffers(page, inode,
  1475. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1476. /*
  1477. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1478. * here, and the (potentially unmapped) buffers may become dirty at
  1479. * any time. If a buffer becomes dirty here after we've inspected it
  1480. * then we just miss that fact, and the page stays dirty.
  1481. *
  1482. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1483. * handle that here by just cleaning them.
  1484. */
  1485. bh = head;
  1486. blocksize = bh->b_size;
  1487. bbits = block_size_bits(blocksize);
  1488. block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
  1489. last_block = (i_size_read(inode) - 1) >> bbits;
  1490. /*
  1491. * Get all the dirty buffers mapped to disk addresses and
  1492. * handle any aliases from the underlying blockdev's mapping.
  1493. */
  1494. do {
  1495. if (block > last_block) {
  1496. /*
  1497. * mapped buffers outside i_size will occur, because
  1498. * this page can be outside i_size when there is a
  1499. * truncate in progress.
  1500. */
  1501. /*
  1502. * The buffer was zeroed by block_write_full_page()
  1503. */
  1504. clear_buffer_dirty(bh);
  1505. set_buffer_uptodate(bh);
  1506. } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
  1507. buffer_dirty(bh)) {
  1508. WARN_ON(bh->b_size != blocksize);
  1509. err = get_block(inode, block, bh, 1);
  1510. if (err)
  1511. goto recover;
  1512. clear_buffer_delay(bh);
  1513. if (buffer_new(bh)) {
  1514. /* blockdev mappings never come here */
  1515. clear_buffer_new(bh);
  1516. unmap_underlying_metadata(bh->b_bdev,
  1517. bh->b_blocknr);
  1518. }
  1519. }
  1520. bh = bh->b_this_page;
  1521. block++;
  1522. } while (bh != head);
  1523. do {
  1524. if (!buffer_mapped(bh))
  1525. continue;
  1526. /*
  1527. * If it's a fully non-blocking write attempt and we cannot
  1528. * lock the buffer then redirty the page. Note that this can
  1529. * potentially cause a busy-wait loop from writeback threads
  1530. * and kswapd activity, but those code paths have their own
  1531. * higher-level throttling.
  1532. */
  1533. if (wbc->sync_mode != WB_SYNC_NONE) {
  1534. lock_buffer(bh);
  1535. } else if (!trylock_buffer(bh)) {
  1536. redirty_page_for_writepage(wbc, page);
  1537. continue;
  1538. }
  1539. if (test_clear_buffer_dirty(bh)) {
  1540. mark_buffer_async_write_endio(bh, handler);
  1541. } else {
  1542. unlock_buffer(bh);
  1543. }
  1544. } while ((bh = bh->b_this_page) != head);
  1545. /*
  1546. * The page and its buffers are protected by PageWriteback(), so we can
  1547. * drop the bh refcounts early.
  1548. */
  1549. BUG_ON(PageWriteback(page));
  1550. set_page_writeback(page);
  1551. do {
  1552. struct buffer_head *next = bh->b_this_page;
  1553. if (buffer_async_write(bh)) {
  1554. submit_bh(write_op, bh);
  1555. nr_underway++;
  1556. }
  1557. bh = next;
  1558. } while (bh != head);
  1559. unlock_page(page);
  1560. err = 0;
  1561. done:
  1562. if (nr_underway == 0) {
  1563. /*
  1564. * The page was marked dirty, but the buffers were
  1565. * clean. Someone wrote them back by hand with
  1566. * ll_rw_block/submit_bh. A rare case.
  1567. */
  1568. end_page_writeback(page);
  1569. /*
  1570. * The page and buffer_heads can be released at any time from
  1571. * here on.
  1572. */
  1573. }
  1574. return err;
  1575. recover:
  1576. /*
  1577. * ENOSPC, or some other error. We may already have added some
  1578. * blocks to the file, so we need to write these out to avoid
  1579. * exposing stale data.
  1580. * The page is currently locked and not marked for writeback
  1581. */
  1582. bh = head;
  1583. /* Recovery: lock and submit the mapped buffers */
  1584. do {
  1585. if (buffer_mapped(bh) && buffer_dirty(bh) &&
  1586. !buffer_delay(bh)) {
  1587. lock_buffer(bh);
  1588. mark_buffer_async_write_endio(bh, handler);
  1589. } else {
  1590. /*
  1591. * The buffer may have been set dirty during
  1592. * attachment to a dirty page.
  1593. */
  1594. clear_buffer_dirty(bh);
  1595. }
  1596. } while ((bh = bh->b_this_page) != head);
  1597. SetPageError(page);
  1598. BUG_ON(PageWriteback(page));
  1599. mapping_set_error(page->mapping, err);
  1600. set_page_writeback(page);
  1601. do {
  1602. struct buffer_head *next = bh->b_this_page;
  1603. if (buffer_async_write(bh)) {
  1604. clear_buffer_dirty(bh);
  1605. submit_bh(write_op, bh);
  1606. nr_underway++;
  1607. }
  1608. bh = next;
  1609. } while (bh != head);
  1610. unlock_page(page);
  1611. goto done;
  1612. }
  1613. /*
  1614. * If a page has any new buffers, zero them out here, and mark them uptodate
  1615. * and dirty so they'll be written out (in order to prevent uninitialised
  1616. * block data from leaking). And clear the new bit.
  1617. */
  1618. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1619. {
  1620. unsigned int block_start, block_end;
  1621. struct buffer_head *head, *bh;
  1622. BUG_ON(!PageLocked(page));
  1623. if (!page_has_buffers(page))
  1624. return;
  1625. bh = head = page_buffers(page);
  1626. block_start = 0;
  1627. do {
  1628. block_end = block_start + bh->b_size;
  1629. if (buffer_new(bh)) {
  1630. if (block_end > from && block_start < to) {
  1631. if (!PageUptodate(page)) {
  1632. unsigned start, size;
  1633. start = max(from, block_start);
  1634. size = min(to, block_end) - start;
  1635. zero_user(page, start, size);
  1636. set_buffer_uptodate(bh);
  1637. }
  1638. clear_buffer_new(bh);
  1639. mark_buffer_dirty(bh);
  1640. }
  1641. }
  1642. block_start = block_end;
  1643. bh = bh->b_this_page;
  1644. } while (bh != head);
  1645. }
  1646. EXPORT_SYMBOL(page_zero_new_buffers);
  1647. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  1648. get_block_t *get_block)
  1649. {
  1650. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1651. unsigned to = from + len;
  1652. struct inode *inode = page->mapping->host;
  1653. unsigned block_start, block_end;
  1654. sector_t block;
  1655. int err = 0;
  1656. unsigned blocksize, bbits;
  1657. struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
  1658. BUG_ON(!PageLocked(page));
  1659. BUG_ON(from > PAGE_CACHE_SIZE);
  1660. BUG_ON(to > PAGE_CACHE_SIZE);
  1661. BUG_ON(from > to);
  1662. head = create_page_buffers(page, inode, 0);
  1663. blocksize = head->b_size;
  1664. bbits = block_size_bits(blocksize);
  1665. block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
  1666. for(bh = head, block_start = 0; bh != head || !block_start;
  1667. block++, block_start=block_end, bh = bh->b_this_page) {
  1668. block_end = block_start + blocksize;
  1669. if (block_end <= from || block_start >= to) {
  1670. if (PageUptodate(page)) {
  1671. if (!buffer_uptodate(bh))
  1672. set_buffer_uptodate(bh);
  1673. }
  1674. continue;
  1675. }
  1676. if (buffer_new(bh))
  1677. clear_buffer_new(bh);
  1678. if (!buffer_mapped(bh)) {
  1679. WARN_ON(bh->b_size != blocksize);
  1680. err = get_block(inode, block, bh, 1);
  1681. if (err)
  1682. break;
  1683. if (buffer_new(bh)) {
  1684. unmap_underlying_metadata(bh->b_bdev,
  1685. bh->b_blocknr);
  1686. if (PageUptodate(page)) {
  1687. clear_buffer_new(bh);
  1688. set_buffer_uptodate(bh);
  1689. mark_buffer_dirty(bh);
  1690. continue;
  1691. }
  1692. if (block_end > to || block_start < from)
  1693. zero_user_segments(page,
  1694. to, block_end,
  1695. block_start, from);
  1696. continue;
  1697. }
  1698. }
  1699. if (PageUptodate(page)) {
  1700. if (!buffer_uptodate(bh))
  1701. set_buffer_uptodate(bh);
  1702. continue;
  1703. }
  1704. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1705. !buffer_unwritten(bh) &&
  1706. (block_start < from || block_end > to)) {
  1707. ll_rw_block(READ, 1, &bh);
  1708. *wait_bh++=bh;
  1709. }
  1710. }
  1711. /*
  1712. * If we issued read requests - let them complete.
  1713. */
  1714. while(wait_bh > wait) {
  1715. wait_on_buffer(*--wait_bh);
  1716. if (!buffer_uptodate(*wait_bh))
  1717. err = -EIO;
  1718. }
  1719. if (unlikely(err))
  1720. page_zero_new_buffers(page, from, to);
  1721. return err;
  1722. }
  1723. EXPORT_SYMBOL(__block_write_begin);
  1724. static int __block_commit_write(struct inode *inode, struct page *page,
  1725. unsigned from, unsigned to)
  1726. {
  1727. unsigned block_start, block_end;
  1728. int partial = 0;
  1729. unsigned blocksize;
  1730. struct buffer_head *bh, *head;
  1731. bh = head = page_buffers(page);
  1732. blocksize = bh->b_size;
  1733. block_start = 0;
  1734. do {
  1735. block_end = block_start + blocksize;
  1736. if (block_end <= from || block_start >= to) {
  1737. if (!buffer_uptodate(bh))
  1738. partial = 1;
  1739. } else {
  1740. set_buffer_uptodate(bh);
  1741. mark_buffer_dirty(bh);
  1742. }
  1743. clear_buffer_new(bh);
  1744. block_start = block_end;
  1745. bh = bh->b_this_page;
  1746. } while (bh != head);
  1747. /*
  1748. * If this is a partial write which happened to make all buffers
  1749. * uptodate then we can optimize away a bogus readpage() for
  1750. * the next read(). Here we 'discover' whether the page went
  1751. * uptodate as a result of this (potentially partial) write.
  1752. */
  1753. if (!partial)
  1754. SetPageUptodate(page);
  1755. return 0;
  1756. }
  1757. /*
  1758. * block_write_begin takes care of the basic task of block allocation and
  1759. * bringing partial write blocks uptodate first.
  1760. *
  1761. * The filesystem needs to handle block truncation upon failure.
  1762. */
  1763. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  1764. unsigned flags, struct page **pagep, get_block_t *get_block)
  1765. {
  1766. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1767. struct page *page;
  1768. int status;
  1769. page = grab_cache_page_write_begin(mapping, index, flags);
  1770. if (!page)
  1771. return -ENOMEM;
  1772. status = __block_write_begin(page, pos, len, get_block);
  1773. if (unlikely(status)) {
  1774. unlock_page(page);
  1775. page_cache_release(page);
  1776. page = NULL;
  1777. }
  1778. *pagep = page;
  1779. return status;
  1780. }
  1781. EXPORT_SYMBOL(block_write_begin);
  1782. int block_write_end(struct file *file, struct address_space *mapping,
  1783. loff_t pos, unsigned len, unsigned copied,
  1784. struct page *page, void *fsdata)
  1785. {
  1786. struct inode *inode = mapping->host;
  1787. unsigned start;
  1788. start = pos & (PAGE_CACHE_SIZE - 1);
  1789. if (unlikely(copied < len)) {
  1790. /*
  1791. * The buffers that were written will now be uptodate, so we
  1792. * don't have to worry about a readpage reading them and
  1793. * overwriting a partial write. However if we have encountered
  1794. * a short write and only partially written into a buffer, it
  1795. * will not be marked uptodate, so a readpage might come in and
  1796. * destroy our partial write.
  1797. *
  1798. * Do the simplest thing, and just treat any short write to a
  1799. * non uptodate page as a zero-length write, and force the
  1800. * caller to redo the whole thing.
  1801. */
  1802. if (!PageUptodate(page))
  1803. copied = 0;
  1804. page_zero_new_buffers(page, start+copied, start+len);
  1805. }
  1806. flush_dcache_page(page);
  1807. /* This could be a short (even 0-length) commit */
  1808. __block_commit_write(inode, page, start, start+copied);
  1809. return copied;
  1810. }
  1811. EXPORT_SYMBOL(block_write_end);
  1812. int generic_write_end(struct file *file, struct address_space *mapping,
  1813. loff_t pos, unsigned len, unsigned copied,
  1814. struct page *page, void *fsdata)
  1815. {
  1816. struct inode *inode = mapping->host;
  1817. int i_size_changed = 0;
  1818. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1819. /*
  1820. * No need to use i_size_read() here, the i_size
  1821. * cannot change under us because we hold i_mutex.
  1822. *
  1823. * But it's important to update i_size while still holding page lock:
  1824. * page writeout could otherwise come in and zero beyond i_size.
  1825. */
  1826. if (pos+copied > inode->i_size) {
  1827. i_size_write(inode, pos+copied);
  1828. i_size_changed = 1;
  1829. }
  1830. unlock_page(page);
  1831. page_cache_release(page);
  1832. /*
  1833. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1834. * makes the holding time of page lock longer. Second, it forces lock
  1835. * ordering of page lock and transaction start for journaling
  1836. * filesystems.
  1837. */
  1838. if (i_size_changed)
  1839. mark_inode_dirty(inode);
  1840. return copied;
  1841. }
  1842. EXPORT_SYMBOL(generic_write_end);
  1843. /*
  1844. * block_is_partially_uptodate checks whether buffers within a page are
  1845. * uptodate or not.
  1846. *
  1847. * Returns true if all buffers which correspond to a file portion
  1848. * we want to read are uptodate.
  1849. */
  1850. int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
  1851. unsigned long from)
  1852. {
  1853. unsigned block_start, block_end, blocksize;
  1854. unsigned to;
  1855. struct buffer_head *bh, *head;
  1856. int ret = 1;
  1857. if (!page_has_buffers(page))
  1858. return 0;
  1859. head = page_buffers(page);
  1860. blocksize = head->b_size;
  1861. to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
  1862. to = from + to;
  1863. if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
  1864. return 0;
  1865. bh = head;
  1866. block_start = 0;
  1867. do {
  1868. block_end = block_start + blocksize;
  1869. if (block_end > from && block_start < to) {
  1870. if (!buffer_uptodate(bh)) {
  1871. ret = 0;
  1872. break;
  1873. }
  1874. if (block_end >= to)
  1875. break;
  1876. }
  1877. block_start = block_end;
  1878. bh = bh->b_this_page;
  1879. } while (bh != head);
  1880. return ret;
  1881. }
  1882. EXPORT_SYMBOL(block_is_partially_uptodate);
  1883. /*
  1884. * Generic "read page" function for block devices that have the normal
  1885. * get_block functionality. This is most of the block device filesystems.
  1886. * Reads the page asynchronously --- the unlock_buffer() and
  1887. * set/clear_buffer_uptodate() functions propagate buffer state into the
  1888. * page struct once IO has completed.
  1889. */
  1890. int block_read_full_page(struct page *page, get_block_t *get_block)
  1891. {
  1892. struct inode *inode = page->mapping->host;
  1893. sector_t iblock, lblock;
  1894. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  1895. unsigned int blocksize, bbits;
  1896. int nr, i;
  1897. int fully_mapped = 1;
  1898. head = create_page_buffers(page, inode, 0);
  1899. blocksize = head->b_size;
  1900. bbits = block_size_bits(blocksize);
  1901. iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
  1902. lblock = (i_size_read(inode)+blocksize-1) >> bbits;
  1903. bh = head;
  1904. nr = 0;
  1905. i = 0;
  1906. do {
  1907. if (buffer_uptodate(bh))
  1908. continue;
  1909. if (!buffer_mapped(bh)) {
  1910. int err = 0;
  1911. fully_mapped = 0;
  1912. if (iblock < lblock) {
  1913. WARN_ON(bh->b_size != blocksize);
  1914. err = get_block(inode, iblock, bh, 0);
  1915. if (err)
  1916. SetPageError(page);
  1917. }
  1918. if (!buffer_mapped(bh)) {
  1919. zero_user(page, i * blocksize, blocksize);
  1920. if (!err)
  1921. set_buffer_uptodate(bh);
  1922. continue;
  1923. }
  1924. /*
  1925. * get_block() might have updated the buffer
  1926. * synchronously
  1927. */
  1928. if (buffer_uptodate(bh))
  1929. continue;
  1930. }
  1931. arr[nr++] = bh;
  1932. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  1933. if (fully_mapped)
  1934. SetPageMappedToDisk(page);
  1935. if (!nr) {
  1936. /*
  1937. * All buffers are uptodate - we can set the page uptodate
  1938. * as well. But not if get_block() returned an error.
  1939. */
  1940. if (!PageError(page))
  1941. SetPageUptodate(page);
  1942. unlock_page(page);
  1943. return 0;
  1944. }
  1945. /* Stage two: lock the buffers */
  1946. for (i = 0; i < nr; i++) {
  1947. bh = arr[i];
  1948. lock_buffer(bh);
  1949. mark_buffer_async_read(bh);
  1950. }
  1951. /*
  1952. * Stage 3: start the IO. Check for uptodateness
  1953. * inside the buffer lock in case another process reading
  1954. * the underlying blockdev brought it uptodate (the sct fix).
  1955. */
  1956. for (i = 0; i < nr; i++) {
  1957. bh = arr[i];
  1958. if (buffer_uptodate(bh))
  1959. end_buffer_async_read(bh, 1);
  1960. else
  1961. submit_bh(READ, bh);
  1962. }
  1963. return 0;
  1964. }
  1965. EXPORT_SYMBOL(block_read_full_page);
  1966. /* utility function for filesystems that need to do work on expanding
  1967. * truncates. Uses filesystem pagecache writes to allow the filesystem to
  1968. * deal with the hole.
  1969. */
  1970. int generic_cont_expand_simple(struct inode *inode, loff_t size)
  1971. {
  1972. struct address_space *mapping = inode->i_mapping;
  1973. struct page *page;
  1974. void *fsdata;
  1975. int err;
  1976. err = inode_newsize_ok(inode, size);
  1977. if (err)
  1978. goto out;
  1979. err = pagecache_write_begin(NULL, mapping, size, 0,
  1980. AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
  1981. &page, &fsdata);
  1982. if (err)
  1983. goto out;
  1984. err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  1985. BUG_ON(err > 0);
  1986. out:
  1987. return err;
  1988. }
  1989. EXPORT_SYMBOL(generic_cont_expand_simple);
  1990. static int cont_expand_zero(struct file *file, struct address_space *mapping,
  1991. loff_t pos, loff_t *bytes)
  1992. {
  1993. struct inode *inode = mapping->host;
  1994. unsigned blocksize = 1 << inode->i_blkbits;
  1995. struct page *page;
  1996. void *fsdata;
  1997. pgoff_t index, curidx;
  1998. loff_t curpos;
  1999. unsigned zerofrom, offset, len;
  2000. int err = 0;
  2001. index = pos >> PAGE_CACHE_SHIFT;
  2002. offset = pos & ~PAGE_CACHE_MASK;
  2003. while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
  2004. zerofrom = curpos & ~PAGE_CACHE_MASK;
  2005. if (zerofrom & (blocksize-1)) {
  2006. *bytes |= (blocksize-1);
  2007. (*bytes)++;
  2008. }
  2009. len = PAGE_CACHE_SIZE - zerofrom;
  2010. err = pagecache_write_begin(file, mapping, curpos, len,
  2011. AOP_FLAG_UNINTERRUPTIBLE,
  2012. &page, &fsdata);
  2013. if (err)
  2014. goto out;
  2015. zero_user(page, zerofrom, len);
  2016. err = pagecache_write_end(file, mapping, curpos, len, len,
  2017. page, fsdata);
  2018. if (err < 0)
  2019. goto out;
  2020. BUG_ON(err != len);
  2021. err = 0;
  2022. balance_dirty_pages_ratelimited(mapping);
  2023. }
  2024. /* page covers the boundary, find the boundary offset */
  2025. if (index == curidx) {
  2026. zerofrom = curpos & ~PAGE_CACHE_MASK;
  2027. /* if we will expand the thing last block will be filled */
  2028. if (offset <= zerofrom) {
  2029. goto out;
  2030. }
  2031. if (zerofrom & (blocksize-1)) {
  2032. *bytes |= (blocksize-1);
  2033. (*bytes)++;
  2034. }
  2035. len = offset - zerofrom;
  2036. err = pagecache_write_begin(file, mapping, curpos, len,
  2037. AOP_FLAG_UNINTERRUPTIBLE,
  2038. &page, &fsdata);
  2039. if (err)
  2040. goto out;
  2041. zero_user(page, zerofrom, len);
  2042. err = pagecache_write_end(file, mapping, curpos, len, len,
  2043. page, fsdata);
  2044. if (err < 0)
  2045. goto out;
  2046. BUG_ON(err != len);
  2047. err = 0;
  2048. }
  2049. out:
  2050. return err;
  2051. }
  2052. /*
  2053. * For moronic filesystems that do not allow holes in file.
  2054. * We may have to extend the file.
  2055. */
  2056. int cont_write_begin(struct file *file, struct address_space *mapping,
  2057. loff_t pos, unsigned len, unsigned flags,
  2058. struct page **pagep, void **fsdata,
  2059. get_block_t *get_block, loff_t *bytes)
  2060. {
  2061. struct inode *inode = mapping->host;
  2062. unsigned blocksize = 1 << inode->i_blkbits;
  2063. unsigned zerofrom;
  2064. int err;
  2065. err = cont_expand_zero(file, mapping, pos, bytes);
  2066. if (err)
  2067. return err;
  2068. zerofrom = *bytes & ~PAGE_CACHE_MASK;
  2069. if (pos+len > *bytes && zerofrom & (blocksize-1)) {
  2070. *bytes |= (blocksize-1);
  2071. (*bytes)++;
  2072. }
  2073. return block_write_begin(mapping, pos, len, flags, pagep, get_block);
  2074. }
  2075. EXPORT_SYMBOL(cont_write_begin);
  2076. int block_commit_write(struct page *page, unsigned from, unsigned to)
  2077. {
  2078. struct inode *inode = page->mapping->host;
  2079. __block_commit_write(inode,page,from,to);
  2080. return 0;
  2081. }
  2082. EXPORT_SYMBOL(block_commit_write);
  2083. /*
  2084. * block_page_mkwrite() is not allowed to change the file size as it gets
  2085. * called from a page fault handler when a page is first dirtied. Hence we must
  2086. * be careful to check for EOF conditions here. We set the page up correctly
  2087. * for a written page which means we get ENOSPC checking when writing into
  2088. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2089. * support these features.
  2090. *
  2091. * We are not allowed to take the i_mutex here so we have to play games to
  2092. * protect against truncate races as the page could now be beyond EOF. Because
  2093. * truncate writes the inode size before removing pages, once we have the
  2094. * page lock we can determine safely if the page is beyond EOF. If it is not
  2095. * beyond EOF, then the page is guaranteed safe against truncation until we
  2096. * unlock the page.
  2097. *
  2098. * Direct callers of this function should protect against filesystem freezing
  2099. * using sb_start_write() - sb_end_write() functions.
  2100. */
  2101. int __block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2102. get_block_t get_block)
  2103. {
  2104. struct page *page = vmf->page;
  2105. struct inode *inode = file_inode(vma->vm_file);
  2106. unsigned long end;
  2107. loff_t size;
  2108. int ret;
  2109. lock_page(page);
  2110. size = i_size_read(inode);
  2111. if ((page->mapping != inode->i_mapping) ||
  2112. (page_offset(page) > size)) {
  2113. /* We overload EFAULT to mean page got truncated */
  2114. ret = -EFAULT;
  2115. goto out_unlock;
  2116. }
  2117. /* page is wholly or partially inside EOF */
  2118. if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
  2119. end = size & ~PAGE_CACHE_MASK;
  2120. else
  2121. end = PAGE_CACHE_SIZE;
  2122. ret = __block_write_begin(page, 0, end, get_block);
  2123. if (!ret)
  2124. ret = block_commit_write(page, 0, end);
  2125. if (unlikely(ret < 0))
  2126. goto out_unlock;
  2127. set_page_dirty(page);
  2128. wait_for_stable_page(page);
  2129. return 0;
  2130. out_unlock:
  2131. unlock_page(page);
  2132. return ret;
  2133. }
  2134. EXPORT_SYMBOL(__block_page_mkwrite);
  2135. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2136. get_block_t get_block)
  2137. {
  2138. int ret;
  2139. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  2140. sb_start_pagefault(sb);
  2141. /*
  2142. * Update file times before taking page lock. We may end up failing the
  2143. * fault so this update may be superfluous but who really cares...
  2144. */
  2145. file_update_time(vma->vm_file);
  2146. ret = __block_page_mkwrite(vma, vmf, get_block);
  2147. sb_end_pagefault(sb);
  2148. return block_page_mkwrite_return(ret);
  2149. }
  2150. EXPORT_SYMBOL(block_page_mkwrite);
  2151. /*
  2152. * nobh_write_begin()'s prereads are special: the buffer_heads are freed
  2153. * immediately, while under the page lock. So it needs a special end_io
  2154. * handler which does not touch the bh after unlocking it.
  2155. */
  2156. static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
  2157. {
  2158. __end_buffer_read_notouch(bh, uptodate);
  2159. }
  2160. /*
  2161. * Attach the singly-linked list of buffers created by nobh_write_begin, to
  2162. * the page (converting it to circular linked list and taking care of page
  2163. * dirty races).
  2164. */
  2165. static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
  2166. {
  2167. struct buffer_head *bh;
  2168. BUG_ON(!PageLocked(page));
  2169. spin_lock(&page->mapping->private_lock);
  2170. bh = head;
  2171. do {
  2172. if (PageDirty(page))
  2173. set_buffer_dirty(bh);
  2174. if (!bh->b_this_page)
  2175. bh->b_this_page = head;
  2176. bh = bh->b_this_page;
  2177. } while (bh != head);
  2178. attach_page_buffers(page, head);
  2179. spin_unlock(&page->mapping->private_lock);
  2180. }
  2181. /*
  2182. * On entry, the page is fully not uptodate.
  2183. * On exit the page is fully uptodate in the areas outside (from,to)
  2184. * The filesystem needs to handle block truncation upon failure.
  2185. */
  2186. int nobh_write_begin(struct address_space *mapping,
  2187. loff_t pos, unsigned len, unsigned flags,
  2188. struct page **pagep, void **fsdata,
  2189. get_block_t *get_block)
  2190. {
  2191. struct inode *inode = mapping->host;
  2192. const unsigned blkbits = inode->i_blkbits;
  2193. const unsigned blocksize = 1 << blkbits;
  2194. struct buffer_head *head, *bh;
  2195. struct page *page;
  2196. pgoff_t index;
  2197. unsigned from, to;
  2198. unsigned block_in_page;
  2199. unsigned block_start, block_end;
  2200. sector_t block_in_file;
  2201. int nr_reads = 0;
  2202. int ret = 0;
  2203. int is_mapped_to_disk = 1;
  2204. index = pos >> PAGE_CACHE_SHIFT;
  2205. from = pos & (PAGE_CACHE_SIZE - 1);
  2206. to = from + len;
  2207. page = grab_cache_page_write_begin(mapping, index, flags);
  2208. if (!page)
  2209. return -ENOMEM;
  2210. *pagep = page;
  2211. *fsdata = NULL;
  2212. if (page_has_buffers(page)) {
  2213. ret = __block_write_begin(page, pos, len, get_block);
  2214. if (unlikely(ret))
  2215. goto out_release;
  2216. return ret;
  2217. }
  2218. if (PageMappedToDisk(page))
  2219. return 0;
  2220. /*
  2221. * Allocate buffers so that we can keep track of state, and potentially
  2222. * attach them to the page if an error occurs. In the common case of
  2223. * no error, they will just be freed again without ever being attached
  2224. * to the page (which is all OK, because we're under the page lock).
  2225. *
  2226. * Be careful: the buffer linked list is a NULL terminated one, rather
  2227. * than the circular one we're used to.
  2228. */
  2229. head = alloc_page_buffers(page, blocksize, 0);
  2230. if (!head) {
  2231. ret = -ENOMEM;
  2232. goto out_release;
  2233. }
  2234. block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
  2235. /*
  2236. * We loop across all blocks in the page, whether or not they are
  2237. * part of the affected region. This is so we can discover if the
  2238. * page is fully mapped-to-disk.
  2239. */
  2240. for (block_start = 0, block_in_page = 0, bh = head;
  2241. block_start < PAGE_CACHE_SIZE;
  2242. block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
  2243. int create;
  2244. block_end = block_start + blocksize;
  2245. bh->b_state = 0;
  2246. create = 1;
  2247. if (block_start >= to)
  2248. create = 0;
  2249. ret = get_block(inode, block_in_file + block_in_page,
  2250. bh, create);
  2251. if (ret)
  2252. goto failed;
  2253. if (!buffer_mapped(bh))
  2254. is_mapped_to_disk = 0;
  2255. if (buffer_new(bh))
  2256. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  2257. if (PageUptodate(page)) {
  2258. set_buffer_uptodate(bh);
  2259. continue;
  2260. }
  2261. if (buffer_new(bh) || !buffer_mapped(bh)) {
  2262. zero_user_segments(page, block_start, from,
  2263. to, block_end);
  2264. continue;
  2265. }
  2266. if (buffer_uptodate(bh))
  2267. continue; /* reiserfs does this */
  2268. if (block_start < from || block_end > to) {
  2269. lock_buffer(bh);
  2270. bh->b_end_io = end_buffer_read_nobh;
  2271. submit_bh(READ, bh);
  2272. nr_reads++;
  2273. }
  2274. }
  2275. if (nr_reads) {
  2276. /*
  2277. * The page is locked, so these buffers are protected from
  2278. * any VM or truncate activity. Hence we don't need to care
  2279. * for the buffer_head refcounts.
  2280. */
  2281. for (bh = head; bh; bh = bh->b_this_page) {
  2282. wait_on_buffer(bh);
  2283. if (!buffer_uptodate(bh))
  2284. ret = -EIO;
  2285. }
  2286. if (ret)
  2287. goto failed;
  2288. }
  2289. if (is_mapped_to_disk)
  2290. SetPageMappedToDisk(page);
  2291. *fsdata = head; /* to be released by nobh_write_end */
  2292. return 0;
  2293. failed:
  2294. BUG_ON(!ret);
  2295. /*
  2296. * Error recovery is a bit difficult. We need to zero out blocks that
  2297. * were newly allocated, and dirty them to ensure they get written out.
  2298. * Buffers need to be attached to the page at this point, otherwise
  2299. * the handling of potential IO errors during writeout would be hard
  2300. * (could try doing synchronous writeout, but what if that fails too?)
  2301. */
  2302. attach_nobh_buffers(page, head);
  2303. page_zero_new_buffers(page, from, to);
  2304. out_release:
  2305. unlock_page(page);
  2306. page_cache_release(page);
  2307. *pagep = NULL;
  2308. return ret;
  2309. }
  2310. EXPORT_SYMBOL(nobh_write_begin);
  2311. int nobh_write_end(struct file *file, struct address_space *mapping,
  2312. loff_t pos, unsigned len, unsigned copied,
  2313. struct page *page, void *fsdata)
  2314. {
  2315. struct inode *inode = page->mapping->host;
  2316. struct buffer_head *head = fsdata;
  2317. struct buffer_head *bh;
  2318. BUG_ON(fsdata != NULL && page_has_buffers(page));
  2319. if (unlikely(copied < len) && head)
  2320. attach_nobh_buffers(page, head);
  2321. if (page_has_buffers(page))
  2322. return generic_write_end(file, mapping, pos, len,
  2323. copied, page, fsdata);
  2324. SetPageUptodate(page);
  2325. set_page_dirty(page);
  2326. if (pos+copied > inode->i_size) {
  2327. i_size_write(inode, pos+copied);
  2328. mark_inode_dirty(inode);
  2329. }
  2330. unlock_page(page);
  2331. page_cache_release(page);
  2332. while (head) {
  2333. bh = head;
  2334. head = head->b_this_page;
  2335. free_buffer_head(bh);
  2336. }
  2337. return copied;
  2338. }
  2339. EXPORT_SYMBOL(nobh_write_end);
  2340. /*
  2341. * nobh_writepage() - based on block_full_write_page() except
  2342. * that it tries to operate without attaching bufferheads to
  2343. * the page.
  2344. */
  2345. int nobh_writepage(struct page *page, get_block_t *get_block,
  2346. struct writeback_control *wbc)
  2347. {
  2348. struct inode * const inode = page->mapping->host;
  2349. loff_t i_size = i_size_read(inode);
  2350. const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  2351. unsigned offset;
  2352. int ret;
  2353. /* Is the page fully inside i_size? */
  2354. if (page->index < end_index)
  2355. goto out;
  2356. /* Is the page fully outside i_size? (truncate in progress) */
  2357. offset = i_size & (PAGE_CACHE_SIZE-1);
  2358. if (page->index >= end_index+1 || !offset) {
  2359. /*
  2360. * The page may have dirty, unmapped buffers. For example,
  2361. * they may have been added in ext3_writepage(). Make them
  2362. * freeable here, so the page does not leak.
  2363. */
  2364. #if 0
  2365. /* Not really sure about this - do we need this ? */
  2366. if (page->mapping->a_ops->invalidatepage)
  2367. page->mapping->a_ops->invalidatepage(page, offset);
  2368. #endif
  2369. unlock_page(page);
  2370. return 0; /* don't care */
  2371. }
  2372. /*
  2373. * The page straddles i_size. It must be zeroed out on each and every
  2374. * writepage invocation because it may be mmapped. "A file is mapped
  2375. * in multiples of the page size. For a file that is not a multiple of
  2376. * the page size, the remaining memory is zeroed when mapped, and
  2377. * writes to that region are not written out to the file."
  2378. */
  2379. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  2380. out:
  2381. ret = mpage_writepage(page, get_block, wbc);
  2382. if (ret == -EAGAIN)
  2383. ret = __block_write_full_page(inode, page, get_block, wbc,
  2384. end_buffer_async_write);
  2385. return ret;
  2386. }
  2387. EXPORT_SYMBOL(nobh_writepage);
  2388. int nobh_truncate_page(struct address_space *mapping,
  2389. loff_t from, get_block_t *get_block)
  2390. {
  2391. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2392. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2393. unsigned blocksize;
  2394. sector_t iblock;
  2395. unsigned length, pos;
  2396. struct inode *inode = mapping->host;
  2397. struct page *page;
  2398. struct buffer_head map_bh;
  2399. int err;
  2400. blocksize = 1 << inode->i_blkbits;
  2401. length = offset & (blocksize - 1);
  2402. /* Block boundary? Nothing to do */
  2403. if (!length)
  2404. return 0;
  2405. length = blocksize - length;
  2406. iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2407. page = grab_cache_page(mapping, index);
  2408. err = -ENOMEM;
  2409. if (!page)
  2410. goto out;
  2411. if (page_has_buffers(page)) {
  2412. has_buffers:
  2413. unlock_page(page);
  2414. page_cache_release(page);
  2415. return block_truncate_page(mapping, from, get_block);
  2416. }
  2417. /* Find the buffer that contains "offset" */
  2418. pos = blocksize;
  2419. while (offset >= pos) {
  2420. iblock++;
  2421. pos += blocksize;
  2422. }
  2423. map_bh.b_size = blocksize;
  2424. map_bh.b_state = 0;
  2425. err = get_block(inode, iblock, &map_bh, 0);
  2426. if (err)
  2427. goto unlock;
  2428. /* unmapped? It's a hole - nothing to do */
  2429. if (!buffer_mapped(&map_bh))
  2430. goto unlock;
  2431. /* Ok, it's mapped. Make sure it's up-to-date */
  2432. if (!PageUptodate(page)) {
  2433. err = mapping->a_ops->readpage(NULL, page);
  2434. if (err) {
  2435. page_cache_release(page);
  2436. goto out;
  2437. }
  2438. lock_page(page);
  2439. if (!PageUptodate(page)) {
  2440. err = -EIO;
  2441. goto unlock;
  2442. }
  2443. if (page_has_buffers(page))
  2444. goto has_buffers;
  2445. }
  2446. zero_user(page, offset, length);
  2447. set_page_dirty(page);
  2448. err = 0;
  2449. unlock:
  2450. unlock_page(page);
  2451. page_cache_release(page);
  2452. out:
  2453. return err;
  2454. }
  2455. EXPORT_SYMBOL(nobh_truncate_page);
  2456. int block_truncate_page(struct address_space *mapping,
  2457. loff_t from, get_block_t *get_block)
  2458. {
  2459. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2460. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2461. unsigned blocksize;
  2462. sector_t iblock;
  2463. unsigned length, pos;
  2464. struct inode *inode = mapping->host;
  2465. struct page *page;
  2466. struct buffer_head *bh;
  2467. int err;
  2468. blocksize = 1 << inode->i_blkbits;
  2469. length = offset & (blocksize - 1);
  2470. /* Block boundary? Nothing to do */
  2471. if (!length)
  2472. return 0;
  2473. length = blocksize - length;
  2474. iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2475. page = grab_cache_page(mapping, index);
  2476. err = -ENOMEM;
  2477. if (!page)
  2478. goto out;
  2479. if (!page_has_buffers(page))
  2480. create_empty_buffers(page, blocksize, 0);
  2481. /* Find the buffer that contains "offset" */
  2482. bh = page_buffers(page);
  2483. pos = blocksize;
  2484. while (offset >= pos) {
  2485. bh = bh->b_this_page;
  2486. iblock++;
  2487. pos += blocksize;
  2488. }
  2489. err = 0;
  2490. if (!buffer_mapped(bh)) {
  2491. WARN_ON(bh->b_size != blocksize);
  2492. err = get_block(inode, iblock, bh, 0);
  2493. if (err)
  2494. goto unlock;
  2495. /* unmapped? It's a hole - nothing to do */
  2496. if (!buffer_mapped(bh))
  2497. goto unlock;
  2498. }
  2499. /* Ok, it's mapped. Make sure it's up-to-date */
  2500. if (PageUptodate(page))
  2501. set_buffer_uptodate(bh);
  2502. if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
  2503. err = -EIO;
  2504. ll_rw_block(READ, 1, &bh);
  2505. wait_on_buffer(bh);
  2506. /* Uhhuh. Read error. Complain and punt. */
  2507. if (!buffer_uptodate(bh))
  2508. goto unlock;
  2509. }
  2510. zero_user(page, offset, length);
  2511. mark_buffer_dirty(bh);
  2512. err = 0;
  2513. unlock:
  2514. unlock_page(page);
  2515. page_cache_release(page);
  2516. out:
  2517. return err;
  2518. }
  2519. EXPORT_SYMBOL(block_truncate_page);
  2520. /*
  2521. * The generic ->writepage function for buffer-backed address_spaces
  2522. * this form passes in the end_io handler used to finish the IO.
  2523. */
  2524. int block_write_full_page_endio(struct page *page, get_block_t *get_block,
  2525. struct writeback_control *wbc, bh_end_io_t *handler)
  2526. {
  2527. struct inode * const inode = page->mapping->host;
  2528. loff_t i_size = i_size_read(inode);
  2529. const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  2530. unsigned offset;
  2531. /* Is the page fully inside i_size? */
  2532. if (page->index < end_index)
  2533. return __block_write_full_page(inode, page, get_block, wbc,
  2534. handler);
  2535. /* Is the page fully outside i_size? (truncate in progress) */
  2536. offset = i_size & (PAGE_CACHE_SIZE-1);
  2537. if (page->index >= end_index+1 || !offset) {
  2538. /*
  2539. * The page may have dirty, unmapped buffers. For example,
  2540. * they may have been added in ext3_writepage(). Make them
  2541. * freeable here, so the page does not leak.
  2542. */
  2543. do_invalidatepage(page, 0);
  2544. unlock_page(page);
  2545. return 0; /* don't care */
  2546. }
  2547. /*
  2548. * The page straddles i_size. It must be zeroed out on each and every
  2549. * writepage invocation because it may be mmapped. "A file is mapped
  2550. * in multiples of the page size. For a file that is not a multiple of
  2551. * the page size, the remaining memory is zeroed when mapped, and
  2552. * writes to that region are not written out to the file."
  2553. */
  2554. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  2555. return __block_write_full_page(inode, page, get_block, wbc, handler);
  2556. }
  2557. EXPORT_SYMBOL(block_write_full_page_endio);
  2558. /*
  2559. * The generic ->writepage function for buffer-backed address_spaces
  2560. */
  2561. int block_write_full_page(struct page *page, get_block_t *get_block,
  2562. struct writeback_control *wbc)
  2563. {
  2564. return block_write_full_page_endio(page, get_block, wbc,
  2565. end_buffer_async_write);
  2566. }
  2567. EXPORT_SYMBOL(block_write_full_page);
  2568. sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
  2569. get_block_t *get_block)
  2570. {
  2571. struct buffer_head tmp;
  2572. struct inode *inode = mapping->host;
  2573. tmp.b_state = 0;
  2574. tmp.b_blocknr = 0;
  2575. tmp.b_size = 1 << inode->i_blkbits;
  2576. get_block(inode, block, &tmp, 0);
  2577. return tmp.b_blocknr;
  2578. }
  2579. EXPORT_SYMBOL(generic_block_bmap);
  2580. static void end_bio_bh_io_sync(struct bio *bio, int err)
  2581. {
  2582. struct buffer_head *bh = bio->bi_private;
  2583. if (err == -EOPNOTSUPP) {
  2584. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  2585. }
  2586. if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
  2587. set_bit(BH_Quiet, &bh->b_state);
  2588. bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
  2589. bio_put(bio);
  2590. }
  2591. /*
  2592. * This allows us to do IO even on the odd last sectors
  2593. * of a device, even if the bh block size is some multiple
  2594. * of the physical sector size.
  2595. *
  2596. * We'll just truncate the bio to the size of the device,
  2597. * and clear the end of the buffer head manually.
  2598. *
  2599. * Truly out-of-range accesses will turn into actual IO
  2600. * errors, this only handles the "we need to be able to
  2601. * do IO at the final sector" case.
  2602. */
  2603. static void guard_bh_eod(int rw, struct bio *bio, struct buffer_head *bh)
  2604. {
  2605. sector_t maxsector;
  2606. unsigned bytes;
  2607. maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
  2608. if (!maxsector)
  2609. return;
  2610. /*
  2611. * If the *whole* IO is past the end of the device,
  2612. * let it through, and the IO layer will turn it into
  2613. * an EIO.
  2614. */
  2615. if (unlikely(bio->bi_sector >= maxsector))
  2616. return;
  2617. maxsector -= bio->bi_sector;
  2618. bytes = bio->bi_size;
  2619. if (likely((bytes >> 9) <= maxsector))
  2620. return;
  2621. /* Uhhuh. We've got a bh that straddles the device size! */
  2622. bytes = maxsector << 9;
  2623. /* Truncate the bio.. */
  2624. bio->bi_size = bytes;
  2625. bio->bi_io_vec[0].bv_len = bytes;
  2626. /* ..and clear the end of the buffer for reads */
  2627. if ((rw & RW_MASK) == READ) {
  2628. void *kaddr = kmap_atomic(bh->b_page);
  2629. memset(kaddr + bh_offset(bh) + bytes, 0, bh->b_size - bytes);
  2630. kunmap_atomic(kaddr);
  2631. flush_dcache_page(bh->b_page);
  2632. }
  2633. }
  2634. int submit_bh(int rw, struct buffer_head * bh)
  2635. {
  2636. struct bio *bio;
  2637. int ret = 0;
  2638. BUG_ON(!buffer_locked(bh));
  2639. BUG_ON(!buffer_mapped(bh));
  2640. BUG_ON(!bh->b_end_io);
  2641. BUG_ON(buffer_delay(bh));
  2642. BUG_ON(buffer_unwritten(bh));
  2643. /*
  2644. * Only clear out a write error when rewriting
  2645. */
  2646. if (test_set_buffer_req(bh) && (rw & WRITE))
  2647. clear_buffer_write_io_error(bh);
  2648. /*
  2649. * from here on down, it's all bio -- do the initial mapping,
  2650. * submit_bio -> generic_make_request may further map this bio around
  2651. */
  2652. bio = bio_alloc(GFP_NOIO, 1);
  2653. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  2654. bio->bi_bdev = bh->b_bdev;
  2655. bio->bi_io_vec[0].bv_page = bh->b_page;
  2656. bio->bi_io_vec[0].bv_len = bh->b_size;
  2657. bio->bi_io_vec[0].bv_offset = bh_offset(bh);
  2658. bio->bi_vcnt = 1;
  2659. bio->bi_idx = 0;
  2660. bio->bi_size = bh->b_size;
  2661. bio->bi_end_io = end_bio_bh_io_sync;
  2662. bio->bi_private = bh;
  2663. /* Take care of bh's that straddle the end of the device */
  2664. guard_bh_eod(rw, bio, bh);
  2665. bio_get(bio);
  2666. submit_bio(rw, bio);
  2667. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  2668. ret = -EOPNOTSUPP;
  2669. bio_put(bio);
  2670. return ret;
  2671. }
  2672. EXPORT_SYMBOL(submit_bh);
  2673. /**
  2674. * ll_rw_block: low-level access to block devices (DEPRECATED)
  2675. * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
  2676. * @nr: number of &struct buffer_heads in the array
  2677. * @bhs: array of pointers to &struct buffer_head
  2678. *
  2679. * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
  2680. * requests an I/O operation on them, either a %READ or a %WRITE. The third
  2681. * %READA option is described in the documentation for generic_make_request()
  2682. * which ll_rw_block() calls.
  2683. *
  2684. * This function drops any buffer that it cannot get a lock on (with the
  2685. * BH_Lock state bit), any buffer that appears to be clean when doing a write
  2686. * request, and any buffer that appears to be up-to-date when doing read
  2687. * request. Further it marks as clean buffers that are processed for
  2688. * writing (the buffer cache won't assume that they are actually clean
  2689. * until the buffer gets unlocked).
  2690. *
  2691. * ll_rw_block sets b_end_io to simple completion handler that marks
  2692. * the buffer up-to-date (if approriate), unlocks the buffer and wakes
  2693. * any waiters.
  2694. *
  2695. * All of the buffers must be for the same device, and must also be a
  2696. * multiple of the current approved size for the device.
  2697. */
  2698. void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
  2699. {
  2700. int i;
  2701. for (i = 0; i < nr; i++) {
  2702. struct buffer_head *bh = bhs[i];
  2703. if (!trylock_buffer(bh))
  2704. continue;
  2705. if (rw == WRITE) {
  2706. if (test_clear_buffer_dirty(bh)) {
  2707. bh->b_end_io = end_buffer_write_sync;
  2708. get_bh(bh);
  2709. submit_bh(WRITE, bh);
  2710. continue;
  2711. }
  2712. } else {
  2713. if (!buffer_uptodate(bh)) {
  2714. bh->b_end_io = end_buffer_read_sync;
  2715. get_bh(bh);
  2716. submit_bh(rw, bh);
  2717. continue;
  2718. }
  2719. }
  2720. unlock_buffer(bh);
  2721. }
  2722. }
  2723. EXPORT_SYMBOL(ll_rw_block);
  2724. void write_dirty_buffer(struct buffer_head *bh, int rw)
  2725. {
  2726. lock_buffer(bh);
  2727. if (!test_clear_buffer_dirty(bh)) {
  2728. unlock_buffer(bh);
  2729. return;
  2730. }
  2731. bh->b_end_io = end_buffer_write_sync;
  2732. get_bh(bh);
  2733. submit_bh(rw, bh);
  2734. }
  2735. EXPORT_SYMBOL(write_dirty_buffer);
  2736. /*
  2737. * For a data-integrity writeout, we need to wait upon any in-progress I/O
  2738. * and then start new I/O and then wait upon it. The caller must have a ref on
  2739. * the buffer_head.
  2740. */
  2741. int __sync_dirty_buffer(struct buffer_head *bh, int rw)
  2742. {
  2743. int ret = 0;
  2744. WARN_ON(atomic_read(&bh->b_count) < 1);
  2745. lock_buffer(bh);
  2746. if (test_clear_buffer_dirty(bh)) {
  2747. get_bh(bh);
  2748. bh->b_end_io = end_buffer_write_sync;
  2749. ret = submit_bh(rw, bh);
  2750. wait_on_buffer(bh);
  2751. if (!ret && !buffer_uptodate(bh))
  2752. ret = -EIO;
  2753. } else {
  2754. unlock_buffer(bh);
  2755. }
  2756. return ret;
  2757. }
  2758. EXPORT_SYMBOL(__sync_dirty_buffer);
  2759. int sync_dirty_buffer(struct buffer_head *bh)
  2760. {
  2761. return __sync_dirty_buffer(bh, WRITE_SYNC);
  2762. }
  2763. EXPORT_SYMBOL(sync_dirty_buffer);
  2764. /*
  2765. * try_to_free_buffers() checks if all the buffers on this particular page
  2766. * are unused, and releases them if so.
  2767. *
  2768. * Exclusion against try_to_free_buffers may be obtained by either
  2769. * locking the page or by holding its mapping's private_lock.
  2770. *
  2771. * If the page is dirty but all the buffers are clean then we need to
  2772. * be sure to mark the page clean as well. This is because the page
  2773. * may be against a block device, and a later reattachment of buffers
  2774. * to a dirty page will set *all* buffers dirty. Which would corrupt
  2775. * filesystem data on the same device.
  2776. *
  2777. * The same applies to regular filesystem pages: if all the buffers are
  2778. * clean then we set the page clean and proceed. To do that, we require
  2779. * total exclusion from __set_page_dirty_buffers(). That is obtained with
  2780. * private_lock.
  2781. *
  2782. * try_to_free_buffers() is non-blocking.
  2783. */
  2784. static inline int buffer_busy(struct buffer_head *bh)
  2785. {
  2786. return atomic_read(&bh->b_count) |
  2787. (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
  2788. }
  2789. static int
  2790. drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
  2791. {
  2792. struct buffer_head *head = page_buffers(page);
  2793. struct buffer_head *bh;
  2794. bh = head;
  2795. do {
  2796. if (buffer_write_io_error(bh) && page->mapping)
  2797. set_bit(AS_EIO, &page->mapping->flags);
  2798. if (buffer_busy(bh))
  2799. goto failed;
  2800. bh = bh->b_this_page;
  2801. } while (bh != head);
  2802. do {
  2803. struct buffer_head *next = bh->b_this_page;
  2804. if (bh->b_assoc_map)
  2805. __remove_assoc_queue(bh);
  2806. bh = next;
  2807. } while (bh != head);
  2808. *buffers_to_free = head;
  2809. __clear_page_buffers(page);
  2810. return 1;
  2811. failed:
  2812. return 0;
  2813. }
  2814. int try_to_free_buffers(struct page *page)
  2815. {
  2816. struct address_space * const mapping = page->mapping;
  2817. struct buffer_head *buffers_to_free = NULL;
  2818. int ret = 0;
  2819. BUG_ON(!PageLocked(page));
  2820. if (PageWriteback(page))
  2821. return 0;
  2822. if (mapping == NULL) { /* can this still happen? */
  2823. ret = drop_buffers(page, &buffers_to_free);
  2824. goto out;
  2825. }
  2826. spin_lock(&mapping->private_lock);
  2827. ret = drop_buffers(page, &buffers_to_free);
  2828. /*
  2829. * If the filesystem writes its buffers by hand (eg ext3)
  2830. * then we can have clean buffers against a dirty page. We
  2831. * clean the page here; otherwise the VM will never notice
  2832. * that the filesystem did any IO at all.
  2833. *
  2834. * Also, during truncate, discard_buffer will have marked all
  2835. * the page's buffers clean. We discover that here and clean
  2836. * the page also.
  2837. *
  2838. * private_lock must be held over this entire operation in order
  2839. * to synchronise against __set_page_dirty_buffers and prevent the
  2840. * dirty bit from being lost.
  2841. */
  2842. if (ret)
  2843. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  2844. spin_unlock(&mapping->private_lock);
  2845. out:
  2846. if (buffers_to_free) {
  2847. struct buffer_head *bh = buffers_to_free;
  2848. do {
  2849. struct buffer_head *next = bh->b_this_page;
  2850. free_buffer_head(bh);
  2851. bh = next;
  2852. } while (bh != buffers_to_free);
  2853. }
  2854. return ret;
  2855. }
  2856. EXPORT_SYMBOL(try_to_free_buffers);
  2857. /*
  2858. * There are no bdflush tunables left. But distributions are
  2859. * still running obsolete flush daemons, so we terminate them here.
  2860. *
  2861. * Use of bdflush() is deprecated and will be removed in a future kernel.
  2862. * The `flush-X' kernel threads fully replace bdflush daemons and this call.
  2863. */
  2864. SYSCALL_DEFINE2(bdflush, int, func, long, data)
  2865. {
  2866. static int msg_count;
  2867. if (!capable(CAP_SYS_ADMIN))
  2868. return -EPERM;
  2869. if (msg_count < 5) {
  2870. msg_count++;
  2871. printk(KERN_INFO
  2872. "warning: process `%s' used the obsolete bdflush"
  2873. " system call\n", current->comm);
  2874. printk(KERN_INFO "Fix your initscripts?\n");
  2875. }
  2876. if (func == 1)
  2877. do_exit(0);
  2878. return 0;
  2879. }
  2880. /*
  2881. * Buffer-head allocation
  2882. */
  2883. static struct kmem_cache *bh_cachep __read_mostly;
  2884. /*
  2885. * Once the number of bh's in the machine exceeds this level, we start
  2886. * stripping them in writeback.
  2887. */
  2888. static unsigned long max_buffer_heads;
  2889. int buffer_heads_over_limit;
  2890. struct bh_accounting {
  2891. int nr; /* Number of live bh's */
  2892. int ratelimit; /* Limit cacheline bouncing */
  2893. };
  2894. static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
  2895. static void recalc_bh_state(void)
  2896. {
  2897. int i;
  2898. int tot = 0;
  2899. if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
  2900. return;
  2901. __this_cpu_write(bh_accounting.ratelimit, 0);
  2902. for_each_online_cpu(i)
  2903. tot += per_cpu(bh_accounting, i).nr;
  2904. buffer_heads_over_limit = (tot > max_buffer_heads);
  2905. }
  2906. struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  2907. {
  2908. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  2909. if (ret) {
  2910. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  2911. preempt_disable();
  2912. __this_cpu_inc(bh_accounting.nr);
  2913. recalc_bh_state();
  2914. preempt_enable();
  2915. }
  2916. return ret;
  2917. }
  2918. EXPORT_SYMBOL(alloc_buffer_head);
  2919. void free_buffer_head(struct buffer_head *bh)
  2920. {
  2921. BUG_ON(!list_empty(&bh->b_assoc_buffers));
  2922. kmem_cache_free(bh_cachep, bh);
  2923. preempt_disable();
  2924. __this_cpu_dec(bh_accounting.nr);
  2925. recalc_bh_state();
  2926. preempt_enable();
  2927. }
  2928. EXPORT_SYMBOL(free_buffer_head);
  2929. static void buffer_exit_cpu(int cpu)
  2930. {
  2931. int i;
  2932. struct bh_lru *b = &per_cpu(bh_lrus, cpu);
  2933. for (i = 0; i < BH_LRU_SIZE; i++) {
  2934. brelse(b->bhs[i]);
  2935. b->bhs[i] = NULL;
  2936. }
  2937. this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
  2938. per_cpu(bh_accounting, cpu).nr = 0;
  2939. }
  2940. static int buffer_cpu_notify(struct notifier_block *self,
  2941. unsigned long action, void *hcpu)
  2942. {
  2943. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  2944. buffer_exit_cpu((unsigned long)hcpu);
  2945. return NOTIFY_OK;
  2946. }
  2947. /**
  2948. * bh_uptodate_or_lock - Test whether the buffer is uptodate
  2949. * @bh: struct buffer_head
  2950. *
  2951. * Return true if the buffer is up-to-date and false,
  2952. * with the buffer locked, if not.
  2953. */
  2954. int bh_uptodate_or_lock(struct buffer_head *bh)
  2955. {
  2956. if (!buffer_uptodate(bh)) {
  2957. lock_buffer(bh);
  2958. if (!buffer_uptodate(bh))
  2959. return 0;
  2960. unlock_buffer(bh);
  2961. }
  2962. return 1;
  2963. }
  2964. EXPORT_SYMBOL(bh_uptodate_or_lock);
  2965. /**
  2966. * bh_submit_read - Submit a locked buffer for reading
  2967. * @bh: struct buffer_head
  2968. *
  2969. * Returns zero on success and -EIO on error.
  2970. */
  2971. int bh_submit_read(struct buffer_head *bh)
  2972. {
  2973. BUG_ON(!buffer_locked(bh));
  2974. if (buffer_uptodate(bh)) {
  2975. unlock_buffer(bh);
  2976. return 0;
  2977. }
  2978. get_bh(bh);
  2979. bh->b_end_io = end_buffer_read_sync;
  2980. submit_bh(READ, bh);
  2981. wait_on_buffer(bh);
  2982. if (buffer_uptodate(bh))
  2983. return 0;
  2984. return -EIO;
  2985. }
  2986. EXPORT_SYMBOL(bh_submit_read);
  2987. void __init buffer_init(void)
  2988. {
  2989. unsigned long nrpages;
  2990. bh_cachep = kmem_cache_create("buffer_head",
  2991. sizeof(struct buffer_head), 0,
  2992. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  2993. SLAB_MEM_SPREAD),
  2994. NULL);
  2995. /*
  2996. * Limit the bh occupancy to 10% of ZONE_NORMAL
  2997. */
  2998. nrpages = (nr_free_buffer_pages() * 10) / 100;
  2999. max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
  3000. hotcpu_notifier(buffer_cpu_notify, 0);
  3001. }