buffer.c 87 KB

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