buffer.c 80 KB

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