buffer.c 91 KB

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