buffer.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538
  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 <[email protected]>
  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. static void __evict_bh_lru(void *arg)
  1308. {
  1309. struct bh_lru *b = &get_cpu_var(bh_lrus);
  1310. struct buffer_head *bh = arg;
  1311. int i;
  1312. for (i = 0; i < BH_LRU_SIZE; i++) {
  1313. if (b->bhs[i] == bh) {
  1314. brelse(b->bhs[i]);
  1315. b->bhs[i] = NULL;
  1316. goto out;
  1317. }
  1318. }
  1319. out:
  1320. put_cpu_var(bh_lrus);
  1321. }
  1322. static bool bh_exists_in_lru(int cpu, void *arg)
  1323. {
  1324. struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
  1325. struct buffer_head *bh = arg;
  1326. int i;
  1327. for (i = 0; i < BH_LRU_SIZE; i++) {
  1328. if (b->bhs[i] == bh)
  1329. return 1;
  1330. }
  1331. return 0;
  1332. }
  1333. void invalidate_bh_lrus(void)
  1334. {
  1335. on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
  1336. }
  1337. EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
  1338. static void evict_bh_lrus(struct buffer_head *bh)
  1339. {
  1340. on_each_cpu_cond(bh_exists_in_lru, __evict_bh_lru, bh, 1, GFP_ATOMIC);
  1341. }
  1342. void set_bh_page(struct buffer_head *bh,
  1343. struct page *page, unsigned long offset)
  1344. {
  1345. bh->b_page = page;
  1346. BUG_ON(offset >= PAGE_SIZE);
  1347. if (PageHighMem(page))
  1348. /*
  1349. * This catches illegal uses and preserves the offset:
  1350. */
  1351. bh->b_data = (char *)(0 + offset);
  1352. else
  1353. bh->b_data = page_address(page) + offset;
  1354. }
  1355. EXPORT_SYMBOL(set_bh_page);
  1356. /*
  1357. * Called when truncating a buffer on a page completely.
  1358. */
  1359. /* Bits that are cleared during an invalidate */
  1360. #define BUFFER_FLAGS_DISCARD \
  1361. (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
  1362. 1 << BH_Delay | 1 << BH_Unwritten)
  1363. static void discard_buffer(struct buffer_head * bh)
  1364. {
  1365. unsigned long b_state, b_state_old;
  1366. lock_buffer(bh);
  1367. clear_buffer_dirty(bh);
  1368. bh->b_bdev = NULL;
  1369. b_state = bh->b_state;
  1370. for (;;) {
  1371. b_state_old = cmpxchg(&bh->b_state, b_state,
  1372. (b_state & ~BUFFER_FLAGS_DISCARD));
  1373. if (b_state_old == b_state)
  1374. break;
  1375. b_state = b_state_old;
  1376. }
  1377. unlock_buffer(bh);
  1378. }
  1379. /**
  1380. * block_invalidatepage - invalidate part or all of a buffer-backed page
  1381. *
  1382. * @page: the page which is affected
  1383. * @offset: start of the range to invalidate
  1384. * @length: length of the range to invalidate
  1385. *
  1386. * block_invalidatepage() is called when all or part of the page has become
  1387. * invalidated by a truncate operation.
  1388. *
  1389. * block_invalidatepage() does not have to release all buffers, but it must
  1390. * ensure that no dirty buffer is left outside @offset and that no I/O
  1391. * is underway against any of the blocks which are outside the truncation
  1392. * point. Because the caller is about to free (and possibly reuse) those
  1393. * blocks on-disk.
  1394. */
  1395. void block_invalidatepage(struct page *page, unsigned int offset,
  1396. unsigned int length)
  1397. {
  1398. struct buffer_head *head, *bh, *next;
  1399. unsigned int curr_off = 0;
  1400. unsigned int stop = length + offset;
  1401. BUG_ON(!PageLocked(page));
  1402. if (!page_has_buffers(page))
  1403. goto out;
  1404. /*
  1405. * Check for overflow
  1406. */
  1407. BUG_ON(stop > PAGE_SIZE || stop < length);
  1408. head = page_buffers(page);
  1409. bh = head;
  1410. do {
  1411. unsigned int next_off = curr_off + bh->b_size;
  1412. next = bh->b_this_page;
  1413. /*
  1414. * Are we still fully in range ?
  1415. */
  1416. if (next_off > stop)
  1417. goto out;
  1418. /*
  1419. * is this block fully invalidated?
  1420. */
  1421. if (offset <= curr_off)
  1422. discard_buffer(bh);
  1423. curr_off = next_off;
  1424. bh = next;
  1425. } while (bh != head);
  1426. /*
  1427. * We release buffers only if the entire page is being invalidated.
  1428. * The get_block cached value has been unconditionally invalidated,
  1429. * so real IO is not possible anymore.
  1430. */
  1431. if (offset == 0)
  1432. try_to_release_page(page, 0);
  1433. out:
  1434. return;
  1435. }
  1436. EXPORT_SYMBOL(block_invalidatepage);
  1437. /*
  1438. * We attach and possibly dirty the buffers atomically wrt
  1439. * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
  1440. * is already excluded via the page lock.
  1441. */
  1442. void create_empty_buffers(struct page *page,
  1443. unsigned long blocksize, unsigned long b_state)
  1444. {
  1445. struct buffer_head *bh, *head, *tail;
  1446. head = alloc_page_buffers(page, blocksize, 1);
  1447. bh = head;
  1448. do {
  1449. bh->b_state |= b_state;
  1450. tail = bh;
  1451. bh = bh->b_this_page;
  1452. } while (bh);
  1453. tail->b_this_page = head;
  1454. spin_lock(&page->mapping->private_lock);
  1455. if (PageUptodate(page) || PageDirty(page)) {
  1456. bh = head;
  1457. do {
  1458. if (PageDirty(page))
  1459. set_buffer_dirty(bh);
  1460. if (PageUptodate(page))
  1461. set_buffer_uptodate(bh);
  1462. bh = bh->b_this_page;
  1463. } while (bh != head);
  1464. }
  1465. attach_page_buffers(page, head);
  1466. spin_unlock(&page->mapping->private_lock);
  1467. }
  1468. EXPORT_SYMBOL(create_empty_buffers);
  1469. /*
  1470. * We are taking a block for data and we don't want any output from any
  1471. * buffer-cache aliases starting from return from that function and
  1472. * until the moment when something will explicitly mark the buffer
  1473. * dirty (hopefully that will not happen until we will free that block ;-)
  1474. * We don't even need to mark it not-uptodate - nobody can expect
  1475. * anything from a newly allocated buffer anyway. We used to used
  1476. * unmap_buffer() for such invalidation, but that was wrong. We definitely
  1477. * don't want to mark the alias unmapped, for example - it would confuse
  1478. * anyone who might pick it with bread() afterwards...
  1479. *
  1480. * Also.. Note that bforget() doesn't lock the buffer. So there can
  1481. * be writeout I/O going on against recently-freed buffers. We don't
  1482. * wait on that I/O in bforget() - it's more efficient to wait on the I/O
  1483. * only if we really need to. That happens here.
  1484. */
  1485. void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
  1486. {
  1487. struct buffer_head *old_bh;
  1488. might_sleep();
  1489. old_bh = __find_get_block_slow(bdev, block);
  1490. if (old_bh) {
  1491. clear_buffer_dirty(old_bh);
  1492. wait_on_buffer(old_bh);
  1493. clear_buffer_req(old_bh);
  1494. __brelse(old_bh);
  1495. }
  1496. }
  1497. EXPORT_SYMBOL(unmap_underlying_metadata);
  1498. /*
  1499. * Size is a power-of-two in the range 512..PAGE_SIZE,
  1500. * and the case we care about most is PAGE_SIZE.
  1501. *
  1502. * So this *could* possibly be written with those
  1503. * constraints in mind (relevant mostly if some
  1504. * architecture has a slow bit-scan instruction)
  1505. */
  1506. static inline int block_size_bits(unsigned int blocksize)
  1507. {
  1508. return ilog2(blocksize);
  1509. }
  1510. static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
  1511. {
  1512. BUG_ON(!PageLocked(page));
  1513. if (!page_has_buffers(page))
  1514. create_empty_buffers(page, 1 << ACCESS_ONCE(inode->i_blkbits), b_state);
  1515. return page_buffers(page);
  1516. }
  1517. /*
  1518. * NOTE! All mapped/uptodate combinations are valid:
  1519. *
  1520. * Mapped Uptodate Meaning
  1521. *
  1522. * No No "unknown" - must do get_block()
  1523. * No Yes "hole" - zero-filled
  1524. * Yes No "allocated" - allocated on disk, not read in
  1525. * Yes Yes "valid" - allocated and up-to-date in memory.
  1526. *
  1527. * "Dirty" is valid only with the last case (mapped+uptodate).
  1528. */
  1529. /*
  1530. * While block_write_full_page is writing back the dirty buffers under
  1531. * the page lock, whoever dirtied the buffers may decide to clean them
  1532. * again at any time. We handle that by only looking at the buffer
  1533. * state inside lock_buffer().
  1534. *
  1535. * If block_write_full_page() is called for regular writeback
  1536. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1537. * locked buffer. This only can happen if someone has written the buffer
  1538. * directly, with submit_bh(). At the address_space level PageWriteback
  1539. * prevents this contention from occurring.
  1540. *
  1541. * If block_write_full_page() is called with wbc->sync_mode ==
  1542. * WB_SYNC_ALL, the writes are posted using WRITE_SYNC; this
  1543. * causes the writes to be flagged as synchronous writes.
  1544. */
  1545. int __block_write_full_page(struct inode *inode, struct page *page,
  1546. get_block_t *get_block, struct writeback_control *wbc,
  1547. bh_end_io_t *handler)
  1548. {
  1549. int err;
  1550. sector_t block;
  1551. sector_t last_block;
  1552. struct buffer_head *bh, *head;
  1553. unsigned int blocksize, bbits;
  1554. int nr_underway = 0;
  1555. int write_flags = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : 0);
  1556. head = create_page_buffers(page, inode,
  1557. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1558. /*
  1559. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1560. * here, and the (potentially unmapped) buffers may become dirty at
  1561. * any time. If a buffer becomes dirty here after we've inspected it
  1562. * then we just miss that fact, and the page stays dirty.
  1563. *
  1564. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1565. * handle that here by just cleaning them.
  1566. */
  1567. bh = head;
  1568. blocksize = bh->b_size;
  1569. bbits = block_size_bits(blocksize);
  1570. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1571. last_block = (i_size_read(inode) - 1) >> bbits;
  1572. /*
  1573. * Get all the dirty buffers mapped to disk addresses and
  1574. * handle any aliases from the underlying blockdev's mapping.
  1575. */
  1576. do {
  1577. if (block > last_block) {
  1578. /*
  1579. * mapped buffers outside i_size will occur, because
  1580. * this page can be outside i_size when there is a
  1581. * truncate in progress.
  1582. */
  1583. /*
  1584. * The buffer was zeroed by block_write_full_page()
  1585. */
  1586. clear_buffer_dirty(bh);
  1587. set_buffer_uptodate(bh);
  1588. } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
  1589. buffer_dirty(bh)) {
  1590. WARN_ON(bh->b_size != blocksize);
  1591. err = get_block(inode, block, bh, 1);
  1592. if (err)
  1593. goto recover;
  1594. clear_buffer_delay(bh);
  1595. if (buffer_new(bh)) {
  1596. /* blockdev mappings never come here */
  1597. clear_buffer_new(bh);
  1598. unmap_underlying_metadata(bh->b_bdev,
  1599. bh->b_blocknr);
  1600. }
  1601. }
  1602. bh = bh->b_this_page;
  1603. block++;
  1604. } while (bh != head);
  1605. do {
  1606. if (!buffer_mapped(bh))
  1607. continue;
  1608. /*
  1609. * If it's a fully non-blocking write attempt and we cannot
  1610. * lock the buffer then redirty the page. Note that this can
  1611. * potentially cause a busy-wait loop from writeback threads
  1612. * and kswapd activity, but those code paths have their own
  1613. * higher-level throttling.
  1614. */
  1615. if (wbc->sync_mode != WB_SYNC_NONE) {
  1616. lock_buffer(bh);
  1617. } else if (!trylock_buffer(bh)) {
  1618. redirty_page_for_writepage(wbc, page);
  1619. continue;
  1620. }
  1621. if (test_clear_buffer_dirty(bh)) {
  1622. mark_buffer_async_write_endio(bh, handler);
  1623. } else {
  1624. unlock_buffer(bh);
  1625. }
  1626. } while ((bh = bh->b_this_page) != head);
  1627. /*
  1628. * The page and its buffers are protected by PageWriteback(), so we can
  1629. * drop the bh refcounts early.
  1630. */
  1631. BUG_ON(PageWriteback(page));
  1632. set_page_writeback(page);
  1633. do {
  1634. struct buffer_head *next = bh->b_this_page;
  1635. if (buffer_async_write(bh)) {
  1636. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh, 0, wbc);
  1637. nr_underway++;
  1638. }
  1639. bh = next;
  1640. } while (bh != head);
  1641. unlock_page(page);
  1642. err = 0;
  1643. done:
  1644. if (nr_underway == 0) {
  1645. /*
  1646. * The page was marked dirty, but the buffers were
  1647. * clean. Someone wrote them back by hand with
  1648. * ll_rw_block/submit_bh. A rare case.
  1649. */
  1650. end_page_writeback(page);
  1651. /*
  1652. * The page and buffer_heads can be released at any time from
  1653. * here on.
  1654. */
  1655. }
  1656. return err;
  1657. recover:
  1658. /*
  1659. * ENOSPC, or some other error. We may already have added some
  1660. * blocks to the file, so we need to write these out to avoid
  1661. * exposing stale data.
  1662. * The page is currently locked and not marked for writeback
  1663. */
  1664. bh = head;
  1665. /* Recovery: lock and submit the mapped buffers */
  1666. do {
  1667. if (buffer_mapped(bh) && buffer_dirty(bh) &&
  1668. !buffer_delay(bh)) {
  1669. lock_buffer(bh);
  1670. mark_buffer_async_write_endio(bh, handler);
  1671. } else {
  1672. /*
  1673. * The buffer may have been set dirty during
  1674. * attachment to a dirty page.
  1675. */
  1676. clear_buffer_dirty(bh);
  1677. }
  1678. } while ((bh = bh->b_this_page) != head);
  1679. SetPageError(page);
  1680. BUG_ON(PageWriteback(page));
  1681. mapping_set_error(page->mapping, err);
  1682. set_page_writeback(page);
  1683. do {
  1684. struct buffer_head *next = bh->b_this_page;
  1685. if (buffer_async_write(bh)) {
  1686. clear_buffer_dirty(bh);
  1687. submit_bh_wbc(REQ_OP_WRITE, write_flags, bh, 0, wbc);
  1688. nr_underway++;
  1689. }
  1690. bh = next;
  1691. } while (bh != head);
  1692. unlock_page(page);
  1693. goto done;
  1694. }
  1695. EXPORT_SYMBOL(__block_write_full_page);
  1696. /*
  1697. * If a page has any new buffers, zero them out here, and mark them uptodate
  1698. * and dirty so they'll be written out (in order to prevent uninitialised
  1699. * block data from leaking). And clear the new bit.
  1700. */
  1701. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1702. {
  1703. unsigned int block_start, block_end;
  1704. struct buffer_head *head, *bh;
  1705. BUG_ON(!PageLocked(page));
  1706. if (!page_has_buffers(page))
  1707. return;
  1708. bh = head = page_buffers(page);
  1709. block_start = 0;
  1710. do {
  1711. block_end = block_start + bh->b_size;
  1712. if (buffer_new(bh)) {
  1713. if (block_end > from && block_start < to) {
  1714. if (!PageUptodate(page)) {
  1715. unsigned start, size;
  1716. start = max(from, block_start);
  1717. size = min(to, block_end) - start;
  1718. zero_user(page, start, size);
  1719. set_buffer_uptodate(bh);
  1720. }
  1721. clear_buffer_new(bh);
  1722. mark_buffer_dirty(bh);
  1723. }
  1724. }
  1725. block_start = block_end;
  1726. bh = bh->b_this_page;
  1727. } while (bh != head);
  1728. }
  1729. EXPORT_SYMBOL(page_zero_new_buffers);
  1730. static void
  1731. iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
  1732. struct iomap *iomap)
  1733. {
  1734. loff_t offset = block << inode->i_blkbits;
  1735. bh->b_bdev = iomap->bdev;
  1736. /*
  1737. * Block points to offset in file we need to map, iomap contains
  1738. * the offset at which the map starts. If the map ends before the
  1739. * current block, then do not map the buffer and let the caller
  1740. * handle it.
  1741. */
  1742. BUG_ON(offset >= iomap->offset + iomap->length);
  1743. switch (iomap->type) {
  1744. case IOMAP_HOLE:
  1745. /*
  1746. * If the buffer is not up to date or beyond the current EOF,
  1747. * we need to mark it as new to ensure sub-block zeroing is
  1748. * executed if necessary.
  1749. */
  1750. if (!buffer_uptodate(bh) ||
  1751. (offset >= i_size_read(inode)))
  1752. set_buffer_new(bh);
  1753. break;
  1754. case IOMAP_DELALLOC:
  1755. if (!buffer_uptodate(bh) ||
  1756. (offset >= i_size_read(inode)))
  1757. set_buffer_new(bh);
  1758. set_buffer_uptodate(bh);
  1759. set_buffer_mapped(bh);
  1760. set_buffer_delay(bh);
  1761. break;
  1762. case IOMAP_UNWRITTEN:
  1763. /*
  1764. * For unwritten regions, we always need to ensure that
  1765. * sub-block writes cause the regions in the block we are not
  1766. * writing to are zeroed. Set the buffer as new to ensure this.
  1767. */
  1768. set_buffer_new(bh);
  1769. set_buffer_unwritten(bh);
  1770. /* FALLTHRU */
  1771. case IOMAP_MAPPED:
  1772. if (offset >= i_size_read(inode))
  1773. set_buffer_new(bh);
  1774. bh->b_blocknr = (iomap->blkno >> (inode->i_blkbits - 9)) +
  1775. ((offset - iomap->offset) >> inode->i_blkbits);
  1776. set_buffer_mapped(bh);
  1777. break;
  1778. }
  1779. }
  1780. int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
  1781. get_block_t *get_block, struct iomap *iomap)
  1782. {
  1783. unsigned from = pos & (PAGE_SIZE - 1);
  1784. unsigned to = from + len;
  1785. struct inode *inode = page->mapping->host;
  1786. unsigned block_start, block_end;
  1787. sector_t block;
  1788. int err = 0;
  1789. unsigned blocksize, bbits;
  1790. struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
  1791. BUG_ON(!PageLocked(page));
  1792. BUG_ON(from > PAGE_SIZE);
  1793. BUG_ON(to > PAGE_SIZE);
  1794. BUG_ON(from > to);
  1795. head = create_page_buffers(page, inode, 0);
  1796. blocksize = head->b_size;
  1797. bbits = block_size_bits(blocksize);
  1798. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1799. for(bh = head, block_start = 0; bh != head || !block_start;
  1800. block++, block_start=block_end, bh = bh->b_this_page) {
  1801. block_end = block_start + blocksize;
  1802. if (block_end <= from || block_start >= to) {
  1803. if (PageUptodate(page)) {
  1804. if (!buffer_uptodate(bh))
  1805. set_buffer_uptodate(bh);
  1806. }
  1807. continue;
  1808. }
  1809. if (buffer_new(bh))
  1810. clear_buffer_new(bh);
  1811. if (!buffer_mapped(bh)) {
  1812. WARN_ON(bh->b_size != blocksize);
  1813. if (get_block) {
  1814. err = get_block(inode, block, bh, 1);
  1815. if (err)
  1816. break;
  1817. } else {
  1818. iomap_to_bh(inode, block, bh, iomap);
  1819. }
  1820. if (buffer_new(bh)) {
  1821. unmap_underlying_metadata(bh->b_bdev,
  1822. bh->b_blocknr);
  1823. if (PageUptodate(page)) {
  1824. clear_buffer_new(bh);
  1825. set_buffer_uptodate(bh);
  1826. mark_buffer_dirty(bh);
  1827. continue;
  1828. }
  1829. if (block_end > to || block_start < from)
  1830. zero_user_segments(page,
  1831. to, block_end,
  1832. block_start, from);
  1833. continue;
  1834. }
  1835. }
  1836. if (PageUptodate(page)) {
  1837. if (!buffer_uptodate(bh))
  1838. set_buffer_uptodate(bh);
  1839. continue;
  1840. }
  1841. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1842. !buffer_unwritten(bh) &&
  1843. (block_start < from || block_end > to)) {
  1844. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1845. *wait_bh++=bh;
  1846. }
  1847. }
  1848. /*
  1849. * If we issued read requests - let them complete.
  1850. */
  1851. while(wait_bh > wait) {
  1852. wait_on_buffer(*--wait_bh);
  1853. if (!buffer_uptodate(*wait_bh))
  1854. err = -EIO;
  1855. }
  1856. if (unlikely(err))
  1857. page_zero_new_buffers(page, from, to);
  1858. return err;
  1859. }
  1860. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  1861. get_block_t *get_block)
  1862. {
  1863. return __block_write_begin_int(page, pos, len, get_block, NULL);
  1864. }
  1865. EXPORT_SYMBOL(__block_write_begin);
  1866. static int __block_commit_write(struct inode *inode, struct page *page,
  1867. unsigned from, unsigned to)
  1868. {
  1869. unsigned block_start, block_end;
  1870. int partial = 0;
  1871. unsigned blocksize;
  1872. struct buffer_head *bh, *head;
  1873. bh = head = page_buffers(page);
  1874. blocksize = bh->b_size;
  1875. block_start = 0;
  1876. do {
  1877. block_end = block_start + blocksize;
  1878. if (block_end <= from || block_start >= to) {
  1879. if (!buffer_uptodate(bh))
  1880. partial = 1;
  1881. } else {
  1882. set_buffer_uptodate(bh);
  1883. mark_buffer_dirty(bh);
  1884. }
  1885. clear_buffer_new(bh);
  1886. block_start = block_end;
  1887. bh = bh->b_this_page;
  1888. } while (bh != head);
  1889. /*
  1890. * If this is a partial write which happened to make all buffers
  1891. * uptodate then we can optimize away a bogus readpage() for
  1892. * the next read(). Here we 'discover' whether the page went
  1893. * uptodate as a result of this (potentially partial) write.
  1894. */
  1895. if (!partial)
  1896. SetPageUptodate(page);
  1897. return 0;
  1898. }
  1899. /*
  1900. * block_write_begin takes care of the basic task of block allocation and
  1901. * bringing partial write blocks uptodate first.
  1902. *
  1903. * The filesystem needs to handle block truncation upon failure.
  1904. */
  1905. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  1906. unsigned flags, struct page **pagep, get_block_t *get_block)
  1907. {
  1908. pgoff_t index = pos >> PAGE_SHIFT;
  1909. struct page *page;
  1910. int status;
  1911. page = grab_cache_page_write_begin(mapping, index, flags);
  1912. if (!page)
  1913. return -ENOMEM;
  1914. status = __block_write_begin(page, pos, len, get_block);
  1915. if (unlikely(status)) {
  1916. unlock_page(page);
  1917. put_page(page);
  1918. page = NULL;
  1919. }
  1920. *pagep = page;
  1921. return status;
  1922. }
  1923. EXPORT_SYMBOL(block_write_begin);
  1924. int block_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. unsigned start;
  1930. start = pos & (PAGE_SIZE - 1);
  1931. if (unlikely(copied < len)) {
  1932. /*
  1933. * The buffers that were written will now be uptodate, so we
  1934. * don't have to worry about a readpage reading them and
  1935. * overwriting a partial write. However if we have encountered
  1936. * a short write and only partially written into a buffer, it
  1937. * will not be marked uptodate, so a readpage might come in and
  1938. * destroy our partial write.
  1939. *
  1940. * Do the simplest thing, and just treat any short write to a
  1941. * non uptodate page as a zero-length write, and force the
  1942. * caller to redo the whole thing.
  1943. */
  1944. if (!PageUptodate(page))
  1945. copied = 0;
  1946. page_zero_new_buffers(page, start+copied, start+len);
  1947. }
  1948. flush_dcache_page(page);
  1949. /* This could be a short (even 0-length) commit */
  1950. __block_commit_write(inode, page, start, start+copied);
  1951. return copied;
  1952. }
  1953. EXPORT_SYMBOL(block_write_end);
  1954. int generic_write_end(struct file *file, struct address_space *mapping,
  1955. loff_t pos, unsigned len, unsigned copied,
  1956. struct page *page, void *fsdata)
  1957. {
  1958. struct inode *inode = mapping->host;
  1959. loff_t old_size = inode->i_size;
  1960. int i_size_changed = 0;
  1961. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1962. /*
  1963. * No need to use i_size_read() here, the i_size
  1964. * cannot change under us because we hold i_mutex.
  1965. *
  1966. * But it's important to update i_size while still holding page lock:
  1967. * page writeout could otherwise come in and zero beyond i_size.
  1968. */
  1969. if (pos+copied > inode->i_size) {
  1970. i_size_write(inode, pos+copied);
  1971. i_size_changed = 1;
  1972. }
  1973. unlock_page(page);
  1974. put_page(page);
  1975. if (old_size < pos)
  1976. pagecache_isize_extended(inode, old_size, pos);
  1977. /*
  1978. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1979. * makes the holding time of page lock longer. Second, it forces lock
  1980. * ordering of page lock and transaction start for journaling
  1981. * filesystems.
  1982. */
  1983. if (i_size_changed)
  1984. mark_inode_dirty(inode);
  1985. return copied;
  1986. }
  1987. EXPORT_SYMBOL(generic_write_end);
  1988. /*
  1989. * block_is_partially_uptodate checks whether buffers within a page are
  1990. * uptodate or not.
  1991. *
  1992. * Returns true if all buffers which correspond to a file portion
  1993. * we want to read are uptodate.
  1994. */
  1995. int block_is_partially_uptodate(struct page *page, unsigned long from,
  1996. unsigned long count)
  1997. {
  1998. unsigned block_start, block_end, blocksize;
  1999. unsigned to;
  2000. struct buffer_head *bh, *head;
  2001. int ret = 1;
  2002. if (!page_has_buffers(page))
  2003. return 0;
  2004. head = page_buffers(page);
  2005. blocksize = head->b_size;
  2006. to = min_t(unsigned, PAGE_SIZE - from, count);
  2007. to = from + to;
  2008. if (from < blocksize && to > PAGE_SIZE - blocksize)
  2009. return 0;
  2010. bh = head;
  2011. block_start = 0;
  2012. do {
  2013. block_end = block_start + blocksize;
  2014. if (block_end > from && block_start < to) {
  2015. if (!buffer_uptodate(bh)) {
  2016. ret = 0;
  2017. break;
  2018. }
  2019. if (block_end >= to)
  2020. break;
  2021. }
  2022. block_start = block_end;
  2023. bh = bh->b_this_page;
  2024. } while (bh != head);
  2025. return ret;
  2026. }
  2027. EXPORT_SYMBOL(block_is_partially_uptodate);
  2028. /*
  2029. * Generic "read page" function for block devices that have the normal
  2030. * get_block functionality. This is most of the block device filesystems.
  2031. * Reads the page asynchronously --- the unlock_buffer() and
  2032. * set/clear_buffer_uptodate() functions propagate buffer state into the
  2033. * page struct once IO has completed.
  2034. */
  2035. int block_read_full_page(struct page *page, get_block_t *get_block)
  2036. {
  2037. struct inode *inode = page->mapping->host;
  2038. sector_t iblock, lblock;
  2039. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  2040. unsigned int blocksize, bbits;
  2041. int nr, i;
  2042. int fully_mapped = 1;
  2043. head = create_page_buffers(page, inode, 0);
  2044. blocksize = head->b_size;
  2045. bbits = block_size_bits(blocksize);
  2046. iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
  2047. lblock = (i_size_read(inode)+blocksize-1) >> bbits;
  2048. bh = head;
  2049. nr = 0;
  2050. i = 0;
  2051. do {
  2052. if (buffer_uptodate(bh))
  2053. continue;
  2054. if (!buffer_mapped(bh)) {
  2055. int err = 0;
  2056. fully_mapped = 0;
  2057. if (iblock < lblock) {
  2058. WARN_ON(bh->b_size != blocksize);
  2059. err = get_block(inode, iblock, bh, 0);
  2060. if (err)
  2061. SetPageError(page);
  2062. }
  2063. if (!buffer_mapped(bh)) {
  2064. zero_user(page, i * blocksize, blocksize);
  2065. if (!err)
  2066. set_buffer_uptodate(bh);
  2067. continue;
  2068. }
  2069. /*
  2070. * get_block() might have updated the buffer
  2071. * synchronously
  2072. */
  2073. if (buffer_uptodate(bh))
  2074. continue;
  2075. }
  2076. arr[nr++] = bh;
  2077. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  2078. if (fully_mapped)
  2079. SetPageMappedToDisk(page);
  2080. if (!nr) {
  2081. /*
  2082. * All buffers are uptodate - we can set the page uptodate
  2083. * as well. But not if get_block() returned an error.
  2084. */
  2085. if (!PageError(page))
  2086. SetPageUptodate(page);
  2087. unlock_page(page);
  2088. return 0;
  2089. }
  2090. /* Stage two: lock the buffers */
  2091. for (i = 0; i < nr; i++) {
  2092. bh = arr[i];
  2093. lock_buffer(bh);
  2094. mark_buffer_async_read(bh);
  2095. }
  2096. /*
  2097. * Stage 3: start the IO. Check for uptodateness
  2098. * inside the buffer lock in case another process reading
  2099. * the underlying blockdev brought it uptodate (the sct fix).
  2100. */
  2101. for (i = 0; i < nr; i++) {
  2102. bh = arr[i];
  2103. if (buffer_uptodate(bh))
  2104. end_buffer_async_read(bh, 1);
  2105. else
  2106. submit_bh(REQ_OP_READ, 0, bh);
  2107. }
  2108. return 0;
  2109. }
  2110. EXPORT_SYMBOL(block_read_full_page);
  2111. /* utility function for filesystems that need to do work on expanding
  2112. * truncates. Uses filesystem pagecache writes to allow the filesystem to
  2113. * deal with the hole.
  2114. */
  2115. int generic_cont_expand_simple(struct inode *inode, loff_t size)
  2116. {
  2117. struct address_space *mapping = inode->i_mapping;
  2118. struct page *page;
  2119. void *fsdata;
  2120. int err;
  2121. err = inode_newsize_ok(inode, size);
  2122. if (err)
  2123. goto out;
  2124. err = pagecache_write_begin(NULL, mapping, size, 0,
  2125. AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
  2126. &page, &fsdata);
  2127. if (err)
  2128. goto out;
  2129. err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  2130. BUG_ON(err > 0);
  2131. out:
  2132. return err;
  2133. }
  2134. EXPORT_SYMBOL(generic_cont_expand_simple);
  2135. static int cont_expand_zero(struct file *file, struct address_space *mapping,
  2136. loff_t pos, loff_t *bytes)
  2137. {
  2138. struct inode *inode = mapping->host;
  2139. unsigned int blocksize = i_blocksize(inode);
  2140. struct page *page;
  2141. void *fsdata;
  2142. pgoff_t index, curidx;
  2143. loff_t curpos;
  2144. unsigned zerofrom, offset, len;
  2145. int err = 0;
  2146. index = pos >> PAGE_SHIFT;
  2147. offset = pos & ~PAGE_MASK;
  2148. while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
  2149. zerofrom = curpos & ~PAGE_MASK;
  2150. if (zerofrom & (blocksize-1)) {
  2151. *bytes |= (blocksize-1);
  2152. (*bytes)++;
  2153. }
  2154. len = PAGE_SIZE - 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. balance_dirty_pages_ratelimited(mapping);
  2168. if (unlikely(fatal_signal_pending(current))) {
  2169. err = -EINTR;
  2170. goto out;
  2171. }
  2172. }
  2173. /* page covers the boundary, find the boundary offset */
  2174. if (index == curidx) {
  2175. zerofrom = curpos & ~PAGE_MASK;
  2176. /* if we will expand the thing last block will be filled */
  2177. if (offset <= zerofrom) {
  2178. goto out;
  2179. }
  2180. if (zerofrom & (blocksize-1)) {
  2181. *bytes |= (blocksize-1);
  2182. (*bytes)++;
  2183. }
  2184. len = offset - zerofrom;
  2185. err = pagecache_write_begin(file, mapping, curpos, len,
  2186. AOP_FLAG_UNINTERRUPTIBLE,
  2187. &page, &fsdata);
  2188. if (err)
  2189. goto out;
  2190. zero_user(page, zerofrom, len);
  2191. err = pagecache_write_end(file, mapping, curpos, len, len,
  2192. page, fsdata);
  2193. if (err < 0)
  2194. goto out;
  2195. BUG_ON(err != len);
  2196. err = 0;
  2197. }
  2198. out:
  2199. return err;
  2200. }
  2201. /*
  2202. * For moronic filesystems that do not allow holes in file.
  2203. * We may have to extend the file.
  2204. */
  2205. int cont_write_begin(struct file *file, struct address_space *mapping,
  2206. loff_t pos, unsigned len, unsigned flags,
  2207. struct page **pagep, void **fsdata,
  2208. get_block_t *get_block, loff_t *bytes)
  2209. {
  2210. struct inode *inode = mapping->host;
  2211. unsigned int blocksize = i_blocksize(inode);
  2212. unsigned int zerofrom;
  2213. int err;
  2214. err = cont_expand_zero(file, mapping, pos, bytes);
  2215. if (err)
  2216. return err;
  2217. zerofrom = *bytes & ~PAGE_MASK;
  2218. if (pos+len > *bytes && zerofrom & (blocksize-1)) {
  2219. *bytes |= (blocksize-1);
  2220. (*bytes)++;
  2221. }
  2222. return block_write_begin(mapping, pos, len, flags, pagep, get_block);
  2223. }
  2224. EXPORT_SYMBOL(cont_write_begin);
  2225. int block_commit_write(struct page *page, unsigned from, unsigned to)
  2226. {
  2227. struct inode *inode = page->mapping->host;
  2228. __block_commit_write(inode,page,from,to);
  2229. return 0;
  2230. }
  2231. EXPORT_SYMBOL(block_commit_write);
  2232. /*
  2233. * block_page_mkwrite() is not allowed to change the file size as it gets
  2234. * called from a page fault handler when a page is first dirtied. Hence we must
  2235. * be careful to check for EOF conditions here. We set the page up correctly
  2236. * for a written page which means we get ENOSPC checking when writing into
  2237. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2238. * support these features.
  2239. *
  2240. * We are not allowed to take the i_mutex here so we have to play games to
  2241. * protect against truncate races as the page could now be beyond EOF. Because
  2242. * truncate writes the inode size before removing pages, once we have the
  2243. * page lock we can determine safely if the page is beyond EOF. If it is not
  2244. * beyond EOF, then the page is guaranteed safe against truncation until we
  2245. * unlock the page.
  2246. *
  2247. * Direct callers of this function should protect against filesystem freezing
  2248. * using sb_start_pagefault() - sb_end_pagefault() functions.
  2249. */
  2250. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2251. get_block_t get_block)
  2252. {
  2253. struct page *page = vmf->page;
  2254. struct inode *inode = file_inode(vma->vm_file);
  2255. unsigned long end;
  2256. loff_t size;
  2257. int ret;
  2258. lock_page(page);
  2259. size = i_size_read(inode);
  2260. if ((page->mapping != inode->i_mapping) ||
  2261. (page_offset(page) > size)) {
  2262. /* We overload EFAULT to mean page got truncated */
  2263. ret = -EFAULT;
  2264. goto out_unlock;
  2265. }
  2266. /* page is wholly or partially inside EOF */
  2267. if (((page->index + 1) << PAGE_SHIFT) > size)
  2268. end = size & ~PAGE_MASK;
  2269. else
  2270. end = PAGE_SIZE;
  2271. ret = __block_write_begin(page, 0, end, get_block);
  2272. if (!ret)
  2273. ret = block_commit_write(page, 0, end);
  2274. if (unlikely(ret < 0))
  2275. goto out_unlock;
  2276. set_page_dirty(page);
  2277. wait_for_stable_page(page);
  2278. return 0;
  2279. out_unlock:
  2280. unlock_page(page);
  2281. return ret;
  2282. }
  2283. EXPORT_SYMBOL(block_page_mkwrite);
  2284. /*
  2285. * nobh_write_begin()'s prereads are special: the buffer_heads are freed
  2286. * immediately, while under the page lock. So it needs a special end_io
  2287. * handler which does not touch the bh after unlocking it.
  2288. */
  2289. static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
  2290. {
  2291. __end_buffer_read_notouch(bh, uptodate);
  2292. }
  2293. /*
  2294. * Attach the singly-linked list of buffers created by nobh_write_begin, to
  2295. * the page (converting it to circular linked list and taking care of page
  2296. * dirty races).
  2297. */
  2298. static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
  2299. {
  2300. struct buffer_head *bh;
  2301. BUG_ON(!PageLocked(page));
  2302. spin_lock(&page->mapping->private_lock);
  2303. bh = head;
  2304. do {
  2305. if (PageDirty(page))
  2306. set_buffer_dirty(bh);
  2307. if (!bh->b_this_page)
  2308. bh->b_this_page = head;
  2309. bh = bh->b_this_page;
  2310. } while (bh != head);
  2311. attach_page_buffers(page, head);
  2312. spin_unlock(&page->mapping->private_lock);
  2313. }
  2314. /*
  2315. * On entry, the page is fully not uptodate.
  2316. * On exit the page is fully uptodate in the areas outside (from,to)
  2317. * The filesystem needs to handle block truncation upon failure.
  2318. */
  2319. int nobh_write_begin(struct address_space *mapping,
  2320. loff_t pos, unsigned len, unsigned flags,
  2321. struct page **pagep, void **fsdata,
  2322. get_block_t *get_block)
  2323. {
  2324. struct inode *inode = mapping->host;
  2325. const unsigned blkbits = inode->i_blkbits;
  2326. const unsigned blocksize = 1 << blkbits;
  2327. struct buffer_head *head, *bh;
  2328. struct page *page;
  2329. pgoff_t index;
  2330. unsigned from, to;
  2331. unsigned block_in_page;
  2332. unsigned block_start, block_end;
  2333. sector_t block_in_file;
  2334. int nr_reads = 0;
  2335. int ret = 0;
  2336. int is_mapped_to_disk = 1;
  2337. index = pos >> PAGE_SHIFT;
  2338. from = pos & (PAGE_SIZE - 1);
  2339. to = from + len;
  2340. page = grab_cache_page_write_begin(mapping, index, flags);
  2341. if (!page)
  2342. return -ENOMEM;
  2343. *pagep = page;
  2344. *fsdata = NULL;
  2345. if (page_has_buffers(page)) {
  2346. ret = __block_write_begin(page, pos, len, get_block);
  2347. if (unlikely(ret))
  2348. goto out_release;
  2349. return ret;
  2350. }
  2351. if (PageMappedToDisk(page))
  2352. return 0;
  2353. /*
  2354. * Allocate buffers so that we can keep track of state, and potentially
  2355. * attach them to the page if an error occurs. In the common case of
  2356. * no error, they will just be freed again without ever being attached
  2357. * to the page (which is all OK, because we're under the page lock).
  2358. *
  2359. * Be careful: the buffer linked list is a NULL terminated one, rather
  2360. * than the circular one we're used to.
  2361. */
  2362. head = alloc_page_buffers(page, blocksize, 0);
  2363. if (!head) {
  2364. ret = -ENOMEM;
  2365. goto out_release;
  2366. }
  2367. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  2368. /*
  2369. * We loop across all blocks in the page, whether or not they are
  2370. * part of the affected region. This is so we can discover if the
  2371. * page is fully mapped-to-disk.
  2372. */
  2373. for (block_start = 0, block_in_page = 0, bh = head;
  2374. block_start < PAGE_SIZE;
  2375. block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
  2376. int create;
  2377. block_end = block_start + blocksize;
  2378. bh->b_state = 0;
  2379. create = 1;
  2380. if (block_start >= to)
  2381. create = 0;
  2382. ret = get_block(inode, block_in_file + block_in_page,
  2383. bh, create);
  2384. if (ret)
  2385. goto failed;
  2386. if (!buffer_mapped(bh))
  2387. is_mapped_to_disk = 0;
  2388. if (buffer_new(bh))
  2389. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  2390. if (PageUptodate(page)) {
  2391. set_buffer_uptodate(bh);
  2392. continue;
  2393. }
  2394. if (buffer_new(bh) || !buffer_mapped(bh)) {
  2395. zero_user_segments(page, block_start, from,
  2396. to, block_end);
  2397. continue;
  2398. }
  2399. if (buffer_uptodate(bh))
  2400. continue; /* reiserfs does this */
  2401. if (block_start < from || block_end > to) {
  2402. lock_buffer(bh);
  2403. bh->b_end_io = end_buffer_read_nobh;
  2404. submit_bh(REQ_OP_READ, 0, bh);
  2405. nr_reads++;
  2406. }
  2407. }
  2408. if (nr_reads) {
  2409. /*
  2410. * The page is locked, so these buffers are protected from
  2411. * any VM or truncate activity. Hence we don't need to care
  2412. * for the buffer_head refcounts.
  2413. */
  2414. for (bh = head; bh; bh = bh->b_this_page) {
  2415. wait_on_buffer(bh);
  2416. if (!buffer_uptodate(bh))
  2417. ret = -EIO;
  2418. }
  2419. if (ret)
  2420. goto failed;
  2421. }
  2422. if (is_mapped_to_disk)
  2423. SetPageMappedToDisk(page);
  2424. *fsdata = head; /* to be released by nobh_write_end */
  2425. return 0;
  2426. failed:
  2427. BUG_ON(!ret);
  2428. /*
  2429. * Error recovery is a bit difficult. We need to zero out blocks that
  2430. * were newly allocated, and dirty them to ensure they get written out.
  2431. * Buffers need to be attached to the page at this point, otherwise
  2432. * the handling of potential IO errors during writeout would be hard
  2433. * (could try doing synchronous writeout, but what if that fails too?)
  2434. */
  2435. attach_nobh_buffers(page, head);
  2436. page_zero_new_buffers(page, from, to);
  2437. out_release:
  2438. unlock_page(page);
  2439. put_page(page);
  2440. *pagep = NULL;
  2441. return ret;
  2442. }
  2443. EXPORT_SYMBOL(nobh_write_begin);
  2444. int nobh_write_end(struct file *file, struct address_space *mapping,
  2445. loff_t pos, unsigned len, unsigned copied,
  2446. struct page *page, void *fsdata)
  2447. {
  2448. struct inode *inode = page->mapping->host;
  2449. struct buffer_head *head = fsdata;
  2450. struct buffer_head *bh;
  2451. BUG_ON(fsdata != NULL && page_has_buffers(page));
  2452. if (unlikely(copied < len) && head)
  2453. attach_nobh_buffers(page, head);
  2454. if (page_has_buffers(page))
  2455. return generic_write_end(file, mapping, pos, len,
  2456. copied, page, fsdata);
  2457. SetPageUptodate(page);
  2458. set_page_dirty(page);
  2459. if (pos+copied > inode->i_size) {
  2460. i_size_write(inode, pos+copied);
  2461. mark_inode_dirty(inode);
  2462. }
  2463. unlock_page(page);
  2464. put_page(page);
  2465. while (head) {
  2466. bh = head;
  2467. head = head->b_this_page;
  2468. free_buffer_head(bh);
  2469. }
  2470. return copied;
  2471. }
  2472. EXPORT_SYMBOL(nobh_write_end);
  2473. /*
  2474. * nobh_writepage() - based on block_full_write_page() except
  2475. * that it tries to operate without attaching bufferheads to
  2476. * the page.
  2477. */
  2478. int nobh_writepage(struct page *page, get_block_t *get_block,
  2479. struct writeback_control *wbc)
  2480. {
  2481. struct inode * const inode = page->mapping->host;
  2482. loff_t i_size = i_size_read(inode);
  2483. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2484. unsigned offset;
  2485. int ret;
  2486. /* Is the page fully inside i_size? */
  2487. if (page->index < end_index)
  2488. goto out;
  2489. /* Is the page fully outside i_size? (truncate in progress) */
  2490. offset = i_size & (PAGE_SIZE-1);
  2491. if (page->index >= end_index+1 || !offset) {
  2492. /*
  2493. * The page may have dirty, unmapped buffers. For example,
  2494. * they may have been added in ext3_writepage(). Make them
  2495. * freeable here, so the page does not leak.
  2496. */
  2497. #if 0
  2498. /* Not really sure about this - do we need this ? */
  2499. if (page->mapping->a_ops->invalidatepage)
  2500. page->mapping->a_ops->invalidatepage(page, offset);
  2501. #endif
  2502. unlock_page(page);
  2503. return 0; /* don't care */
  2504. }
  2505. /*
  2506. * The page straddles i_size. It must be zeroed out on each and every
  2507. * writepage invocation because it may be mmapped. "A file is mapped
  2508. * in multiples of the page size. For a file that is not a multiple of
  2509. * the page size, the remaining memory is zeroed when mapped, and
  2510. * writes to that region are not written out to the file."
  2511. */
  2512. zero_user_segment(page, offset, PAGE_SIZE);
  2513. out:
  2514. ret = mpage_writepage(page, get_block, wbc);
  2515. if (ret == -EAGAIN)
  2516. ret = __block_write_full_page(inode, page, get_block, wbc,
  2517. end_buffer_async_write);
  2518. return ret;
  2519. }
  2520. EXPORT_SYMBOL(nobh_writepage);
  2521. int nobh_truncate_page(struct address_space *mapping,
  2522. loff_t from, get_block_t *get_block)
  2523. {
  2524. pgoff_t index = from >> PAGE_SHIFT;
  2525. unsigned offset = from & (PAGE_SIZE-1);
  2526. unsigned blocksize;
  2527. sector_t iblock;
  2528. unsigned length, pos;
  2529. struct inode *inode = mapping->host;
  2530. struct page *page;
  2531. struct buffer_head map_bh;
  2532. int err;
  2533. blocksize = i_blocksize(inode);
  2534. length = offset & (blocksize - 1);
  2535. /* Block boundary? Nothing to do */
  2536. if (!length)
  2537. return 0;
  2538. length = blocksize - length;
  2539. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2540. page = grab_cache_page(mapping, index);
  2541. err = -ENOMEM;
  2542. if (!page)
  2543. goto out;
  2544. if (page_has_buffers(page)) {
  2545. has_buffers:
  2546. unlock_page(page);
  2547. put_page(page);
  2548. return block_truncate_page(mapping, from, get_block);
  2549. }
  2550. /* Find the buffer that contains "offset" */
  2551. pos = blocksize;
  2552. while (offset >= pos) {
  2553. iblock++;
  2554. pos += blocksize;
  2555. }
  2556. map_bh.b_size = blocksize;
  2557. map_bh.b_state = 0;
  2558. err = get_block(inode, iblock, &map_bh, 0);
  2559. if (err)
  2560. goto unlock;
  2561. /* unmapped? It's a hole - nothing to do */
  2562. if (!buffer_mapped(&map_bh))
  2563. goto unlock;
  2564. /* Ok, it's mapped. Make sure it's up-to-date */
  2565. if (!PageUptodate(page)) {
  2566. err = mapping->a_ops->readpage(NULL, page);
  2567. if (err) {
  2568. put_page(page);
  2569. goto out;
  2570. }
  2571. lock_page(page);
  2572. if (!PageUptodate(page)) {
  2573. err = -EIO;
  2574. goto unlock;
  2575. }
  2576. if (page_has_buffers(page))
  2577. goto has_buffers;
  2578. }
  2579. zero_user(page, offset, length);
  2580. set_page_dirty(page);
  2581. err = 0;
  2582. unlock:
  2583. unlock_page(page);
  2584. put_page(page);
  2585. out:
  2586. return err;
  2587. }
  2588. EXPORT_SYMBOL(nobh_truncate_page);
  2589. int block_truncate_page(struct address_space *mapping,
  2590. loff_t from, get_block_t *get_block)
  2591. {
  2592. pgoff_t index = from >> PAGE_SHIFT;
  2593. unsigned offset = from & (PAGE_SIZE-1);
  2594. unsigned blocksize;
  2595. sector_t iblock;
  2596. unsigned length, pos;
  2597. struct inode *inode = mapping->host;
  2598. struct page *page;
  2599. struct buffer_head *bh;
  2600. int err;
  2601. blocksize = i_blocksize(inode);
  2602. length = offset & (blocksize - 1);
  2603. /* Block boundary? Nothing to do */
  2604. if (!length)
  2605. return 0;
  2606. length = blocksize - length;
  2607. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2608. page = grab_cache_page(mapping, index);
  2609. err = -ENOMEM;
  2610. if (!page)
  2611. goto out;
  2612. if (!page_has_buffers(page))
  2613. create_empty_buffers(page, blocksize, 0);
  2614. /* Find the buffer that contains "offset" */
  2615. bh = page_buffers(page);
  2616. pos = blocksize;
  2617. while (offset >= pos) {
  2618. bh = bh->b_this_page;
  2619. iblock++;
  2620. pos += blocksize;
  2621. }
  2622. err = 0;
  2623. if (!buffer_mapped(bh)) {
  2624. WARN_ON(bh->b_size != blocksize);
  2625. err = get_block(inode, iblock, bh, 0);
  2626. if (err)
  2627. goto unlock;
  2628. /* unmapped? It's a hole - nothing to do */
  2629. if (!buffer_mapped(bh))
  2630. goto unlock;
  2631. }
  2632. /* Ok, it's mapped. Make sure it's up-to-date */
  2633. if (PageUptodate(page))
  2634. set_buffer_uptodate(bh);
  2635. if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
  2636. err = -EIO;
  2637. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  2638. wait_on_buffer(bh);
  2639. /* Uhhuh. Read error. Complain and punt. */
  2640. if (!buffer_uptodate(bh))
  2641. goto unlock;
  2642. }
  2643. zero_user(page, offset, length);
  2644. mark_buffer_dirty(bh);
  2645. err = 0;
  2646. unlock:
  2647. unlock_page(page);
  2648. put_page(page);
  2649. out:
  2650. return err;
  2651. }
  2652. EXPORT_SYMBOL(block_truncate_page);
  2653. /*
  2654. * The generic ->writepage function for buffer-backed address_spaces
  2655. */
  2656. int block_write_full_page(struct page *page, get_block_t *get_block,
  2657. struct writeback_control *wbc)
  2658. {
  2659. struct inode * const inode = page->mapping->host;
  2660. loff_t i_size = i_size_read(inode);
  2661. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2662. unsigned offset;
  2663. /* Is the page fully inside i_size? */
  2664. if (page->index < end_index)
  2665. return __block_write_full_page(inode, page, get_block, wbc,
  2666. end_buffer_async_write);
  2667. /* Is the page fully outside i_size? (truncate in progress) */
  2668. offset = i_size & (PAGE_SIZE-1);
  2669. if (page->index >= end_index+1 || !offset) {
  2670. /*
  2671. * The page may have dirty, unmapped buffers. For example,
  2672. * they may have been added in ext3_writepage(). Make them
  2673. * freeable here, so the page does not leak.
  2674. */
  2675. do_invalidatepage(page, 0, PAGE_SIZE);
  2676. unlock_page(page);
  2677. return 0; /* don't care */
  2678. }
  2679. /*
  2680. * The page straddles i_size. It must be zeroed out on each and every
  2681. * writepage invocation because it may be mmapped. "A file is mapped
  2682. * in multiples of the page size. For a file that is not a multiple of
  2683. * the page size, the remaining memory is zeroed when mapped, and
  2684. * writes to that region are not written out to the file."
  2685. */
  2686. zero_user_segment(page, offset, PAGE_SIZE);
  2687. return __block_write_full_page(inode, page, get_block, wbc,
  2688. end_buffer_async_write);
  2689. }
  2690. EXPORT_SYMBOL(block_write_full_page);
  2691. sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
  2692. get_block_t *get_block)
  2693. {
  2694. struct buffer_head tmp;
  2695. struct inode *inode = mapping->host;
  2696. tmp.b_state = 0;
  2697. tmp.b_blocknr = 0;
  2698. tmp.b_size = i_blocksize(inode);
  2699. get_block(inode, block, &tmp, 0);
  2700. return tmp.b_blocknr;
  2701. }
  2702. EXPORT_SYMBOL(generic_block_bmap);
  2703. static void end_bio_bh_io_sync(struct bio *bio)
  2704. {
  2705. struct buffer_head *bh = bio->bi_private;
  2706. if (unlikely(bio_flagged(bio, BIO_QUIET)))
  2707. set_bit(BH_Quiet, &bh->b_state);
  2708. bh->b_end_io(bh, !bio->bi_error);
  2709. bio_put(bio);
  2710. }
  2711. /*
  2712. * This allows us to do IO even on the odd last sectors
  2713. * of a device, even if the block size is some multiple
  2714. * of the physical sector size.
  2715. *
  2716. * We'll just truncate the bio to the size of the device,
  2717. * and clear the end of the buffer head manually.
  2718. *
  2719. * Truly out-of-range accesses will turn into actual IO
  2720. * errors, this only handles the "we need to be able to
  2721. * do IO at the final sector" case.
  2722. */
  2723. void guard_bio_eod(int op, struct bio *bio)
  2724. {
  2725. sector_t maxsector;
  2726. struct bio_vec *bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
  2727. unsigned truncated_bytes;
  2728. maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
  2729. if (!maxsector)
  2730. return;
  2731. /*
  2732. * If the *whole* IO is past the end of the device,
  2733. * let it through, and the IO layer will turn it into
  2734. * an EIO.
  2735. */
  2736. if (unlikely(bio->bi_iter.bi_sector >= maxsector))
  2737. return;
  2738. maxsector -= bio->bi_iter.bi_sector;
  2739. if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
  2740. return;
  2741. /* Uhhuh. We've got a bio that straddles the device size! */
  2742. truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
  2743. /*
  2744. * The bio contains more than one segment which spans EOD, just return
  2745. * and let IO layer turn it into an EIO
  2746. */
  2747. if (truncated_bytes > bvec->bv_len)
  2748. return;
  2749. /* Truncate the bio.. */
  2750. bio->bi_iter.bi_size -= truncated_bytes;
  2751. bvec->bv_len -= truncated_bytes;
  2752. /* ..and clear the end of the buffer for reads */
  2753. if (op == REQ_OP_READ) {
  2754. zero_user(bvec->bv_page, bvec->bv_offset + bvec->bv_len,
  2755. truncated_bytes);
  2756. }
  2757. }
  2758. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  2759. unsigned long bio_flags, struct writeback_control *wbc)
  2760. {
  2761. struct bio *bio;
  2762. BUG_ON(!buffer_locked(bh));
  2763. BUG_ON(!buffer_mapped(bh));
  2764. BUG_ON(!bh->b_end_io);
  2765. BUG_ON(buffer_delay(bh));
  2766. BUG_ON(buffer_unwritten(bh));
  2767. /*
  2768. * Only clear out a write error when rewriting
  2769. */
  2770. if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
  2771. clear_buffer_write_io_error(bh);
  2772. /*
  2773. * from here on down, it's all bio -- do the initial mapping,
  2774. * submit_bio -> generic_make_request may further map this bio around
  2775. */
  2776. bio = bio_alloc(GFP_NOIO, 1);
  2777. if (wbc) {
  2778. wbc_init_bio(wbc, bio);
  2779. wbc_account_io(wbc, bh->b_page, bh->b_size);
  2780. }
  2781. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  2782. bio->bi_bdev = bh->b_bdev;
  2783. bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  2784. BUG_ON(bio->bi_iter.bi_size != bh->b_size);
  2785. bio->bi_end_io = end_bio_bh_io_sync;
  2786. bio->bi_private = bh;
  2787. bio->bi_flags |= bio_flags;
  2788. /* Take care of bh's that straddle the end of the device */
  2789. guard_bio_eod(op, bio);
  2790. if (buffer_meta(bh))
  2791. op_flags |= REQ_META;
  2792. if (buffer_prio(bh))
  2793. op_flags |= REQ_PRIO;
  2794. bio_set_op_attrs(bio, op, op_flags);
  2795. submit_bio(bio);
  2796. return 0;
  2797. }
  2798. int _submit_bh(int op, int op_flags, struct buffer_head *bh,
  2799. unsigned long bio_flags)
  2800. {
  2801. return submit_bh_wbc(op, op_flags, bh, bio_flags, NULL);
  2802. }
  2803. EXPORT_SYMBOL_GPL(_submit_bh);
  2804. int submit_bh(int op, int op_flags, struct buffer_head *bh)
  2805. {
  2806. return submit_bh_wbc(op, op_flags, bh, 0, NULL);
  2807. }
  2808. EXPORT_SYMBOL(submit_bh);
  2809. /**
  2810. * ll_rw_block: low-level access to block devices (DEPRECATED)
  2811. * @op: whether to %READ or %WRITE
  2812. * @op_flags: rq_flag_bits
  2813. * @nr: number of &struct buffer_heads in the array
  2814. * @bhs: array of pointers to &struct buffer_head
  2815. *
  2816. * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
  2817. * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
  2818. * @op_flags contains flags modifying the detailed I/O behavior, most notably
  2819. * %REQ_RAHEAD.
  2820. *
  2821. * This function drops any buffer that it cannot get a lock on (with the
  2822. * BH_Lock state bit), any buffer that appears to be clean when doing a write
  2823. * request, and any buffer that appears to be up-to-date when doing read
  2824. * request. Further it marks as clean buffers that are processed for
  2825. * writing (the buffer cache won't assume that they are actually clean
  2826. * until the buffer gets unlocked).
  2827. *
  2828. * ll_rw_block sets b_end_io to simple completion handler that marks
  2829. * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
  2830. * any waiters.
  2831. *
  2832. * All of the buffers must be for the same device, and must also be a
  2833. * multiple of the current approved size for the device.
  2834. */
  2835. void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
  2836. {
  2837. int i;
  2838. for (i = 0; i < nr; i++) {
  2839. struct buffer_head *bh = bhs[i];
  2840. if (!trylock_buffer(bh))
  2841. continue;
  2842. if (op == WRITE) {
  2843. if (test_clear_buffer_dirty(bh)) {
  2844. bh->b_end_io = end_buffer_write_sync;
  2845. get_bh(bh);
  2846. submit_bh(op, op_flags, bh);
  2847. continue;
  2848. }
  2849. } else {
  2850. if (!buffer_uptodate(bh)) {
  2851. bh->b_end_io = end_buffer_read_sync;
  2852. get_bh(bh);
  2853. submit_bh(op, op_flags, bh);
  2854. continue;
  2855. }
  2856. }
  2857. unlock_buffer(bh);
  2858. }
  2859. }
  2860. EXPORT_SYMBOL(ll_rw_block);
  2861. void write_dirty_buffer(struct buffer_head *bh, int op_flags)
  2862. {
  2863. lock_buffer(bh);
  2864. if (!test_clear_buffer_dirty(bh)) {
  2865. unlock_buffer(bh);
  2866. return;
  2867. }
  2868. bh->b_end_io = end_buffer_write_sync;
  2869. get_bh(bh);
  2870. submit_bh(REQ_OP_WRITE, op_flags, bh);
  2871. }
  2872. EXPORT_SYMBOL(write_dirty_buffer);
  2873. /*
  2874. * For a data-integrity writeout, we need to wait upon any in-progress I/O
  2875. * and then start new I/O and then wait upon it. The caller must have a ref on
  2876. * the buffer_head.
  2877. */
  2878. int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
  2879. {
  2880. int ret = 0;
  2881. WARN_ON(atomic_read(&bh->b_count) < 1);
  2882. lock_buffer(bh);
  2883. if (test_clear_buffer_dirty(bh)) {
  2884. get_bh(bh);
  2885. bh->b_end_io = end_buffer_write_sync;
  2886. ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
  2887. wait_on_buffer(bh);
  2888. if (!ret && !buffer_uptodate(bh))
  2889. ret = -EIO;
  2890. } else {
  2891. unlock_buffer(bh);
  2892. }
  2893. return ret;
  2894. }
  2895. EXPORT_SYMBOL(__sync_dirty_buffer);
  2896. int sync_dirty_buffer(struct buffer_head *bh)
  2897. {
  2898. return __sync_dirty_buffer(bh, WRITE_SYNC);
  2899. }
  2900. EXPORT_SYMBOL(sync_dirty_buffer);
  2901. /*
  2902. * try_to_free_buffers() checks if all the buffers on this particular page
  2903. * are unused, and releases them if so.
  2904. *
  2905. * Exclusion against try_to_free_buffers may be obtained by either
  2906. * locking the page or by holding its mapping's private_lock.
  2907. *
  2908. * If the page is dirty but all the buffers are clean then we need to
  2909. * be sure to mark the page clean as well. This is because the page
  2910. * may be against a block device, and a later reattachment of buffers
  2911. * to a dirty page will set *all* buffers dirty. Which would corrupt
  2912. * filesystem data on the same device.
  2913. *
  2914. * The same applies to regular filesystem pages: if all the buffers are
  2915. * clean then we set the page clean and proceed. To do that, we require
  2916. * total exclusion from __set_page_dirty_buffers(). That is obtained with
  2917. * private_lock.
  2918. *
  2919. * try_to_free_buffers() is non-blocking.
  2920. */
  2921. static inline int buffer_busy(struct buffer_head *bh)
  2922. {
  2923. return atomic_read(&bh->b_count) |
  2924. (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
  2925. }
  2926. static int
  2927. drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
  2928. {
  2929. struct buffer_head *head = page_buffers(page);
  2930. struct buffer_head *bh;
  2931. bh = head;
  2932. do {
  2933. if (buffer_write_io_error(bh) && page->mapping)
  2934. mapping_set_error(page->mapping, -EIO);
  2935. if (buffer_busy(bh)) {
  2936. /*
  2937. * Check if the busy failure was due to an
  2938. * outstanding LRU reference
  2939. */
  2940. evict_bh_lrus(bh);
  2941. if (buffer_busy(bh))
  2942. goto failed;
  2943. }
  2944. bh = bh->b_this_page;
  2945. } while (bh != head);
  2946. do {
  2947. struct buffer_head *next = bh->b_this_page;
  2948. if (bh->b_assoc_map)
  2949. __remove_assoc_queue(bh);
  2950. bh = next;
  2951. } while (bh != head);
  2952. *buffers_to_free = head;
  2953. __clear_page_buffers(page);
  2954. return 1;
  2955. failed:
  2956. return 0;
  2957. }
  2958. int try_to_free_buffers(struct page *page)
  2959. {
  2960. struct address_space * const mapping = page->mapping;
  2961. struct buffer_head *buffers_to_free = NULL;
  2962. int ret = 0;
  2963. BUG_ON(!PageLocked(page));
  2964. if (PageWriteback(page))
  2965. return 0;
  2966. if (mapping == NULL) { /* can this still happen? */
  2967. ret = drop_buffers(page, &buffers_to_free);
  2968. goto out;
  2969. }
  2970. spin_lock(&mapping->private_lock);
  2971. ret = drop_buffers(page, &buffers_to_free);
  2972. /*
  2973. * If the filesystem writes its buffers by hand (eg ext3)
  2974. * then we can have clean buffers against a dirty page. We
  2975. * clean the page here; otherwise the VM will never notice
  2976. * that the filesystem did any IO at all.
  2977. *
  2978. * Also, during truncate, discard_buffer will have marked all
  2979. * the page's buffers clean. We discover that here and clean
  2980. * the page also.
  2981. *
  2982. * private_lock must be held over this entire operation in order
  2983. * to synchronise against __set_page_dirty_buffers and prevent the
  2984. * dirty bit from being lost.
  2985. */
  2986. if (ret)
  2987. cancel_dirty_page(page);
  2988. spin_unlock(&mapping->private_lock);
  2989. out:
  2990. if (buffers_to_free) {
  2991. struct buffer_head *bh = buffers_to_free;
  2992. do {
  2993. struct buffer_head *next = bh->b_this_page;
  2994. free_buffer_head(bh);
  2995. bh = next;
  2996. } while (bh != buffers_to_free);
  2997. }
  2998. return ret;
  2999. }
  3000. EXPORT_SYMBOL(try_to_free_buffers);
  3001. /*
  3002. * There are no bdflush tunables left. But distributions are
  3003. * still running obsolete flush daemons, so we terminate them here.
  3004. *
  3005. * Use of bdflush() is deprecated and will be removed in a future kernel.
  3006. * The `flush-X' kernel threads fully replace bdflush daemons and this call.
  3007. */
  3008. SYSCALL_DEFINE2(bdflush, int, func, long, data)
  3009. {
  3010. static int msg_count;
  3011. if (!capable(CAP_SYS_ADMIN))
  3012. return -EPERM;
  3013. if (msg_count < 5) {
  3014. msg_count++;
  3015. printk(KERN_INFO
  3016. "warning: process `%s' used the obsolete bdflush"
  3017. " system call\n", current->comm);
  3018. printk(KERN_INFO "Fix your initscripts?\n");
  3019. }
  3020. if (func == 1)
  3021. do_exit(0);
  3022. return 0;
  3023. }
  3024. /*
  3025. * Buffer-head allocation
  3026. */
  3027. static struct kmem_cache *bh_cachep __read_mostly;
  3028. /*
  3029. * Once the number of bh's in the machine exceeds this level, we start
  3030. * stripping them in writeback.
  3031. */
  3032. static unsigned long max_buffer_heads;
  3033. int buffer_heads_over_limit;
  3034. struct bh_accounting {
  3035. int nr; /* Number of live bh's */
  3036. int ratelimit; /* Limit cacheline bouncing */
  3037. };
  3038. static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
  3039. static void recalc_bh_state(void)
  3040. {
  3041. int i;
  3042. int tot = 0;
  3043. if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
  3044. return;
  3045. __this_cpu_write(bh_accounting.ratelimit, 0);
  3046. for_each_online_cpu(i)
  3047. tot += per_cpu(bh_accounting, i).nr;
  3048. buffer_heads_over_limit = (tot > max_buffer_heads);
  3049. }
  3050. struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  3051. {
  3052. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  3053. if (ret) {
  3054. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  3055. preempt_disable();
  3056. __this_cpu_inc(bh_accounting.nr);
  3057. recalc_bh_state();
  3058. preempt_enable();
  3059. }
  3060. return ret;
  3061. }
  3062. EXPORT_SYMBOL(alloc_buffer_head);
  3063. void free_buffer_head(struct buffer_head *bh)
  3064. {
  3065. BUG_ON(!list_empty(&bh->b_assoc_buffers));
  3066. kmem_cache_free(bh_cachep, bh);
  3067. preempt_disable();
  3068. __this_cpu_dec(bh_accounting.nr);
  3069. recalc_bh_state();
  3070. preempt_enable();
  3071. }
  3072. EXPORT_SYMBOL(free_buffer_head);
  3073. static void buffer_exit_cpu(int cpu)
  3074. {
  3075. int i;
  3076. struct bh_lru *b = &per_cpu(bh_lrus, cpu);
  3077. for (i = 0; i < BH_LRU_SIZE; i++) {
  3078. brelse(b->bhs[i]);
  3079. b->bhs[i] = NULL;
  3080. }
  3081. this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
  3082. per_cpu(bh_accounting, cpu).nr = 0;
  3083. }
  3084. static int buffer_cpu_notify(struct notifier_block *self,
  3085. unsigned long action, void *hcpu)
  3086. {
  3087. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  3088. buffer_exit_cpu((unsigned long)hcpu);
  3089. return NOTIFY_OK;
  3090. }
  3091. /**
  3092. * bh_uptodate_or_lock - Test whether the buffer is uptodate
  3093. * @bh: struct buffer_head
  3094. *
  3095. * Return true if the buffer is up-to-date and false,
  3096. * with the buffer locked, if not.
  3097. */
  3098. int bh_uptodate_or_lock(struct buffer_head *bh)
  3099. {
  3100. if (!buffer_uptodate(bh)) {
  3101. lock_buffer(bh);
  3102. if (!buffer_uptodate(bh))
  3103. return 0;
  3104. unlock_buffer(bh);
  3105. }
  3106. return 1;
  3107. }
  3108. EXPORT_SYMBOL(bh_uptodate_or_lock);
  3109. /**
  3110. * bh_submit_read - Submit a locked buffer for reading
  3111. * @bh: struct buffer_head
  3112. *
  3113. * Returns zero on success and -EIO on error.
  3114. */
  3115. int bh_submit_read(struct buffer_head *bh)
  3116. {
  3117. BUG_ON(!buffer_locked(bh));
  3118. if (buffer_uptodate(bh)) {
  3119. unlock_buffer(bh);
  3120. return 0;
  3121. }
  3122. get_bh(bh);
  3123. bh->b_end_io = end_buffer_read_sync;
  3124. submit_bh(REQ_OP_READ, 0, bh);
  3125. wait_on_buffer(bh);
  3126. if (buffer_uptodate(bh))
  3127. return 0;
  3128. return -EIO;
  3129. }
  3130. EXPORT_SYMBOL(bh_submit_read);
  3131. void __init buffer_init(void)
  3132. {
  3133. unsigned long nrpages;
  3134. bh_cachep = kmem_cache_create("buffer_head",
  3135. sizeof(struct buffer_head), 0,
  3136. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  3137. SLAB_MEM_SPREAD),
  3138. NULL);
  3139. /*
  3140. * Limit the bh occupancy to 10% of ZONE_NORMAL
  3141. */
  3142. nrpages = (nr_free_buffer_pages() * 10) / 100;
  3143. max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
  3144. hotcpu_notifier(buffer_cpu_notify, 0);
  3145. }