buffer.c 88 KB

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