buffer.c 88 KB

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