buffer.c 85 KB

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