buffer.c 87 KB

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