buffer.c 88 KB

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