buffer.c 94 KB

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