buffer.c 82 KB

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