buffer.c 85 KB

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