buffer.c 89 KB

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