buffer.c 89 KB

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