buffer.c 89 KB

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