buffer.c 89 KB

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