buffer.c 87 KB

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