buffer.c 90 KB

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