buffer.c 89 KB

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