buffer.c 87 KB

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