buffer.c 88 KB

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