buffer.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488
  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/iomap.h>
  23. #include <linux/mm.h>
  24. #include <linux/percpu.h>
  25. #include <linux/slab.h>
  26. #include <linux/capability.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/file.h>
  29. #include <linux/quotaops.h>
  30. #include <linux/highmem.h>
  31. #include <linux/export.h>
  32. #include <linux/backing-dev.h>
  33. #include <linux/writeback.h>
  34. #include <linux/hash.h>
  35. #include <linux/suspend.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/task_io_accounting_ops.h>
  38. #include <linux/bio.h>
  39. #include <linux/notifier.h>
  40. #include <linux/cpu.h>
  41. #include <linux/bitops.h>
  42. #include <linux/mpage.h>
  43. #include <linux/bit_spinlock.h>
  44. #include <trace/events/block.h>
  45. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
  46. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  47. unsigned long bio_flags,
  48. struct writeback_control *wbc);
  49. #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
  50. void init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
  51. {
  52. bh->b_end_io = handler;
  53. bh->b_private = private;
  54. }
  55. EXPORT_SYMBOL(init_buffer);
  56. inline void touch_buffer(struct buffer_head *bh)
  57. {
  58. trace_block_touch_buffer(bh);
  59. mark_page_accessed(bh->b_page);
  60. }
  61. EXPORT_SYMBOL(touch_buffer);
  62. void __lock_buffer(struct buffer_head *bh)
  63. {
  64. wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
  65. }
  66. EXPORT_SYMBOL(__lock_buffer);
  67. void unlock_buffer(struct buffer_head *bh)
  68. {
  69. clear_bit_unlock(BH_Lock, &bh->b_state);
  70. smp_mb__after_atomic();
  71. wake_up_bit(&bh->b_state, BH_Lock);
  72. }
  73. EXPORT_SYMBOL(unlock_buffer);
  74. /*
  75. * Returns if the page has dirty or writeback buffers. If all the buffers
  76. * are unlocked and clean then the PageDirty information is stale. If
  77. * any of the pages are locked, it is assumed they are locked for IO.
  78. */
  79. void buffer_check_dirty_writeback(struct page *page,
  80. bool *dirty, bool *writeback)
  81. {
  82. struct buffer_head *head, *bh;
  83. *dirty = false;
  84. *writeback = false;
  85. BUG_ON(!PageLocked(page));
  86. if (!page_has_buffers(page))
  87. return;
  88. if (PageWriteback(page))
  89. *writeback = true;
  90. head = page_buffers(page);
  91. bh = head;
  92. do {
  93. if (buffer_locked(bh))
  94. *writeback = true;
  95. if (buffer_dirty(bh))
  96. *dirty = true;
  97. bh = bh->b_this_page;
  98. } while (bh != head);
  99. }
  100. EXPORT_SYMBOL(buffer_check_dirty_writeback);
  101. /*
  102. * Block until a buffer comes unlocked. This doesn't stop it
  103. * from becoming locked again - you have to lock it yourself
  104. * if you want to preserve its state.
  105. */
  106. void __wait_on_buffer(struct buffer_head * bh)
  107. {
  108. wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
  109. }
  110. EXPORT_SYMBOL(__wait_on_buffer);
  111. static void
  112. __clear_page_buffers(struct page *page)
  113. {
  114. ClearPagePrivate(page);
  115. set_page_private(page, 0);
  116. put_page(page);
  117. }
  118. static void buffer_io_error(struct buffer_head *bh, char *msg)
  119. {
  120. if (!test_bit(BH_Quiet, &bh->b_state))
  121. printk_ratelimited(KERN_ERR
  122. "Buffer I/O error on dev %pg, logical block %llu%s\n",
  123. bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
  124. }
  125. /*
  126. * End-of-IO handler helper function which does not touch the bh after
  127. * unlocking it.
  128. * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
  129. * a race there is benign: unlock_buffer() only use the bh's address for
  130. * hashing after unlocking the buffer, so it doesn't actually touch the bh
  131. * itself.
  132. */
  133. static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
  134. {
  135. if (uptodate) {
  136. set_buffer_uptodate(bh);
  137. } else {
  138. /* This happens, due to failed read-ahead attempts. */
  139. clear_buffer_uptodate(bh);
  140. }
  141. unlock_buffer(bh);
  142. }
  143. /*
  144. * Default synchronous end-of-IO handler.. Just mark it up-to-date and
  145. * unlock the buffer. This is what ll_rw_block uses too.
  146. */
  147. void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
  148. {
  149. __end_buffer_read_notouch(bh, uptodate);
  150. put_bh(bh);
  151. }
  152. EXPORT_SYMBOL(end_buffer_read_sync);
  153. void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  154. {
  155. if (uptodate) {
  156. set_buffer_uptodate(bh);
  157. } else {
  158. buffer_io_error(bh, ", lost sync page write");
  159. set_buffer_write_io_error(bh);
  160. clear_buffer_uptodate(bh);
  161. }
  162. unlock_buffer(bh);
  163. put_bh(bh);
  164. }
  165. EXPORT_SYMBOL(end_buffer_write_sync);
  166. /*
  167. * Various filesystems appear to want __find_get_block to be non-blocking.
  168. * But it's the page lock which protects the buffers. To get around this,
  169. * we get exclusion from try_to_free_buffers with the blockdev mapping's
  170. * private_lock.
  171. *
  172. * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
  173. * may be quite high. This code could TryLock the page, and if that
  174. * succeeds, there is no need to take private_lock. (But if
  175. * private_lock is contended then so is mapping->tree_lock).
  176. */
  177. static struct buffer_head *
  178. __find_get_block_slow(struct block_device *bdev, sector_t block)
  179. {
  180. struct inode *bd_inode = bdev->bd_inode;
  181. struct address_space *bd_mapping = bd_inode->i_mapping;
  182. struct buffer_head *ret = NULL;
  183. pgoff_t index;
  184. struct buffer_head *bh;
  185. struct buffer_head *head;
  186. struct page *page;
  187. int all_mapped = 1;
  188. index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
  189. page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
  190. if (!page)
  191. goto out;
  192. spin_lock(&bd_mapping->private_lock);
  193. if (!page_has_buffers(page))
  194. goto out_unlock;
  195. head = page_buffers(page);
  196. bh = head;
  197. do {
  198. if (!buffer_mapped(bh))
  199. all_mapped = 0;
  200. else if (bh->b_blocknr == block) {
  201. ret = bh;
  202. get_bh(bh);
  203. goto out_unlock;
  204. }
  205. bh = bh->b_this_page;
  206. } while (bh != head);
  207. /* we might be here because some of the buffers on this page are
  208. * not mapped. This is due to various races between
  209. * file io on the block device and getblk. It gets dealt with
  210. * elsewhere, don't buffer_error if we had some unmapped buffers
  211. */
  212. if (all_mapped) {
  213. printk("__find_get_block_slow() failed. "
  214. "block=%llu, b_blocknr=%llu\n",
  215. (unsigned long long)block,
  216. (unsigned long long)bh->b_blocknr);
  217. printk("b_state=0x%08lx, b_size=%zu\n",
  218. bh->b_state, bh->b_size);
  219. printk("device %pg blocksize: %d\n", bdev,
  220. 1 << bd_inode->i_blkbits);
  221. }
  222. out_unlock:
  223. spin_unlock(&bd_mapping->private_lock);
  224. put_page(page);
  225. out:
  226. return ret;
  227. }
  228. /*
  229. * Kick the writeback threads then try to free up some ZONE_NORMAL memory.
  230. */
  231. static void free_more_memory(void)
  232. {
  233. struct zoneref *z;
  234. int nid;
  235. wakeup_flusher_threads(1024, WB_REASON_FREE_MORE_MEM);
  236. yield();
  237. for_each_online_node(nid) {
  238. z = first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
  239. gfp_zone(GFP_NOFS), NULL);
  240. if (z->zone)
  241. try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
  242. GFP_NOFS, NULL);
  243. }
  244. }
  245. /*
  246. * I/O completion handler for block_read_full_page() - pages
  247. * which come unlocked at the end of I/O.
  248. */
  249. static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  250. {
  251. unsigned long flags;
  252. struct buffer_head *first;
  253. struct buffer_head *tmp;
  254. struct page *page;
  255. int page_uptodate = 1;
  256. BUG_ON(!buffer_async_read(bh));
  257. page = bh->b_page;
  258. if (uptodate) {
  259. set_buffer_uptodate(bh);
  260. } else {
  261. clear_buffer_uptodate(bh);
  262. buffer_io_error(bh, ", async page read");
  263. SetPageError(page);
  264. }
  265. /*
  266. * Be _very_ careful from here on. Bad things can happen if
  267. * two buffer heads end IO at almost the same time and both
  268. * decide that the page is now completely done.
  269. */
  270. first = page_buffers(page);
  271. local_irq_save(flags);
  272. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  273. clear_buffer_async_read(bh);
  274. unlock_buffer(bh);
  275. tmp = bh;
  276. do {
  277. if (!buffer_uptodate(tmp))
  278. page_uptodate = 0;
  279. if (buffer_async_read(tmp)) {
  280. BUG_ON(!buffer_locked(tmp));
  281. goto still_busy;
  282. }
  283. tmp = tmp->b_this_page;
  284. } while (tmp != bh);
  285. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  286. local_irq_restore(flags);
  287. /*
  288. * If none of the buffers had errors and they are all
  289. * uptodate then we can set the page uptodate.
  290. */
  291. if (page_uptodate && !PageError(page))
  292. SetPageUptodate(page);
  293. unlock_page(page);
  294. return;
  295. still_busy:
  296. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  297. local_irq_restore(flags);
  298. return;
  299. }
  300. /*
  301. * Completion handler for block_write_full_page() - pages which are unlocked
  302. * during I/O, and which have PageWriteback cleared upon I/O completion.
  303. */
  304. void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  305. {
  306. unsigned long flags;
  307. struct buffer_head *first;
  308. struct buffer_head *tmp;
  309. struct page *page;
  310. BUG_ON(!buffer_async_write(bh));
  311. page = bh->b_page;
  312. if (uptodate) {
  313. set_buffer_uptodate(bh);
  314. } else {
  315. buffer_io_error(bh, ", lost async page write");
  316. mapping_set_error(page->mapping, -EIO);
  317. set_buffer_write_io_error(bh);
  318. clear_buffer_uptodate(bh);
  319. SetPageError(page);
  320. }
  321. first = page_buffers(page);
  322. local_irq_save(flags);
  323. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  324. clear_buffer_async_write(bh);
  325. unlock_buffer(bh);
  326. tmp = bh->b_this_page;
  327. while (tmp != bh) {
  328. if (buffer_async_write(tmp)) {
  329. BUG_ON(!buffer_locked(tmp));
  330. goto still_busy;
  331. }
  332. tmp = tmp->b_this_page;
  333. }
  334. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  335. local_irq_restore(flags);
  336. end_page_writeback(page);
  337. return;
  338. still_busy:
  339. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  340. local_irq_restore(flags);
  341. return;
  342. }
  343. EXPORT_SYMBOL(end_buffer_async_write);
  344. /*
  345. * If a page's buffers are under async readin (end_buffer_async_read
  346. * completion) then there is a possibility that another thread of
  347. * control could lock one of the buffers after it has completed
  348. * but while some of the other buffers have not completed. This
  349. * locked buffer would confuse end_buffer_async_read() into not unlocking
  350. * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
  351. * that this buffer is not under async I/O.
  352. *
  353. * The page comes unlocked when it has no locked buffer_async buffers
  354. * left.
  355. *
  356. * PageLocked prevents anyone starting new async I/O reads any of
  357. * the buffers.
  358. *
  359. * PageWriteback is used to prevent simultaneous writeout of the same
  360. * page.
  361. *
  362. * PageLocked prevents anyone from starting writeback of a page which is
  363. * under read I/O (PageWriteback is only ever set against a locked page).
  364. */
  365. static void mark_buffer_async_read(struct buffer_head *bh)
  366. {
  367. bh->b_end_io = end_buffer_async_read;
  368. set_buffer_async_read(bh);
  369. }
  370. static void mark_buffer_async_write_endio(struct buffer_head *bh,
  371. bh_end_io_t *handler)
  372. {
  373. bh->b_end_io = handler;
  374. set_buffer_async_write(bh);
  375. }
  376. void mark_buffer_async_write(struct buffer_head *bh)
  377. {
  378. mark_buffer_async_write_endio(bh, end_buffer_async_write);
  379. }
  380. EXPORT_SYMBOL(mark_buffer_async_write);
  381. /*
  382. * fs/buffer.c contains helper functions for buffer-backed address space's
  383. * fsync functions. A common requirement for buffer-based filesystems is
  384. * that certain data from the backing blockdev needs to be written out for
  385. * a successful fsync(). For example, ext2 indirect blocks need to be
  386. * written back and waited upon before fsync() returns.
  387. *
  388. * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
  389. * inode_has_buffers() and invalidate_inode_buffers() are provided for the
  390. * management of a list of dependent buffers at ->i_mapping->private_list.
  391. *
  392. * Locking is a little subtle: try_to_free_buffers() will remove buffers
  393. * from their controlling inode's queue when they are being freed. But
  394. * try_to_free_buffers() will be operating against the *blockdev* mapping
  395. * at the time, not against the S_ISREG file which depends on those buffers.
  396. * So the locking for private_list is via the private_lock in the address_space
  397. * which backs the buffers. Which is different from the address_space
  398. * against which the buffers are listed. So for a particular address_space,
  399. * mapping->private_lock does *not* protect mapping->private_list! In fact,
  400. * mapping->private_list will always be protected by the backing blockdev's
  401. * ->private_lock.
  402. *
  403. * Which introduces a requirement: all buffers on an address_space's
  404. * ->private_list must be from the same address_space: the blockdev's.
  405. *
  406. * address_spaces which do not place buffers at ->private_list via these
  407. * utility functions are free to use private_lock and private_list for
  408. * whatever they want. The only requirement is that list_empty(private_list)
  409. * be true at clear_inode() time.
  410. *
  411. * FIXME: clear_inode should not call invalidate_inode_buffers(). The
  412. * filesystems should do that. invalidate_inode_buffers() should just go
  413. * BUG_ON(!list_empty).
  414. *
  415. * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
  416. * take an address_space, not an inode. And it should be called
  417. * mark_buffer_dirty_fsync() to clearly define why those buffers are being
  418. * queued up.
  419. *
  420. * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
  421. * list if it is already on a list. Because if the buffer is on a list,
  422. * it *must* already be on the right one. If not, the filesystem is being
  423. * silly. This will save a ton of locking. But first we have to ensure
  424. * that buffers are taken *off* the old inode's list when they are freed
  425. * (presumably in truncate). That requires careful auditing of all
  426. * filesystems (do it inside bforget()). It could also be done by bringing
  427. * b_inode back.
  428. */
  429. /*
  430. * The buffer's backing address_space's private_lock must be held
  431. */
  432. static void __remove_assoc_queue(struct buffer_head *bh)
  433. {
  434. list_del_init(&bh->b_assoc_buffers);
  435. WARN_ON(!bh->b_assoc_map);
  436. if (buffer_write_io_error(bh))
  437. set_bit(AS_EIO, &bh->b_assoc_map->flags);
  438. bh->b_assoc_map = NULL;
  439. }
  440. int inode_has_buffers(struct inode *inode)
  441. {
  442. return !list_empty(&inode->i_data.private_list);
  443. }
  444. /*
  445. * osync is designed to support O_SYNC io. It waits synchronously for
  446. * all already-submitted IO to complete, but does not queue any new
  447. * writes to the disk.
  448. *
  449. * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
  450. * you dirty the buffers, and then use osync_inode_buffers to wait for
  451. * completion. Any other dirty buffers which are not yet queued for
  452. * write will not be flushed to disk by the osync.
  453. */
  454. static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
  455. {
  456. struct buffer_head *bh;
  457. struct list_head *p;
  458. int err = 0;
  459. spin_lock(lock);
  460. repeat:
  461. list_for_each_prev(p, list) {
  462. bh = BH_ENTRY(p);
  463. if (buffer_locked(bh)) {
  464. get_bh(bh);
  465. spin_unlock(lock);
  466. wait_on_buffer(bh);
  467. if (!buffer_uptodate(bh))
  468. err = -EIO;
  469. brelse(bh);
  470. spin_lock(lock);
  471. goto repeat;
  472. }
  473. }
  474. spin_unlock(lock);
  475. return err;
  476. }
  477. static void do_thaw_one(struct super_block *sb, void *unused)
  478. {
  479. while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
  480. printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev);
  481. }
  482. static void do_thaw_all(struct work_struct *work)
  483. {
  484. iterate_supers(do_thaw_one, NULL);
  485. kfree(work);
  486. printk(KERN_WARNING "Emergency Thaw complete\n");
  487. }
  488. /**
  489. * emergency_thaw_all -- forcibly thaw every frozen filesystem
  490. *
  491. * Used for emergency unfreeze of all filesystems via SysRq
  492. */
  493. void emergency_thaw_all(void)
  494. {
  495. struct work_struct *work;
  496. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  497. if (work) {
  498. INIT_WORK(work, do_thaw_all);
  499. schedule_work(work);
  500. }
  501. }
  502. /**
  503. * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
  504. * @mapping: the mapping which wants those buffers written
  505. *
  506. * Starts I/O against the buffers at mapping->private_list, and waits upon
  507. * that I/O.
  508. *
  509. * Basically, this is a convenience function for fsync().
  510. * @mapping is a file or directory which needs those buffers to be written for
  511. * a successful fsync().
  512. */
  513. int sync_mapping_buffers(struct address_space *mapping)
  514. {
  515. struct address_space *buffer_mapping = mapping->private_data;
  516. if (buffer_mapping == NULL || list_empty(&mapping->private_list))
  517. return 0;
  518. return fsync_buffers_list(&buffer_mapping->private_lock,
  519. &mapping->private_list);
  520. }
  521. EXPORT_SYMBOL(sync_mapping_buffers);
  522. /*
  523. * Called when we've recently written block `bblock', and it is known that
  524. * `bblock' was for a buffer_boundary() buffer. This means that the block at
  525. * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
  526. * dirty, schedule it for IO. So that indirects merge nicely with their data.
  527. */
  528. void write_boundary_block(struct block_device *bdev,
  529. sector_t bblock, unsigned blocksize)
  530. {
  531. struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
  532. if (bh) {
  533. if (buffer_dirty(bh))
  534. ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
  535. put_bh(bh);
  536. }
  537. }
  538. void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
  539. {
  540. struct address_space *mapping = inode->i_mapping;
  541. struct address_space *buffer_mapping = bh->b_page->mapping;
  542. mark_buffer_dirty(bh);
  543. if (!mapping->private_data) {
  544. mapping->private_data = buffer_mapping;
  545. } else {
  546. BUG_ON(mapping->private_data != buffer_mapping);
  547. }
  548. if (!bh->b_assoc_map) {
  549. spin_lock(&buffer_mapping->private_lock);
  550. list_move_tail(&bh->b_assoc_buffers,
  551. &mapping->private_list);
  552. bh->b_assoc_map = mapping;
  553. spin_unlock(&buffer_mapping->private_lock);
  554. }
  555. }
  556. EXPORT_SYMBOL(mark_buffer_dirty_inode);
  557. /*
  558. * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
  559. * dirty.
  560. *
  561. * If warn is true, then emit a warning if the page is not uptodate and has
  562. * not been truncated.
  563. *
  564. * The caller must hold lock_page_memcg().
  565. */
  566. static void __set_page_dirty(struct page *page, struct address_space *mapping,
  567. int warn)
  568. {
  569. unsigned long flags;
  570. spin_lock_irqsave(&mapping->tree_lock, flags);
  571. if (page->mapping) { /* Race with truncate? */
  572. WARN_ON_ONCE(warn && !PageUptodate(page));
  573. account_page_dirtied(page, mapping);
  574. radix_tree_tag_set(&mapping->page_tree,
  575. page_index(page), PAGECACHE_TAG_DIRTY);
  576. }
  577. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  578. }
  579. /*
  580. * Add a page to the dirty page list.
  581. *
  582. * It is a sad fact of life that this function is called from several places
  583. * deeply under spinlocking. It may not sleep.
  584. *
  585. * If the page has buffers, the uptodate buffers are set dirty, to preserve
  586. * dirty-state coherency between the page and the buffers. It the page does
  587. * not have buffers then when they are later attached they will all be set
  588. * dirty.
  589. *
  590. * The buffers are dirtied before the page is dirtied. There's a small race
  591. * window in which a writepage caller may see the page cleanness but not the
  592. * buffer dirtiness. That's fine. If this code were to set the page dirty
  593. * before the buffers, a concurrent writepage caller could clear the page dirty
  594. * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
  595. * page on the dirty page list.
  596. *
  597. * We use private_lock to lock against try_to_free_buffers while using the
  598. * page's buffer list. Also use this to protect against clean buffers being
  599. * added to the page after it was set dirty.
  600. *
  601. * FIXME: may need to call ->reservepage here as well. That's rather up to the
  602. * address_space though.
  603. */
  604. int __set_page_dirty_buffers(struct page *page)
  605. {
  606. int newly_dirty;
  607. struct address_space *mapping = page_mapping(page);
  608. if (unlikely(!mapping))
  609. return !TestSetPageDirty(page);
  610. spin_lock(&mapping->private_lock);
  611. if (page_has_buffers(page)) {
  612. struct buffer_head *head = page_buffers(page);
  613. struct buffer_head *bh = head;
  614. do {
  615. set_buffer_dirty(bh);
  616. bh = bh->b_this_page;
  617. } while (bh != head);
  618. }
  619. /*
  620. * Lock out page->mem_cgroup migration to keep PageDirty
  621. * synchronized with per-memcg dirty page counters.
  622. */
  623. lock_page_memcg(page);
  624. newly_dirty = !TestSetPageDirty(page);
  625. spin_unlock(&mapping->private_lock);
  626. if (newly_dirty)
  627. __set_page_dirty(page, mapping, 1);
  628. unlock_page_memcg(page);
  629. if (newly_dirty)
  630. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  631. return newly_dirty;
  632. }
  633. EXPORT_SYMBOL(__set_page_dirty_buffers);
  634. /*
  635. * Write out and wait upon a list of buffers.
  636. *
  637. * We have conflicting pressures: we want to make sure that all
  638. * initially dirty buffers get waited on, but that any subsequently
  639. * dirtied buffers don't. After all, we don't want fsync to last
  640. * forever if somebody is actively writing to the file.
  641. *
  642. * Do this in two main stages: first we copy dirty buffers to a
  643. * temporary inode list, queueing the writes as we go. Then we clean
  644. * up, waiting for those writes to complete.
  645. *
  646. * During this second stage, any subsequent updates to the file may end
  647. * up refiling the buffer on the original inode's dirty list again, so
  648. * there is a chance we will end up with a buffer queued for write but
  649. * not yet completed on that list. So, as a final cleanup we go through
  650. * the osync code to catch these locked, dirty buffers without requeuing
  651. * any newly dirty buffers for write.
  652. */
  653. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
  654. {
  655. struct buffer_head *bh;
  656. struct list_head tmp;
  657. struct address_space *mapping;
  658. int err = 0, err2;
  659. struct blk_plug plug;
  660. INIT_LIST_HEAD(&tmp);
  661. blk_start_plug(&plug);
  662. spin_lock(lock);
  663. while (!list_empty(list)) {
  664. bh = BH_ENTRY(list->next);
  665. mapping = bh->b_assoc_map;
  666. __remove_assoc_queue(bh);
  667. /* Avoid race with mark_buffer_dirty_inode() which does
  668. * a lockless check and we rely on seeing the dirty bit */
  669. smp_mb();
  670. if (buffer_dirty(bh) || buffer_locked(bh)) {
  671. list_add(&bh->b_assoc_buffers, &tmp);
  672. bh->b_assoc_map = mapping;
  673. if (buffer_dirty(bh)) {
  674. get_bh(bh);
  675. spin_unlock(lock);
  676. /*
  677. * Ensure any pending I/O completes so that
  678. * write_dirty_buffer() actually writes the
  679. * current contents - it is a noop if I/O is
  680. * still in flight on potentially older
  681. * contents.
  682. */
  683. write_dirty_buffer(bh, WRITE_SYNC);
  684. /*
  685. * Kick off IO for the previous mapping. Note
  686. * that we will not run the very last mapping,
  687. * wait_on_buffer() will do that for us
  688. * through sync_buffer().
  689. */
  690. brelse(bh);
  691. spin_lock(lock);
  692. }
  693. }
  694. }
  695. spin_unlock(lock);
  696. blk_finish_plug(&plug);
  697. spin_lock(lock);
  698. while (!list_empty(&tmp)) {
  699. bh = BH_ENTRY(tmp.prev);
  700. get_bh(bh);
  701. mapping = bh->b_assoc_map;
  702. __remove_assoc_queue(bh);
  703. /* Avoid race with mark_buffer_dirty_inode() which does
  704. * a lockless check and we rely on seeing the dirty bit */
  705. smp_mb();
  706. if (buffer_dirty(bh)) {
  707. list_add(&bh->b_assoc_buffers,
  708. &mapping->private_list);
  709. bh->b_assoc_map = mapping;
  710. }
  711. spin_unlock(lock);
  712. wait_on_buffer(bh);
  713. if (!buffer_uptodate(bh))
  714. err = -EIO;
  715. brelse(bh);
  716. spin_lock(lock);
  717. }
  718. spin_unlock(lock);
  719. err2 = osync_buffers_list(lock, list);
  720. if (err)
  721. return err;
  722. else
  723. return err2;
  724. }
  725. /*
  726. * Invalidate any and all dirty buffers on a given inode. We are
  727. * probably unmounting the fs, but that doesn't mean we have already
  728. * done a sync(). Just drop the buffers from the inode list.
  729. *
  730. * NOTE: we take the inode's blockdev's mapping's private_lock. Which
  731. * assumes that all the buffers are against the blockdev. Not true
  732. * for reiserfs.
  733. */
  734. void invalidate_inode_buffers(struct inode *inode)
  735. {
  736. if (inode_has_buffers(inode)) {
  737. struct address_space *mapping = &inode->i_data;
  738. struct list_head *list = &mapping->private_list;
  739. struct address_space *buffer_mapping = mapping->private_data;
  740. spin_lock(&buffer_mapping->private_lock);
  741. while (!list_empty(list))
  742. __remove_assoc_queue(BH_ENTRY(list->next));
  743. spin_unlock(&buffer_mapping->private_lock);
  744. }
  745. }
  746. EXPORT_SYMBOL(invalidate_inode_buffers);
  747. /*
  748. * Remove any clean buffers from the inode's buffer list. This is called
  749. * when we're trying to free the inode itself. Those buffers can pin it.
  750. *
  751. * Returns true if all buffers were removed.
  752. */
  753. int remove_inode_buffers(struct inode *inode)
  754. {
  755. int ret = 1;
  756. if (inode_has_buffers(inode)) {
  757. struct address_space *mapping = &inode->i_data;
  758. struct list_head *list = &mapping->private_list;
  759. struct address_space *buffer_mapping = mapping->private_data;
  760. spin_lock(&buffer_mapping->private_lock);
  761. while (!list_empty(list)) {
  762. struct buffer_head *bh = BH_ENTRY(list->next);
  763. if (buffer_dirty(bh)) {
  764. ret = 0;
  765. break;
  766. }
  767. __remove_assoc_queue(bh);
  768. }
  769. spin_unlock(&buffer_mapping->private_lock);
  770. }
  771. return ret;
  772. }
  773. /*
  774. * Create the appropriate buffers when given a page for data area and
  775. * the size of each buffer.. Use the bh->b_this_page linked list to
  776. * follow the buffers created. Return NULL if unable to create more
  777. * buffers.
  778. *
  779. * The retry flag is used to differentiate async IO (paging, swapping)
  780. * which may not fail from ordinary buffer allocations.
  781. */
  782. struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
  783. int retry)
  784. {
  785. struct buffer_head *bh, *head;
  786. long offset;
  787. try_again:
  788. head = NULL;
  789. offset = PAGE_SIZE;
  790. while ((offset -= size) >= 0) {
  791. bh = alloc_buffer_head(GFP_NOFS);
  792. if (!bh)
  793. goto no_grow;
  794. bh->b_this_page = head;
  795. bh->b_blocknr = -1;
  796. head = bh;
  797. bh->b_size = size;
  798. /* Link the buffer to its page */
  799. set_bh_page(bh, page, offset);
  800. }
  801. return head;
  802. /*
  803. * In case anything failed, we just free everything we got.
  804. */
  805. no_grow:
  806. if (head) {
  807. do {
  808. bh = head;
  809. head = head->b_this_page;
  810. free_buffer_head(bh);
  811. } while (head);
  812. }
  813. /*
  814. * Return failure for non-async IO requests. Async IO requests
  815. * are not allowed to fail, so we have to wait until buffer heads
  816. * become available. But we don't want tasks sleeping with
  817. * partially complete buffers, so all were released above.
  818. */
  819. if (!retry)
  820. return NULL;
  821. /* We're _really_ low on memory. Now we just
  822. * wait for old buffer heads to become free due to
  823. * finishing IO. Since this is an async request and
  824. * the reserve list is empty, we're sure there are
  825. * async buffer heads in use.
  826. */
  827. free_more_memory();
  828. goto try_again;
  829. }
  830. EXPORT_SYMBOL_GPL(alloc_page_buffers);
  831. static inline void
  832. link_dev_buffers(struct page *page, struct buffer_head *head)
  833. {
  834. struct buffer_head *bh, *tail;
  835. bh = head;
  836. do {
  837. tail = bh;
  838. bh = bh->b_this_page;
  839. } while (bh);
  840. tail->b_this_page = head;
  841. attach_page_buffers(page, head);
  842. }
  843. static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
  844. {
  845. sector_t retval = ~((sector_t)0);
  846. loff_t sz = i_size_read(bdev->bd_inode);
  847. if (sz) {
  848. unsigned int sizebits = blksize_bits(size);
  849. retval = (sz >> sizebits);
  850. }
  851. return retval;
  852. }
  853. /*
  854. * Initialise the state of a blockdev page's buffers.
  855. */
  856. static sector_t
  857. init_page_buffers(struct page *page, struct block_device *bdev,
  858. sector_t block, int size)
  859. {
  860. struct buffer_head *head = page_buffers(page);
  861. struct buffer_head *bh = head;
  862. int uptodate = PageUptodate(page);
  863. sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
  864. do {
  865. if (!buffer_mapped(bh)) {
  866. init_buffer(bh, NULL, NULL);
  867. bh->b_bdev = bdev;
  868. bh->b_blocknr = block;
  869. if (uptodate)
  870. set_buffer_uptodate(bh);
  871. if (block < end_block)
  872. set_buffer_mapped(bh);
  873. }
  874. block++;
  875. bh = bh->b_this_page;
  876. } while (bh != head);
  877. /*
  878. * Caller needs to validate requested block against end of device.
  879. */
  880. return end_block;
  881. }
  882. /*
  883. * Create the page-cache page that contains the requested block.
  884. *
  885. * This is used purely for blockdev mappings.
  886. */
  887. static int
  888. grow_dev_page(struct block_device *bdev, sector_t block,
  889. pgoff_t index, int size, int sizebits, gfp_t gfp)
  890. {
  891. struct inode *inode = bdev->bd_inode;
  892. struct page *page;
  893. struct buffer_head *bh;
  894. sector_t end_block;
  895. int ret = 0; /* Will call free_more_memory() */
  896. gfp_t gfp_mask;
  897. gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
  898. /*
  899. * XXX: __getblk_slow() can not really deal with failure and
  900. * will endlessly loop on improvised global reclaim. Prefer
  901. * looping in the allocator rather than here, at least that
  902. * code knows what it's doing.
  903. */
  904. gfp_mask |= __GFP_NOFAIL;
  905. page = find_or_create_page(inode->i_mapping, index, gfp_mask);
  906. if (!page)
  907. return ret;
  908. BUG_ON(!PageLocked(page));
  909. if (page_has_buffers(page)) {
  910. bh = page_buffers(page);
  911. if (bh->b_size == size) {
  912. end_block = init_page_buffers(page, bdev,
  913. (sector_t)index << sizebits,
  914. size);
  915. goto done;
  916. }
  917. if (!try_to_free_buffers(page))
  918. goto failed;
  919. }
  920. /*
  921. * Allocate some buffers for this page
  922. */
  923. bh = alloc_page_buffers(page, size, 0);
  924. if (!bh)
  925. goto failed;
  926. /*
  927. * Link the page to the buffers and initialise them. Take the
  928. * lock to be atomic wrt __find_get_block(), which does not
  929. * run under the page lock.
  930. */
  931. spin_lock(&inode->i_mapping->private_lock);
  932. link_dev_buffers(page, bh);
  933. end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
  934. size);
  935. spin_unlock(&inode->i_mapping->private_lock);
  936. done:
  937. ret = (block < end_block) ? 1 : -ENXIO;
  938. failed:
  939. unlock_page(page);
  940. put_page(page);
  941. return ret;
  942. }
  943. /*
  944. * Create buffers for the specified block device block's page. If
  945. * that page was dirty, the buffers are set dirty also.
  946. */
  947. static int
  948. grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
  949. {
  950. pgoff_t index;
  951. int sizebits;
  952. sizebits = -1;
  953. do {
  954. sizebits++;
  955. } while ((size << sizebits) < PAGE_SIZE);
  956. index = block >> sizebits;
  957. /*
  958. * Check for a block which wants to lie outside our maximum possible
  959. * pagecache index. (this comparison is done using sector_t types).
  960. */
  961. if (unlikely(index != block >> sizebits)) {
  962. printk(KERN_ERR "%s: requested out-of-range block %llu for "
  963. "device %pg\n",
  964. __func__, (unsigned long long)block,
  965. bdev);
  966. return -EIO;
  967. }
  968. /* Create a page with the proper size buffers.. */
  969. return grow_dev_page(bdev, block, index, size, sizebits, gfp);
  970. }
  971. static struct buffer_head *
  972. __getblk_slow(struct block_device *bdev, sector_t block,
  973. unsigned size, gfp_t gfp)
  974. {
  975. /* Size must be multiple of hard sectorsize */
  976. if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
  977. (size < 512 || size > PAGE_SIZE))) {
  978. printk(KERN_ERR "getblk(): invalid block size %d requested\n",
  979. size);
  980. printk(KERN_ERR "logical block size: %d\n",
  981. bdev_logical_block_size(bdev));
  982. dump_stack();
  983. return NULL;
  984. }
  985. for (;;) {
  986. struct buffer_head *bh;
  987. int ret;
  988. bh = __find_get_block(bdev, block, size);
  989. if (bh)
  990. return bh;
  991. ret = grow_buffers(bdev, block, size, gfp);
  992. if (ret < 0)
  993. return NULL;
  994. if (ret == 0)
  995. free_more_memory();
  996. }
  997. }
  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(REQ_OP_READ, 0, 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(REQ_OP_READ, REQ_RAHEAD, 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_flags = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : 0);
  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(REQ_OP_WRITE, write_flags, 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(REQ_OP_WRITE, write_flags, 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. static void
  1700. iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
  1701. struct iomap *iomap)
  1702. {
  1703. loff_t offset = block << inode->i_blkbits;
  1704. bh->b_bdev = iomap->bdev;
  1705. /*
  1706. * Block points to offset in file we need to map, iomap contains
  1707. * the offset at which the map starts. If the map ends before the
  1708. * current block, then do not map the buffer and let the caller
  1709. * handle it.
  1710. */
  1711. BUG_ON(offset >= iomap->offset + iomap->length);
  1712. switch (iomap->type) {
  1713. case IOMAP_HOLE:
  1714. /*
  1715. * If the buffer is not up to date or beyond the current EOF,
  1716. * we need to mark it as new to ensure sub-block zeroing is
  1717. * executed if necessary.
  1718. */
  1719. if (!buffer_uptodate(bh) ||
  1720. (offset >= i_size_read(inode)))
  1721. set_buffer_new(bh);
  1722. break;
  1723. case IOMAP_DELALLOC:
  1724. if (!buffer_uptodate(bh) ||
  1725. (offset >= i_size_read(inode)))
  1726. set_buffer_new(bh);
  1727. set_buffer_uptodate(bh);
  1728. set_buffer_mapped(bh);
  1729. set_buffer_delay(bh);
  1730. break;
  1731. case IOMAP_UNWRITTEN:
  1732. /*
  1733. * For unwritten regions, we always need to ensure that
  1734. * sub-block writes cause the regions in the block we are not
  1735. * writing to are zeroed. Set the buffer as new to ensure this.
  1736. */
  1737. set_buffer_new(bh);
  1738. set_buffer_unwritten(bh);
  1739. /* FALLTHRU */
  1740. case IOMAP_MAPPED:
  1741. if (offset >= i_size_read(inode))
  1742. set_buffer_new(bh);
  1743. bh->b_blocknr = (iomap->blkno >> (inode->i_blkbits - 9)) +
  1744. ((offset - iomap->offset) >> inode->i_blkbits);
  1745. set_buffer_mapped(bh);
  1746. break;
  1747. }
  1748. }
  1749. int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
  1750. get_block_t *get_block, struct iomap *iomap)
  1751. {
  1752. unsigned from = pos & (PAGE_SIZE - 1);
  1753. unsigned to = from + len;
  1754. struct inode *inode = page->mapping->host;
  1755. unsigned block_start, block_end;
  1756. sector_t block;
  1757. int err = 0;
  1758. unsigned blocksize, bbits;
  1759. struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
  1760. BUG_ON(!PageLocked(page));
  1761. BUG_ON(from > PAGE_SIZE);
  1762. BUG_ON(to > PAGE_SIZE);
  1763. BUG_ON(from > to);
  1764. head = create_page_buffers(page, inode, 0);
  1765. blocksize = head->b_size;
  1766. bbits = block_size_bits(blocksize);
  1767. block = (sector_t)page->index << (PAGE_SHIFT - bbits);
  1768. for(bh = head, block_start = 0; bh != head || !block_start;
  1769. block++, block_start=block_end, bh = bh->b_this_page) {
  1770. block_end = block_start + blocksize;
  1771. if (block_end <= from || block_start >= to) {
  1772. if (PageUptodate(page)) {
  1773. if (!buffer_uptodate(bh))
  1774. set_buffer_uptodate(bh);
  1775. }
  1776. continue;
  1777. }
  1778. if (buffer_new(bh))
  1779. clear_buffer_new(bh);
  1780. if (!buffer_mapped(bh)) {
  1781. WARN_ON(bh->b_size != blocksize);
  1782. if (get_block) {
  1783. err = get_block(inode, block, bh, 1);
  1784. if (err)
  1785. break;
  1786. } else {
  1787. iomap_to_bh(inode, block, bh, iomap);
  1788. }
  1789. if (buffer_new(bh)) {
  1790. unmap_underlying_metadata(bh->b_bdev,
  1791. bh->b_blocknr);
  1792. if (PageUptodate(page)) {
  1793. clear_buffer_new(bh);
  1794. set_buffer_uptodate(bh);
  1795. mark_buffer_dirty(bh);
  1796. continue;
  1797. }
  1798. if (block_end > to || block_start < from)
  1799. zero_user_segments(page,
  1800. to, block_end,
  1801. block_start, from);
  1802. continue;
  1803. }
  1804. }
  1805. if (PageUptodate(page)) {
  1806. if (!buffer_uptodate(bh))
  1807. set_buffer_uptodate(bh);
  1808. continue;
  1809. }
  1810. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1811. !buffer_unwritten(bh) &&
  1812. (block_start < from || block_end > to)) {
  1813. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1814. *wait_bh++=bh;
  1815. }
  1816. }
  1817. /*
  1818. * If we issued read requests - let them complete.
  1819. */
  1820. while(wait_bh > wait) {
  1821. wait_on_buffer(*--wait_bh);
  1822. if (!buffer_uptodate(*wait_bh))
  1823. err = -EIO;
  1824. }
  1825. if (unlikely(err))
  1826. page_zero_new_buffers(page, from, to);
  1827. return err;
  1828. }
  1829. int __block_write_begin(struct page *page, loff_t pos, unsigned len,
  1830. get_block_t *get_block)
  1831. {
  1832. return __block_write_begin_int(page, pos, len, get_block, NULL);
  1833. }
  1834. EXPORT_SYMBOL(__block_write_begin);
  1835. static int __block_commit_write(struct inode *inode, struct page *page,
  1836. unsigned from, unsigned to)
  1837. {
  1838. unsigned block_start, block_end;
  1839. int partial = 0;
  1840. unsigned blocksize;
  1841. struct buffer_head *bh, *head;
  1842. bh = head = page_buffers(page);
  1843. blocksize = bh->b_size;
  1844. block_start = 0;
  1845. do {
  1846. block_end = block_start + blocksize;
  1847. if (block_end <= from || block_start >= to) {
  1848. if (!buffer_uptodate(bh))
  1849. partial = 1;
  1850. } else {
  1851. set_buffer_uptodate(bh);
  1852. mark_buffer_dirty(bh);
  1853. }
  1854. clear_buffer_new(bh);
  1855. block_start = block_end;
  1856. bh = bh->b_this_page;
  1857. } while (bh != head);
  1858. /*
  1859. * If this is a partial write which happened to make all buffers
  1860. * uptodate then we can optimize away a bogus readpage() for
  1861. * the next read(). Here we 'discover' whether the page went
  1862. * uptodate as a result of this (potentially partial) write.
  1863. */
  1864. if (!partial)
  1865. SetPageUptodate(page);
  1866. return 0;
  1867. }
  1868. /*
  1869. * block_write_begin takes care of the basic task of block allocation and
  1870. * bringing partial write blocks uptodate first.
  1871. *
  1872. * The filesystem needs to handle block truncation upon failure.
  1873. */
  1874. int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
  1875. unsigned flags, struct page **pagep, get_block_t *get_block)
  1876. {
  1877. pgoff_t index = pos >> PAGE_SHIFT;
  1878. struct page *page;
  1879. int status;
  1880. page = grab_cache_page_write_begin(mapping, index, flags);
  1881. if (!page)
  1882. return -ENOMEM;
  1883. status = __block_write_begin(page, pos, len, get_block);
  1884. if (unlikely(status)) {
  1885. unlock_page(page);
  1886. put_page(page);
  1887. page = NULL;
  1888. }
  1889. *pagep = page;
  1890. return status;
  1891. }
  1892. EXPORT_SYMBOL(block_write_begin);
  1893. int block_write_end(struct file *file, struct address_space *mapping,
  1894. loff_t pos, unsigned len, unsigned copied,
  1895. struct page *page, void *fsdata)
  1896. {
  1897. struct inode *inode = mapping->host;
  1898. unsigned start;
  1899. start = pos & (PAGE_SIZE - 1);
  1900. if (unlikely(copied < len)) {
  1901. /*
  1902. * The buffers that were written will now be uptodate, so we
  1903. * don't have to worry about a readpage reading them and
  1904. * overwriting a partial write. However if we have encountered
  1905. * a short write and only partially written into a buffer, it
  1906. * will not be marked uptodate, so a readpage might come in and
  1907. * destroy our partial write.
  1908. *
  1909. * Do the simplest thing, and just treat any short write to a
  1910. * non uptodate page as a zero-length write, and force the
  1911. * caller to redo the whole thing.
  1912. */
  1913. if (!PageUptodate(page))
  1914. copied = 0;
  1915. page_zero_new_buffers(page, start+copied, start+len);
  1916. }
  1917. flush_dcache_page(page);
  1918. /* This could be a short (even 0-length) commit */
  1919. __block_commit_write(inode, page, start, start+copied);
  1920. return copied;
  1921. }
  1922. EXPORT_SYMBOL(block_write_end);
  1923. int generic_write_end(struct file *file, struct address_space *mapping,
  1924. loff_t pos, unsigned len, unsigned copied,
  1925. struct page *page, void *fsdata)
  1926. {
  1927. struct inode *inode = mapping->host;
  1928. loff_t old_size = inode->i_size;
  1929. int i_size_changed = 0;
  1930. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1931. /*
  1932. * No need to use i_size_read() here, the i_size
  1933. * cannot change under us because we hold i_mutex.
  1934. *
  1935. * But it's important to update i_size while still holding page lock:
  1936. * page writeout could otherwise come in and zero beyond i_size.
  1937. */
  1938. if (pos+copied > inode->i_size) {
  1939. i_size_write(inode, pos+copied);
  1940. i_size_changed = 1;
  1941. }
  1942. unlock_page(page);
  1943. put_page(page);
  1944. if (old_size < pos)
  1945. pagecache_isize_extended(inode, old_size, pos);
  1946. /*
  1947. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1948. * makes the holding time of page lock longer. Second, it forces lock
  1949. * ordering of page lock and transaction start for journaling
  1950. * filesystems.
  1951. */
  1952. if (i_size_changed)
  1953. mark_inode_dirty(inode);
  1954. return copied;
  1955. }
  1956. EXPORT_SYMBOL(generic_write_end);
  1957. /*
  1958. * block_is_partially_uptodate checks whether buffers within a page are
  1959. * uptodate or not.
  1960. *
  1961. * Returns true if all buffers which correspond to a file portion
  1962. * we want to read are uptodate.
  1963. */
  1964. int block_is_partially_uptodate(struct page *page, unsigned long from,
  1965. unsigned long count)
  1966. {
  1967. unsigned block_start, block_end, blocksize;
  1968. unsigned to;
  1969. struct buffer_head *bh, *head;
  1970. int ret = 1;
  1971. if (!page_has_buffers(page))
  1972. return 0;
  1973. head = page_buffers(page);
  1974. blocksize = head->b_size;
  1975. to = min_t(unsigned, PAGE_SIZE - from, count);
  1976. to = from + to;
  1977. if (from < blocksize && to > PAGE_SIZE - blocksize)
  1978. return 0;
  1979. bh = head;
  1980. block_start = 0;
  1981. do {
  1982. block_end = block_start + blocksize;
  1983. if (block_end > from && block_start < to) {
  1984. if (!buffer_uptodate(bh)) {
  1985. ret = 0;
  1986. break;
  1987. }
  1988. if (block_end >= to)
  1989. break;
  1990. }
  1991. block_start = block_end;
  1992. bh = bh->b_this_page;
  1993. } while (bh != head);
  1994. return ret;
  1995. }
  1996. EXPORT_SYMBOL(block_is_partially_uptodate);
  1997. /*
  1998. * Generic "read page" function for block devices that have the normal
  1999. * get_block functionality. This is most of the block device filesystems.
  2000. * Reads the page asynchronously --- the unlock_buffer() and
  2001. * set/clear_buffer_uptodate() functions propagate buffer state into the
  2002. * page struct once IO has completed.
  2003. */
  2004. int block_read_full_page(struct page *page, get_block_t *get_block)
  2005. {
  2006. struct inode *inode = page->mapping->host;
  2007. sector_t iblock, lblock;
  2008. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  2009. unsigned int blocksize, bbits;
  2010. int nr, i;
  2011. int fully_mapped = 1;
  2012. head = create_page_buffers(page, inode, 0);
  2013. blocksize = head->b_size;
  2014. bbits = block_size_bits(blocksize);
  2015. iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
  2016. lblock = (i_size_read(inode)+blocksize-1) >> bbits;
  2017. bh = head;
  2018. nr = 0;
  2019. i = 0;
  2020. do {
  2021. if (buffer_uptodate(bh))
  2022. continue;
  2023. if (!buffer_mapped(bh)) {
  2024. int err = 0;
  2025. fully_mapped = 0;
  2026. if (iblock < lblock) {
  2027. WARN_ON(bh->b_size != blocksize);
  2028. err = get_block(inode, iblock, bh, 0);
  2029. if (err)
  2030. SetPageError(page);
  2031. }
  2032. if (!buffer_mapped(bh)) {
  2033. zero_user(page, i * blocksize, blocksize);
  2034. if (!err)
  2035. set_buffer_uptodate(bh);
  2036. continue;
  2037. }
  2038. /*
  2039. * get_block() might have updated the buffer
  2040. * synchronously
  2041. */
  2042. if (buffer_uptodate(bh))
  2043. continue;
  2044. }
  2045. arr[nr++] = bh;
  2046. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  2047. if (fully_mapped)
  2048. SetPageMappedToDisk(page);
  2049. if (!nr) {
  2050. /*
  2051. * All buffers are uptodate - we can set the page uptodate
  2052. * as well. But not if get_block() returned an error.
  2053. */
  2054. if (!PageError(page))
  2055. SetPageUptodate(page);
  2056. unlock_page(page);
  2057. return 0;
  2058. }
  2059. /* Stage two: lock the buffers */
  2060. for (i = 0; i < nr; i++) {
  2061. bh = arr[i];
  2062. lock_buffer(bh);
  2063. mark_buffer_async_read(bh);
  2064. }
  2065. /*
  2066. * Stage 3: start the IO. Check for uptodateness
  2067. * inside the buffer lock in case another process reading
  2068. * the underlying blockdev brought it uptodate (the sct fix).
  2069. */
  2070. for (i = 0; i < nr; i++) {
  2071. bh = arr[i];
  2072. if (buffer_uptodate(bh))
  2073. end_buffer_async_read(bh, 1);
  2074. else
  2075. submit_bh(REQ_OP_READ, 0, bh);
  2076. }
  2077. return 0;
  2078. }
  2079. EXPORT_SYMBOL(block_read_full_page);
  2080. /* utility function for filesystems that need to do work on expanding
  2081. * truncates. Uses filesystem pagecache writes to allow the filesystem to
  2082. * deal with the hole.
  2083. */
  2084. int generic_cont_expand_simple(struct inode *inode, loff_t size)
  2085. {
  2086. struct address_space *mapping = inode->i_mapping;
  2087. struct page *page;
  2088. void *fsdata;
  2089. int err;
  2090. err = inode_newsize_ok(inode, size);
  2091. if (err)
  2092. goto out;
  2093. err = pagecache_write_begin(NULL, mapping, size, 0,
  2094. AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
  2095. &page, &fsdata);
  2096. if (err)
  2097. goto out;
  2098. err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
  2099. BUG_ON(err > 0);
  2100. out:
  2101. return err;
  2102. }
  2103. EXPORT_SYMBOL(generic_cont_expand_simple);
  2104. static int cont_expand_zero(struct file *file, struct address_space *mapping,
  2105. loff_t pos, loff_t *bytes)
  2106. {
  2107. struct inode *inode = mapping->host;
  2108. unsigned int blocksize = i_blocksize(inode);
  2109. struct page *page;
  2110. void *fsdata;
  2111. pgoff_t index, curidx;
  2112. loff_t curpos;
  2113. unsigned zerofrom, offset, len;
  2114. int err = 0;
  2115. index = pos >> PAGE_SHIFT;
  2116. offset = pos & ~PAGE_MASK;
  2117. while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
  2118. zerofrom = curpos & ~PAGE_MASK;
  2119. if (zerofrom & (blocksize-1)) {
  2120. *bytes |= (blocksize-1);
  2121. (*bytes)++;
  2122. }
  2123. len = PAGE_SIZE - zerofrom;
  2124. err = pagecache_write_begin(file, mapping, curpos, len,
  2125. AOP_FLAG_UNINTERRUPTIBLE,
  2126. &page, &fsdata);
  2127. if (err)
  2128. goto out;
  2129. zero_user(page, zerofrom, len);
  2130. err = pagecache_write_end(file, mapping, curpos, len, len,
  2131. page, fsdata);
  2132. if (err < 0)
  2133. goto out;
  2134. BUG_ON(err != len);
  2135. err = 0;
  2136. balance_dirty_pages_ratelimited(mapping);
  2137. if (unlikely(fatal_signal_pending(current))) {
  2138. err = -EINTR;
  2139. goto out;
  2140. }
  2141. }
  2142. /* page covers the boundary, find the boundary offset */
  2143. if (index == curidx) {
  2144. zerofrom = curpos & ~PAGE_MASK;
  2145. /* if we will expand the thing last block will be filled */
  2146. if (offset <= zerofrom) {
  2147. goto out;
  2148. }
  2149. if (zerofrom & (blocksize-1)) {
  2150. *bytes |= (blocksize-1);
  2151. (*bytes)++;
  2152. }
  2153. len = offset - zerofrom;
  2154. err = pagecache_write_begin(file, mapping, curpos, len,
  2155. AOP_FLAG_UNINTERRUPTIBLE,
  2156. &page, &fsdata);
  2157. if (err)
  2158. goto out;
  2159. zero_user(page, zerofrom, len);
  2160. err = pagecache_write_end(file, mapping, curpos, len, len,
  2161. page, fsdata);
  2162. if (err < 0)
  2163. goto out;
  2164. BUG_ON(err != len);
  2165. err = 0;
  2166. }
  2167. out:
  2168. return err;
  2169. }
  2170. /*
  2171. * For moronic filesystems that do not allow holes in file.
  2172. * We may have to extend the file.
  2173. */
  2174. int cont_write_begin(struct file *file, struct address_space *mapping,
  2175. loff_t pos, unsigned len, unsigned flags,
  2176. struct page **pagep, void **fsdata,
  2177. get_block_t *get_block, loff_t *bytes)
  2178. {
  2179. struct inode *inode = mapping->host;
  2180. unsigned int blocksize = i_blocksize(inode);
  2181. unsigned int zerofrom;
  2182. int err;
  2183. err = cont_expand_zero(file, mapping, pos, bytes);
  2184. if (err)
  2185. return err;
  2186. zerofrom = *bytes & ~PAGE_MASK;
  2187. if (pos+len > *bytes && zerofrom & (blocksize-1)) {
  2188. *bytes |= (blocksize-1);
  2189. (*bytes)++;
  2190. }
  2191. return block_write_begin(mapping, pos, len, flags, pagep, get_block);
  2192. }
  2193. EXPORT_SYMBOL(cont_write_begin);
  2194. int block_commit_write(struct page *page, unsigned from, unsigned to)
  2195. {
  2196. struct inode *inode = page->mapping->host;
  2197. __block_commit_write(inode,page,from,to);
  2198. return 0;
  2199. }
  2200. EXPORT_SYMBOL(block_commit_write);
  2201. /*
  2202. * block_page_mkwrite() is not allowed to change the file size as it gets
  2203. * called from a page fault handler when a page is first dirtied. Hence we must
  2204. * be careful to check for EOF conditions here. We set the page up correctly
  2205. * for a written page which means we get ENOSPC checking when writing into
  2206. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2207. * support these features.
  2208. *
  2209. * We are not allowed to take the i_mutex here so we have to play games to
  2210. * protect against truncate races as the page could now be beyond EOF. Because
  2211. * truncate writes the inode size before removing pages, once we have the
  2212. * page lock we can determine safely if the page is beyond EOF. If it is not
  2213. * beyond EOF, then the page is guaranteed safe against truncation until we
  2214. * unlock the page.
  2215. *
  2216. * Direct callers of this function should protect against filesystem freezing
  2217. * using sb_start_pagefault() - sb_end_pagefault() functions.
  2218. */
  2219. int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
  2220. get_block_t get_block)
  2221. {
  2222. struct page *page = vmf->page;
  2223. struct inode *inode = file_inode(vma->vm_file);
  2224. unsigned long end;
  2225. loff_t size;
  2226. int ret;
  2227. lock_page(page);
  2228. size = i_size_read(inode);
  2229. if ((page->mapping != inode->i_mapping) ||
  2230. (page_offset(page) > size)) {
  2231. /* We overload EFAULT to mean page got truncated */
  2232. ret = -EFAULT;
  2233. goto out_unlock;
  2234. }
  2235. /* page is wholly or partially inside EOF */
  2236. if (((page->index + 1) << PAGE_SHIFT) > size)
  2237. end = size & ~PAGE_MASK;
  2238. else
  2239. end = PAGE_SIZE;
  2240. ret = __block_write_begin(page, 0, end, get_block);
  2241. if (!ret)
  2242. ret = block_commit_write(page, 0, end);
  2243. if (unlikely(ret < 0))
  2244. goto out_unlock;
  2245. set_page_dirty(page);
  2246. wait_for_stable_page(page);
  2247. return 0;
  2248. out_unlock:
  2249. unlock_page(page);
  2250. return ret;
  2251. }
  2252. EXPORT_SYMBOL(block_page_mkwrite);
  2253. /*
  2254. * nobh_write_begin()'s prereads are special: the buffer_heads are freed
  2255. * immediately, while under the page lock. So it needs a special end_io
  2256. * handler which does not touch the bh after unlocking it.
  2257. */
  2258. static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
  2259. {
  2260. __end_buffer_read_notouch(bh, uptodate);
  2261. }
  2262. /*
  2263. * Attach the singly-linked list of buffers created by nobh_write_begin, to
  2264. * the page (converting it to circular linked list and taking care of page
  2265. * dirty races).
  2266. */
  2267. static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
  2268. {
  2269. struct buffer_head *bh;
  2270. BUG_ON(!PageLocked(page));
  2271. spin_lock(&page->mapping->private_lock);
  2272. bh = head;
  2273. do {
  2274. if (PageDirty(page))
  2275. set_buffer_dirty(bh);
  2276. if (!bh->b_this_page)
  2277. bh->b_this_page = head;
  2278. bh = bh->b_this_page;
  2279. } while (bh != head);
  2280. attach_page_buffers(page, head);
  2281. spin_unlock(&page->mapping->private_lock);
  2282. }
  2283. /*
  2284. * On entry, the page is fully not uptodate.
  2285. * On exit the page is fully uptodate in the areas outside (from,to)
  2286. * The filesystem needs to handle block truncation upon failure.
  2287. */
  2288. int nobh_write_begin(struct address_space *mapping,
  2289. loff_t pos, unsigned len, unsigned flags,
  2290. struct page **pagep, void **fsdata,
  2291. get_block_t *get_block)
  2292. {
  2293. struct inode *inode = mapping->host;
  2294. const unsigned blkbits = inode->i_blkbits;
  2295. const unsigned blocksize = 1 << blkbits;
  2296. struct buffer_head *head, *bh;
  2297. struct page *page;
  2298. pgoff_t index;
  2299. unsigned from, to;
  2300. unsigned block_in_page;
  2301. unsigned block_start, block_end;
  2302. sector_t block_in_file;
  2303. int nr_reads = 0;
  2304. int ret = 0;
  2305. int is_mapped_to_disk = 1;
  2306. index = pos >> PAGE_SHIFT;
  2307. from = pos & (PAGE_SIZE - 1);
  2308. to = from + len;
  2309. page = grab_cache_page_write_begin(mapping, index, flags);
  2310. if (!page)
  2311. return -ENOMEM;
  2312. *pagep = page;
  2313. *fsdata = NULL;
  2314. if (page_has_buffers(page)) {
  2315. ret = __block_write_begin(page, pos, len, get_block);
  2316. if (unlikely(ret))
  2317. goto out_release;
  2318. return ret;
  2319. }
  2320. if (PageMappedToDisk(page))
  2321. return 0;
  2322. /*
  2323. * Allocate buffers so that we can keep track of state, and potentially
  2324. * attach them to the page if an error occurs. In the common case of
  2325. * no error, they will just be freed again without ever being attached
  2326. * to the page (which is all OK, because we're under the page lock).
  2327. *
  2328. * Be careful: the buffer linked list is a NULL terminated one, rather
  2329. * than the circular one we're used to.
  2330. */
  2331. head = alloc_page_buffers(page, blocksize, 0);
  2332. if (!head) {
  2333. ret = -ENOMEM;
  2334. goto out_release;
  2335. }
  2336. block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
  2337. /*
  2338. * We loop across all blocks in the page, whether or not they are
  2339. * part of the affected region. This is so we can discover if the
  2340. * page is fully mapped-to-disk.
  2341. */
  2342. for (block_start = 0, block_in_page = 0, bh = head;
  2343. block_start < PAGE_SIZE;
  2344. block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
  2345. int create;
  2346. block_end = block_start + blocksize;
  2347. bh->b_state = 0;
  2348. create = 1;
  2349. if (block_start >= to)
  2350. create = 0;
  2351. ret = get_block(inode, block_in_file + block_in_page,
  2352. bh, create);
  2353. if (ret)
  2354. goto failed;
  2355. if (!buffer_mapped(bh))
  2356. is_mapped_to_disk = 0;
  2357. if (buffer_new(bh))
  2358. unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
  2359. if (PageUptodate(page)) {
  2360. set_buffer_uptodate(bh);
  2361. continue;
  2362. }
  2363. if (buffer_new(bh) || !buffer_mapped(bh)) {
  2364. zero_user_segments(page, block_start, from,
  2365. to, block_end);
  2366. continue;
  2367. }
  2368. if (buffer_uptodate(bh))
  2369. continue; /* reiserfs does this */
  2370. if (block_start < from || block_end > to) {
  2371. lock_buffer(bh);
  2372. bh->b_end_io = end_buffer_read_nobh;
  2373. submit_bh(REQ_OP_READ, 0, bh);
  2374. nr_reads++;
  2375. }
  2376. }
  2377. if (nr_reads) {
  2378. /*
  2379. * The page is locked, so these buffers are protected from
  2380. * any VM or truncate activity. Hence we don't need to care
  2381. * for the buffer_head refcounts.
  2382. */
  2383. for (bh = head; bh; bh = bh->b_this_page) {
  2384. wait_on_buffer(bh);
  2385. if (!buffer_uptodate(bh))
  2386. ret = -EIO;
  2387. }
  2388. if (ret)
  2389. goto failed;
  2390. }
  2391. if (is_mapped_to_disk)
  2392. SetPageMappedToDisk(page);
  2393. *fsdata = head; /* to be released by nobh_write_end */
  2394. return 0;
  2395. failed:
  2396. BUG_ON(!ret);
  2397. /*
  2398. * Error recovery is a bit difficult. We need to zero out blocks that
  2399. * were newly allocated, and dirty them to ensure they get written out.
  2400. * Buffers need to be attached to the page at this point, otherwise
  2401. * the handling of potential IO errors during writeout would be hard
  2402. * (could try doing synchronous writeout, but what if that fails too?)
  2403. */
  2404. attach_nobh_buffers(page, head);
  2405. page_zero_new_buffers(page, from, to);
  2406. out_release:
  2407. unlock_page(page);
  2408. put_page(page);
  2409. *pagep = NULL;
  2410. return ret;
  2411. }
  2412. EXPORT_SYMBOL(nobh_write_begin);
  2413. int nobh_write_end(struct file *file, struct address_space *mapping,
  2414. loff_t pos, unsigned len, unsigned copied,
  2415. struct page *page, void *fsdata)
  2416. {
  2417. struct inode *inode = page->mapping->host;
  2418. struct buffer_head *head = fsdata;
  2419. struct buffer_head *bh;
  2420. BUG_ON(fsdata != NULL && page_has_buffers(page));
  2421. if (unlikely(copied < len) && head)
  2422. attach_nobh_buffers(page, head);
  2423. if (page_has_buffers(page))
  2424. return generic_write_end(file, mapping, pos, len,
  2425. copied, page, fsdata);
  2426. SetPageUptodate(page);
  2427. set_page_dirty(page);
  2428. if (pos+copied > inode->i_size) {
  2429. i_size_write(inode, pos+copied);
  2430. mark_inode_dirty(inode);
  2431. }
  2432. unlock_page(page);
  2433. put_page(page);
  2434. while (head) {
  2435. bh = head;
  2436. head = head->b_this_page;
  2437. free_buffer_head(bh);
  2438. }
  2439. return copied;
  2440. }
  2441. EXPORT_SYMBOL(nobh_write_end);
  2442. /*
  2443. * nobh_writepage() - based on block_full_write_page() except
  2444. * that it tries to operate without attaching bufferheads to
  2445. * the page.
  2446. */
  2447. int nobh_writepage(struct page *page, get_block_t *get_block,
  2448. struct writeback_control *wbc)
  2449. {
  2450. struct inode * const inode = page->mapping->host;
  2451. loff_t i_size = i_size_read(inode);
  2452. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2453. unsigned offset;
  2454. int ret;
  2455. /* Is the page fully inside i_size? */
  2456. if (page->index < end_index)
  2457. goto out;
  2458. /* Is the page fully outside i_size? (truncate in progress) */
  2459. offset = i_size & (PAGE_SIZE-1);
  2460. if (page->index >= end_index+1 || !offset) {
  2461. /*
  2462. * The page may have dirty, unmapped buffers. For example,
  2463. * they may have been added in ext3_writepage(). Make them
  2464. * freeable here, so the page does not leak.
  2465. */
  2466. #if 0
  2467. /* Not really sure about this - do we need this ? */
  2468. if (page->mapping->a_ops->invalidatepage)
  2469. page->mapping->a_ops->invalidatepage(page, offset);
  2470. #endif
  2471. unlock_page(page);
  2472. return 0; /* don't care */
  2473. }
  2474. /*
  2475. * The page straddles i_size. It must be zeroed out on each and every
  2476. * writepage invocation because it may be mmapped. "A file is mapped
  2477. * in multiples of the page size. For a file that is not a multiple of
  2478. * the page size, the remaining memory is zeroed when mapped, and
  2479. * writes to that region are not written out to the file."
  2480. */
  2481. zero_user_segment(page, offset, PAGE_SIZE);
  2482. out:
  2483. ret = mpage_writepage(page, get_block, wbc);
  2484. if (ret == -EAGAIN)
  2485. ret = __block_write_full_page(inode, page, get_block, wbc,
  2486. end_buffer_async_write);
  2487. return ret;
  2488. }
  2489. EXPORT_SYMBOL(nobh_writepage);
  2490. int nobh_truncate_page(struct address_space *mapping,
  2491. loff_t from, get_block_t *get_block)
  2492. {
  2493. pgoff_t index = from >> PAGE_SHIFT;
  2494. unsigned offset = from & (PAGE_SIZE-1);
  2495. unsigned blocksize;
  2496. sector_t iblock;
  2497. unsigned length, pos;
  2498. struct inode *inode = mapping->host;
  2499. struct page *page;
  2500. struct buffer_head map_bh;
  2501. int err;
  2502. blocksize = i_blocksize(inode);
  2503. length = offset & (blocksize - 1);
  2504. /* Block boundary? Nothing to do */
  2505. if (!length)
  2506. return 0;
  2507. length = blocksize - length;
  2508. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2509. page = grab_cache_page(mapping, index);
  2510. err = -ENOMEM;
  2511. if (!page)
  2512. goto out;
  2513. if (page_has_buffers(page)) {
  2514. has_buffers:
  2515. unlock_page(page);
  2516. put_page(page);
  2517. return block_truncate_page(mapping, from, get_block);
  2518. }
  2519. /* Find the buffer that contains "offset" */
  2520. pos = blocksize;
  2521. while (offset >= pos) {
  2522. iblock++;
  2523. pos += blocksize;
  2524. }
  2525. map_bh.b_size = blocksize;
  2526. map_bh.b_state = 0;
  2527. err = get_block(inode, iblock, &map_bh, 0);
  2528. if (err)
  2529. goto unlock;
  2530. /* unmapped? It's a hole - nothing to do */
  2531. if (!buffer_mapped(&map_bh))
  2532. goto unlock;
  2533. /* Ok, it's mapped. Make sure it's up-to-date */
  2534. if (!PageUptodate(page)) {
  2535. err = mapping->a_ops->readpage(NULL, page);
  2536. if (err) {
  2537. put_page(page);
  2538. goto out;
  2539. }
  2540. lock_page(page);
  2541. if (!PageUptodate(page)) {
  2542. err = -EIO;
  2543. goto unlock;
  2544. }
  2545. if (page_has_buffers(page))
  2546. goto has_buffers;
  2547. }
  2548. zero_user(page, offset, length);
  2549. set_page_dirty(page);
  2550. err = 0;
  2551. unlock:
  2552. unlock_page(page);
  2553. put_page(page);
  2554. out:
  2555. return err;
  2556. }
  2557. EXPORT_SYMBOL(nobh_truncate_page);
  2558. int block_truncate_page(struct address_space *mapping,
  2559. loff_t from, get_block_t *get_block)
  2560. {
  2561. pgoff_t index = from >> PAGE_SHIFT;
  2562. unsigned offset = from & (PAGE_SIZE-1);
  2563. unsigned blocksize;
  2564. sector_t iblock;
  2565. unsigned length, pos;
  2566. struct inode *inode = mapping->host;
  2567. struct page *page;
  2568. struct buffer_head *bh;
  2569. int err;
  2570. blocksize = i_blocksize(inode);
  2571. length = offset & (blocksize - 1);
  2572. /* Block boundary? Nothing to do */
  2573. if (!length)
  2574. return 0;
  2575. length = blocksize - length;
  2576. iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
  2577. page = grab_cache_page(mapping, index);
  2578. err = -ENOMEM;
  2579. if (!page)
  2580. goto out;
  2581. if (!page_has_buffers(page))
  2582. create_empty_buffers(page, blocksize, 0);
  2583. /* Find the buffer that contains "offset" */
  2584. bh = page_buffers(page);
  2585. pos = blocksize;
  2586. while (offset >= pos) {
  2587. bh = bh->b_this_page;
  2588. iblock++;
  2589. pos += blocksize;
  2590. }
  2591. err = 0;
  2592. if (!buffer_mapped(bh)) {
  2593. WARN_ON(bh->b_size != blocksize);
  2594. err = get_block(inode, iblock, bh, 0);
  2595. if (err)
  2596. goto unlock;
  2597. /* unmapped? It's a hole - nothing to do */
  2598. if (!buffer_mapped(bh))
  2599. goto unlock;
  2600. }
  2601. /* Ok, it's mapped. Make sure it's up-to-date */
  2602. if (PageUptodate(page))
  2603. set_buffer_uptodate(bh);
  2604. if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
  2605. err = -EIO;
  2606. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  2607. wait_on_buffer(bh);
  2608. /* Uhhuh. Read error. Complain and punt. */
  2609. if (!buffer_uptodate(bh))
  2610. goto unlock;
  2611. }
  2612. zero_user(page, offset, length);
  2613. mark_buffer_dirty(bh);
  2614. err = 0;
  2615. unlock:
  2616. unlock_page(page);
  2617. put_page(page);
  2618. out:
  2619. return err;
  2620. }
  2621. EXPORT_SYMBOL(block_truncate_page);
  2622. /*
  2623. * The generic ->writepage function for buffer-backed address_spaces
  2624. */
  2625. int block_write_full_page(struct page *page, get_block_t *get_block,
  2626. struct writeback_control *wbc)
  2627. {
  2628. struct inode * const inode = page->mapping->host;
  2629. loff_t i_size = i_size_read(inode);
  2630. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2631. unsigned offset;
  2632. /* Is the page fully inside i_size? */
  2633. if (page->index < end_index)
  2634. return __block_write_full_page(inode, page, get_block, wbc,
  2635. end_buffer_async_write);
  2636. /* Is the page fully outside i_size? (truncate in progress) */
  2637. offset = i_size & (PAGE_SIZE-1);
  2638. if (page->index >= end_index+1 || !offset) {
  2639. /*
  2640. * The page may have dirty, unmapped buffers. For example,
  2641. * they may have been added in ext3_writepage(). Make them
  2642. * freeable here, so the page does not leak.
  2643. */
  2644. do_invalidatepage(page, 0, PAGE_SIZE);
  2645. unlock_page(page);
  2646. return 0; /* don't care */
  2647. }
  2648. /*
  2649. * The page straddles i_size. It must be zeroed out on each and every
  2650. * writepage invocation because it may be mmapped. "A file is mapped
  2651. * in multiples of the page size. For a file that is not a multiple of
  2652. * the page size, the remaining memory is zeroed when mapped, and
  2653. * writes to that region are not written out to the file."
  2654. */
  2655. zero_user_segment(page, offset, PAGE_SIZE);
  2656. return __block_write_full_page(inode, page, get_block, wbc,
  2657. end_buffer_async_write);
  2658. }
  2659. EXPORT_SYMBOL(block_write_full_page);
  2660. sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
  2661. get_block_t *get_block)
  2662. {
  2663. struct buffer_head tmp;
  2664. struct inode *inode = mapping->host;
  2665. tmp.b_state = 0;
  2666. tmp.b_blocknr = 0;
  2667. tmp.b_size = i_blocksize(inode);
  2668. get_block(inode, block, &tmp, 0);
  2669. return tmp.b_blocknr;
  2670. }
  2671. EXPORT_SYMBOL(generic_block_bmap);
  2672. static void end_bio_bh_io_sync(struct bio *bio)
  2673. {
  2674. struct buffer_head *bh = bio->bi_private;
  2675. if (unlikely(bio_flagged(bio, BIO_QUIET)))
  2676. set_bit(BH_Quiet, &bh->b_state);
  2677. bh->b_end_io(bh, !bio->bi_error);
  2678. bio_put(bio);
  2679. }
  2680. /*
  2681. * This allows us to do IO even on the odd last sectors
  2682. * of a device, even if the block size is some multiple
  2683. * of the physical sector size.
  2684. *
  2685. * We'll just truncate the bio to the size of the device,
  2686. * and clear the end of the buffer head manually.
  2687. *
  2688. * Truly out-of-range accesses will turn into actual IO
  2689. * errors, this only handles the "we need to be able to
  2690. * do IO at the final sector" case.
  2691. */
  2692. void guard_bio_eod(int op, struct bio *bio)
  2693. {
  2694. sector_t maxsector;
  2695. struct bio_vec *bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
  2696. unsigned truncated_bytes;
  2697. maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
  2698. if (!maxsector)
  2699. return;
  2700. /*
  2701. * If the *whole* IO is past the end of the device,
  2702. * let it through, and the IO layer will turn it into
  2703. * an EIO.
  2704. */
  2705. if (unlikely(bio->bi_iter.bi_sector >= maxsector))
  2706. return;
  2707. maxsector -= bio->bi_iter.bi_sector;
  2708. if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
  2709. return;
  2710. /* Uhhuh. We've got a bio that straddles the device size! */
  2711. truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9);
  2712. /* Truncate the bio.. */
  2713. bio->bi_iter.bi_size -= truncated_bytes;
  2714. bvec->bv_len -= truncated_bytes;
  2715. /* ..and clear the end of the buffer for reads */
  2716. if (op == REQ_OP_READ) {
  2717. zero_user(bvec->bv_page, bvec->bv_offset + bvec->bv_len,
  2718. truncated_bytes);
  2719. }
  2720. }
  2721. static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
  2722. unsigned long bio_flags, struct writeback_control *wbc)
  2723. {
  2724. struct bio *bio;
  2725. BUG_ON(!buffer_locked(bh));
  2726. BUG_ON(!buffer_mapped(bh));
  2727. BUG_ON(!bh->b_end_io);
  2728. BUG_ON(buffer_delay(bh));
  2729. BUG_ON(buffer_unwritten(bh));
  2730. /*
  2731. * Only clear out a write error when rewriting
  2732. */
  2733. if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
  2734. clear_buffer_write_io_error(bh);
  2735. /*
  2736. * from here on down, it's all bio -- do the initial mapping,
  2737. * submit_bio -> generic_make_request may further map this bio around
  2738. */
  2739. bio = bio_alloc(GFP_NOIO, 1);
  2740. if (wbc) {
  2741. wbc_init_bio(wbc, bio);
  2742. wbc_account_io(wbc, bh->b_page, bh->b_size);
  2743. }
  2744. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  2745. bio->bi_bdev = bh->b_bdev;
  2746. bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  2747. BUG_ON(bio->bi_iter.bi_size != bh->b_size);
  2748. bio->bi_end_io = end_bio_bh_io_sync;
  2749. bio->bi_private = bh;
  2750. bio->bi_flags |= bio_flags;
  2751. /* Take care of bh's that straddle the end of the device */
  2752. guard_bio_eod(op, bio);
  2753. if (buffer_meta(bh))
  2754. op_flags |= REQ_META;
  2755. if (buffer_prio(bh))
  2756. op_flags |= REQ_PRIO;
  2757. bio_set_op_attrs(bio, op, op_flags);
  2758. submit_bio(bio);
  2759. return 0;
  2760. }
  2761. int _submit_bh(int op, int op_flags, struct buffer_head *bh,
  2762. unsigned long bio_flags)
  2763. {
  2764. return submit_bh_wbc(op, op_flags, bh, bio_flags, NULL);
  2765. }
  2766. EXPORT_SYMBOL_GPL(_submit_bh);
  2767. int submit_bh(int op, int op_flags, struct buffer_head *bh)
  2768. {
  2769. return submit_bh_wbc(op, op_flags, bh, 0, NULL);
  2770. }
  2771. EXPORT_SYMBOL(submit_bh);
  2772. /**
  2773. * ll_rw_block: low-level access to block devices (DEPRECATED)
  2774. * @op: whether to %READ or %WRITE
  2775. * @op_flags: rq_flag_bits
  2776. * @nr: number of &struct buffer_heads in the array
  2777. * @bhs: array of pointers to &struct buffer_head
  2778. *
  2779. * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
  2780. * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
  2781. * @op_flags contains flags modifying the detailed I/O behavior, most notably
  2782. * %REQ_RAHEAD.
  2783. *
  2784. * This function drops any buffer that it cannot get a lock on (with the
  2785. * BH_Lock state bit), any buffer that appears to be clean when doing a write
  2786. * request, and any buffer that appears to be up-to-date when doing read
  2787. * request. Further it marks as clean buffers that are processed for
  2788. * writing (the buffer cache won't assume that they are actually clean
  2789. * until the buffer gets unlocked).
  2790. *
  2791. * ll_rw_block sets b_end_io to simple completion handler that marks
  2792. * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
  2793. * any waiters.
  2794. *
  2795. * All of the buffers must be for the same device, and must also be a
  2796. * multiple of the current approved size for the device.
  2797. */
  2798. void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
  2799. {
  2800. int i;
  2801. for (i = 0; i < nr; i++) {
  2802. struct buffer_head *bh = bhs[i];
  2803. if (!trylock_buffer(bh))
  2804. continue;
  2805. if (op == WRITE) {
  2806. if (test_clear_buffer_dirty(bh)) {
  2807. bh->b_end_io = end_buffer_write_sync;
  2808. get_bh(bh);
  2809. submit_bh(op, op_flags, bh);
  2810. continue;
  2811. }
  2812. } else {
  2813. if (!buffer_uptodate(bh)) {
  2814. bh->b_end_io = end_buffer_read_sync;
  2815. get_bh(bh);
  2816. submit_bh(op, op_flags, bh);
  2817. continue;
  2818. }
  2819. }
  2820. unlock_buffer(bh);
  2821. }
  2822. }
  2823. EXPORT_SYMBOL(ll_rw_block);
  2824. void write_dirty_buffer(struct buffer_head *bh, int op_flags)
  2825. {
  2826. lock_buffer(bh);
  2827. if (!test_clear_buffer_dirty(bh)) {
  2828. unlock_buffer(bh);
  2829. return;
  2830. }
  2831. bh->b_end_io = end_buffer_write_sync;
  2832. get_bh(bh);
  2833. submit_bh(REQ_OP_WRITE, op_flags, bh);
  2834. }
  2835. EXPORT_SYMBOL(write_dirty_buffer);
  2836. /*
  2837. * For a data-integrity writeout, we need to wait upon any in-progress I/O
  2838. * and then start new I/O and then wait upon it. The caller must have a ref on
  2839. * the buffer_head.
  2840. */
  2841. int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
  2842. {
  2843. int ret = 0;
  2844. WARN_ON(atomic_read(&bh->b_count) < 1);
  2845. lock_buffer(bh);
  2846. if (test_clear_buffer_dirty(bh)) {
  2847. get_bh(bh);
  2848. bh->b_end_io = end_buffer_write_sync;
  2849. ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
  2850. wait_on_buffer(bh);
  2851. if (!ret && !buffer_uptodate(bh))
  2852. ret = -EIO;
  2853. } else {
  2854. unlock_buffer(bh);
  2855. }
  2856. return ret;
  2857. }
  2858. EXPORT_SYMBOL(__sync_dirty_buffer);
  2859. int sync_dirty_buffer(struct buffer_head *bh)
  2860. {
  2861. return __sync_dirty_buffer(bh, WRITE_SYNC);
  2862. }
  2863. EXPORT_SYMBOL(sync_dirty_buffer);
  2864. /*
  2865. * try_to_free_buffers() checks if all the buffers on this particular page
  2866. * are unused, and releases them if so.
  2867. *
  2868. * Exclusion against try_to_free_buffers may be obtained by either
  2869. * locking the page or by holding its mapping's private_lock.
  2870. *
  2871. * If the page is dirty but all the buffers are clean then we need to
  2872. * be sure to mark the page clean as well. This is because the page
  2873. * may be against a block device, and a later reattachment of buffers
  2874. * to a dirty page will set *all* buffers dirty. Which would corrupt
  2875. * filesystem data on the same device.
  2876. *
  2877. * The same applies to regular filesystem pages: if all the buffers are
  2878. * clean then we set the page clean and proceed. To do that, we require
  2879. * total exclusion from __set_page_dirty_buffers(). That is obtained with
  2880. * private_lock.
  2881. *
  2882. * try_to_free_buffers() is non-blocking.
  2883. */
  2884. static inline int buffer_busy(struct buffer_head *bh)
  2885. {
  2886. return atomic_read(&bh->b_count) |
  2887. (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
  2888. }
  2889. static int
  2890. drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
  2891. {
  2892. struct buffer_head *head = page_buffers(page);
  2893. struct buffer_head *bh;
  2894. bh = head;
  2895. do {
  2896. if (buffer_write_io_error(bh) && page->mapping)
  2897. mapping_set_error(page->mapping, -EIO);
  2898. if (buffer_busy(bh))
  2899. goto failed;
  2900. bh = bh->b_this_page;
  2901. } while (bh != head);
  2902. do {
  2903. struct buffer_head *next = bh->b_this_page;
  2904. if (bh->b_assoc_map)
  2905. __remove_assoc_queue(bh);
  2906. bh = next;
  2907. } while (bh != head);
  2908. *buffers_to_free = head;
  2909. __clear_page_buffers(page);
  2910. return 1;
  2911. failed:
  2912. return 0;
  2913. }
  2914. int try_to_free_buffers(struct page *page)
  2915. {
  2916. struct address_space * const mapping = page->mapping;
  2917. struct buffer_head *buffers_to_free = NULL;
  2918. int ret = 0;
  2919. BUG_ON(!PageLocked(page));
  2920. if (PageWriteback(page))
  2921. return 0;
  2922. if (mapping == NULL) { /* can this still happen? */
  2923. ret = drop_buffers(page, &buffers_to_free);
  2924. goto out;
  2925. }
  2926. spin_lock(&mapping->private_lock);
  2927. ret = drop_buffers(page, &buffers_to_free);
  2928. /*
  2929. * If the filesystem writes its buffers by hand (eg ext3)
  2930. * then we can have clean buffers against a dirty page. We
  2931. * clean the page here; otherwise the VM will never notice
  2932. * that the filesystem did any IO at all.
  2933. *
  2934. * Also, during truncate, discard_buffer will have marked all
  2935. * the page's buffers clean. We discover that here and clean
  2936. * the page also.
  2937. *
  2938. * private_lock must be held over this entire operation in order
  2939. * to synchronise against __set_page_dirty_buffers and prevent the
  2940. * dirty bit from being lost.
  2941. */
  2942. if (ret)
  2943. cancel_dirty_page(page);
  2944. spin_unlock(&mapping->private_lock);
  2945. out:
  2946. if (buffers_to_free) {
  2947. struct buffer_head *bh = buffers_to_free;
  2948. do {
  2949. struct buffer_head *next = bh->b_this_page;
  2950. free_buffer_head(bh);
  2951. bh = next;
  2952. } while (bh != buffers_to_free);
  2953. }
  2954. return ret;
  2955. }
  2956. EXPORT_SYMBOL(try_to_free_buffers);
  2957. /*
  2958. * There are no bdflush tunables left. But distributions are
  2959. * still running obsolete flush daemons, so we terminate them here.
  2960. *
  2961. * Use of bdflush() is deprecated and will be removed in a future kernel.
  2962. * The `flush-X' kernel threads fully replace bdflush daemons and this call.
  2963. */
  2964. SYSCALL_DEFINE2(bdflush, int, func, long, data)
  2965. {
  2966. static int msg_count;
  2967. if (!capable(CAP_SYS_ADMIN))
  2968. return -EPERM;
  2969. if (msg_count < 5) {
  2970. msg_count++;
  2971. printk(KERN_INFO
  2972. "warning: process `%s' used the obsolete bdflush"
  2973. " system call\n", current->comm);
  2974. printk(KERN_INFO "Fix your initscripts?\n");
  2975. }
  2976. if (func == 1)
  2977. do_exit(0);
  2978. return 0;
  2979. }
  2980. /*
  2981. * Buffer-head allocation
  2982. */
  2983. static struct kmem_cache *bh_cachep __read_mostly;
  2984. /*
  2985. * Once the number of bh's in the machine exceeds this level, we start
  2986. * stripping them in writeback.
  2987. */
  2988. static unsigned long max_buffer_heads;
  2989. int buffer_heads_over_limit;
  2990. struct bh_accounting {
  2991. int nr; /* Number of live bh's */
  2992. int ratelimit; /* Limit cacheline bouncing */
  2993. };
  2994. static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
  2995. static void recalc_bh_state(void)
  2996. {
  2997. int i;
  2998. int tot = 0;
  2999. if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
  3000. return;
  3001. __this_cpu_write(bh_accounting.ratelimit, 0);
  3002. for_each_online_cpu(i)
  3003. tot += per_cpu(bh_accounting, i).nr;
  3004. buffer_heads_over_limit = (tot > max_buffer_heads);
  3005. }
  3006. struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  3007. {
  3008. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  3009. if (ret) {
  3010. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  3011. preempt_disable();
  3012. __this_cpu_inc(bh_accounting.nr);
  3013. recalc_bh_state();
  3014. preempt_enable();
  3015. }
  3016. return ret;
  3017. }
  3018. EXPORT_SYMBOL(alloc_buffer_head);
  3019. void free_buffer_head(struct buffer_head *bh)
  3020. {
  3021. BUG_ON(!list_empty(&bh->b_assoc_buffers));
  3022. kmem_cache_free(bh_cachep, bh);
  3023. preempt_disable();
  3024. __this_cpu_dec(bh_accounting.nr);
  3025. recalc_bh_state();
  3026. preempt_enable();
  3027. }
  3028. EXPORT_SYMBOL(free_buffer_head);
  3029. static void buffer_exit_cpu(int cpu)
  3030. {
  3031. int i;
  3032. struct bh_lru *b = &per_cpu(bh_lrus, cpu);
  3033. for (i = 0; i < BH_LRU_SIZE; i++) {
  3034. brelse(b->bhs[i]);
  3035. b->bhs[i] = NULL;
  3036. }
  3037. this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
  3038. per_cpu(bh_accounting, cpu).nr = 0;
  3039. }
  3040. static int buffer_cpu_notify(struct notifier_block *self,
  3041. unsigned long action, void *hcpu)
  3042. {
  3043. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  3044. buffer_exit_cpu((unsigned long)hcpu);
  3045. return NOTIFY_OK;
  3046. }
  3047. /**
  3048. * bh_uptodate_or_lock - Test whether the buffer is uptodate
  3049. * @bh: struct buffer_head
  3050. *
  3051. * Return true if the buffer is up-to-date and false,
  3052. * with the buffer locked, if not.
  3053. */
  3054. int bh_uptodate_or_lock(struct buffer_head *bh)
  3055. {
  3056. if (!buffer_uptodate(bh)) {
  3057. lock_buffer(bh);
  3058. if (!buffer_uptodate(bh))
  3059. return 0;
  3060. unlock_buffer(bh);
  3061. }
  3062. return 1;
  3063. }
  3064. EXPORT_SYMBOL(bh_uptodate_or_lock);
  3065. /**
  3066. * bh_submit_read - Submit a locked buffer for reading
  3067. * @bh: struct buffer_head
  3068. *
  3069. * Returns zero on success and -EIO on error.
  3070. */
  3071. int bh_submit_read(struct buffer_head *bh)
  3072. {
  3073. BUG_ON(!buffer_locked(bh));
  3074. if (buffer_uptodate(bh)) {
  3075. unlock_buffer(bh);
  3076. return 0;
  3077. }
  3078. get_bh(bh);
  3079. bh->b_end_io = end_buffer_read_sync;
  3080. submit_bh(REQ_OP_READ, 0, bh);
  3081. wait_on_buffer(bh);
  3082. if (buffer_uptodate(bh))
  3083. return 0;
  3084. return -EIO;
  3085. }
  3086. EXPORT_SYMBOL(bh_submit_read);
  3087. void __init buffer_init(void)
  3088. {
  3089. unsigned long nrpages;
  3090. bh_cachep = kmem_cache_create("buffer_head",
  3091. sizeof(struct buffer_head), 0,
  3092. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  3093. SLAB_MEM_SPREAD),
  3094. NULL);
  3095. /*
  3096. * Limit the bh occupancy to 10% of ZONE_NORMAL
  3097. */
  3098. nrpages = (nr_free_buffer_pages() * 10) / 100;
  3099. max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
  3100. hotcpu_notifier(buffer_cpu_notify, 0);
  3101. }