buffer.c 88 KB

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