buffer.c 89 KB

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