buffer.c 87 KB

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